Welcome to MSDN Blogs Sign in | Join | Help

Announcing the Policy Injection Application Block

Just when you thought you knew everything there was to know about Enterprise Library 3.0, we have one last surprise in store: a new application block! The Policy Injection Application Block got off to a late start, but we're making good progress now and we'll ship the first public drop in the February 2007 CTP. The final release of Enterprise Library 3.0 (scheduled for late March) will of course contain the final release.

But first things first: what the hell is this thing? The short(ish) answer is that the Policy Injection Application Block will simplify the separation of business logic from cross cutting concerns, by letting you define policies and the objects/methods they apply to in a declarative way. Each policy contains a pipeline of "handlers" that are executed before and after a policy-enabled method is called. The handlers can do whatever you want, but the most common scenario will be to implement cross-cutting concerns such as logging, validation, exception handling and authorization. By amazing coincidence, Enterprise Library already includes blocks that implement these kinds of capabilities, so our out-of-the-box handlers will be simple wrappers over existing application blocks.

For a detailed description of the design and the rationale behind it, check out Ed Jezierski's post - but to get you started, here are a few more details on how this will work. The Policy Injection Application Block will provide a special Factory class that you can use to create instances of objects that may potentially have policies defined on them. I say potentially as one of the big benefits of this block is that you can define and change the policies through configuration, so you won't actually know at compile time which objects will have policies defined. The factory will inspect the configuration and determine if a policy exists. If no such policy exists, the factory will create an instance of the requested object and return it. If a policy does exist, the factory will create a proxy (technically a transparent proxy / real proxy pair) for the object, and wire this up to the chain of handlers and ultimately the real object. To the client, the proxy looks and smells exactly like the real object, but it will have the policies injected between the proxy and the real object.

The configuration for the block will look something like this:

  • Policy Injection Application Block
    • MyPolicy1
      • Matching Rules
        • MyMatchingRule1
        • MyMatchingRule2
        • ...
      • Handlers
        • MyHandler1
        • MyHandler2
        • ...
    • MyPolicy2
      • ...
    • ...

As you can see, each policy consists of a collection of matching rules and a collection of handlers. A matching rule is basically a predicate that defines which types and members the policy should apply to. If you specify more than one matching rule, all must apply for a policy to take effect. Out of the box we will ship matching rules that check for a specific assembly, namespace, type, method signature, or specific attributes on type or member definitions. Of course you'll also be able to write your own matching rules, should you want a policy to apply only when the moon is full or something similarly obscure. In the event that a particular class/method matches more than one policy, all of the matching policies will be executed in the order that they are defined.

Handlers are the classes that do the interesting work before and/or after a method is called. A handler is able to inspect the payload of the method call or return value, act on it, and even change it. In normal situations each handler will do its preprocessing work, invoke the next handler, wait for the call to return, do postprocessing work, and return control to the previous handler. However in certain situations a handler may decide not to call the next handler at all. One good example is an Authorization Handler. Its job is to check whether the current user is authorized to call the method. If the check shows the user is good to go, the handler will invoke the next handler in the chain. However if the user is not authorized, the handler will create an exception, package it up and send it back to the previous handler in the chain as shown below:

From the client's perspective, the result will appear identical to what would happen if the target object threw the exception itself, although in this case the exception was generated without ever calling the real object.

As I mentioned before, the handlers we will supply out of the box will be thin wrappers around existing application blocks, although again you can easily create your own. While the number of handlers and the details on how they will work are not entirely set in concrete, here is what we are hoping to include:

  • Validation Handler. This will look for validation rules applied on the method parameters or within types in the message signature, and call the Validation Application Block to check the supplied parameters against the validation rules. If validation succeeds the method will be called. If not, the handler chain will be aborted and an exception will be returned to the client.
  • Logging Handler. This will call the Logging Application Block to write a log message either before the method is called, after the method is called, or both. Optionally the handler will be able to log details such as supplied parameter values and call execution time.
  • Exception Handling Handler. This handler will do nothing before the method is called, and do nothing after the method is called unless an exception was thrown. If an exception was thrown, the handler will pass it to the Exception Handling Application Block using a specified exception policy, and any resulting exceptions will be returned to the client. In effect this will eliminate the need for boilerplate exception blocks when using the Exception Handling Application Block.
  • Performance Counter Handler. This will create a number of performance counter instances measuring things like number of calls, calls per second, average call execution time and number and rate of exceptions thrown by the target method.
  • Authorization Handler. This handler will use a specified authorization provider configured with the Security Application Block to check if the current user (obtained from the thread principal) is authorized to perform the current task (specified in the handler configuration). If so, the target object is called. If not, an exception will be returned to the client.
  • Caching Handler. This handler will generate a cache key based on the target method signature and values, and use the Caching Application Block to see if the method has already been called with these values within the configured cache threshold. If a value was found in the cache, it will be returned to the client and the handler chain will be aborted (i.e. the object will not actually be called). If a value was not found in the cache, the method will be called, and the return value will be added to the cache on the return path.

We're very excited by the potential for this block to help simplify your code and provide increased flexibility to change cross-cutting concerns at different stages of the application lifecycle. Keep in mind that the first drop of the Policy Injection Application Block (in the February 2007 CTP) will still be incomplete, but we'd love to hear your feedback to help us make the final release as valuable as possible.

By the way, if you're not the kind of person who likes surprises, apologies for springing this on you so late. It wasn't our intention to be secretive, however until very recently we were still not sure if we were going to be able to include this functionality in this release of Enterprise Library, so we didn't want to set expectations too early. Hopefully it was worth the wait, and that you can file it under "good surprises" :-)

Thanks to Alex Homer for the great graphics, which were taken from the draft documentation.

Published Friday, February 23, 2007 9:53 AM by tomholl

Comments

# edjez's WebLog : Policy Injection App Block - Behind the Scenes

Friday, February 23, 2007 1:20 PM by Brad Wilson - The .NET Guy

# Policy Injection App Block

Friday, February 23, 2007 2:19 PM by Kris

# re: Announcing the Policy Injection Application Block

Very cool indeed. Is this all config file driven or are there any attributes to support this functionality a la AOP (Aspect Oriented Prog)

Friday, February 23, 2007 2:43 PM by David Hayden - Florida .NET Developer - C# and SQL Server

# Policy Injection Application Block - New Addition to Enterprise Library 3.0

Friday, February 23, 2007 4:01 PM by tomholl

# re: Announcing the Policy Injection Application Block

Kris: so far it is all configuration driven, although one of the matching rules will check for the existence of a particular attribute, so you can do something like this:

[Tag("AuthorizeMe")]

public void DoStuff()

... where there is a policy defined in configuration that will look for this particular tag.

For the final release we're hoping to add the ability to explicitly attach handlers to types and methods via attributes, without the need for any configuration.

Tom

Friday, February 23, 2007 6:06 PM by Kevin

# re: Announcing the Policy Injection Application Block

Hi Tom,

I am wondering if (or how) the ObjectBuilder is used in this Policy Injection Application Block?

Thanks

Friday, February 23, 2007 6:36 PM by Miguel Campos Blog

# Just new: Policy Inyection Application Block

Do you wonder how to apply authorization, validation, logging and other requierements to you business

Friday, February 23, 2007 9:03 PM by tomholl

# re: Announcing the Policy Injection Application Block

Kevin - the PIAB uses Object Builder in much the same way as the other blocks, for example by configuring the core components by injecting the configuration data.

Tom

Friday, February 23, 2007 9:39 PM by Being Scott Densmore

# patterns

Saturday, February 24, 2007 12:33 AM by vikasgoyal77

# re: Announcing the Policy Injection Application Block

Hi Tom,

This block is going to be very useful. Thanks for your efforts. Any study on performance impact on execution. I have also started a kind of application block called Web Analytics. Its hosted on CodePlex. Pls let me know your feedback.

http://dotnetwithme.blogspot.com/2007/02/started-my-first-codeplex-project.html

Vikas Goyal

Saturday, February 24, 2007 12:01 PM by Jason Haley

# Interesting Finds: February 24, 2007

Sunday, February 25, 2007 7:04 AM by Anders Norås' Blog

# Software Factories - Another Unlearned EJB Lesson

Many software projects experience disappointing productivity. Often productivity problems are inherent

Sunday, February 25, 2007 5:40 PM by Matt Dunn

# re: Announcing the Policy Injection Application Block

Hi Tom,

Great to see this implemented!

As far as the factory for instance creation is concerned, does this place any restrictions on how instances can be instantiated e.g. only via a default constructor, thus no parameterised constructors?

Cheers,

Matt

Sunday, February 25, 2007 9:18 PM by tomholl

# re: Announcing the Policy Injection Application Block

Matt - take a look at Ed's post for some more details on how the interception mechanism works and how you can replace it with a different mechanism. But with our default remoting proxy mechansim, the main restrictions are that you must create the object with our proxy rather than "new", and the object must either derive from MarshalByRefObject or have an explicit interface for marshalling. I don't believe there are any restrictions on the use of parameters in the constructor.

Tom

Monday, February 26, 2007 4:06 AM by Patrik Löwendahl

# re: Announcing the Policy Injection Application Block

Why not just go all the way and build an AOP framework which are target towards cross-cutting concerns? It's really what it is, or part of AOP anyway but with a Microsoft name tagged on it (Policy instead of Aspects).

Also, have you tried this with WCF? WCF doesn't really like proxys.

Monday, February 26, 2007 4:08 AM by Patrik Löwendahl

# re: Announcing the Policy Injection Application Block

Another point, this time on the implementation. Why taking the MarshalByRef (or really contextboundobject) path? This is really much slower then dynamic runtime subclassing.

Monday, February 26, 2007 5:56 AM by Patrik Löwendahl

# re: Announcing the Policy Injection Application Block

Some comments summed up in a post:

http://www.lowendahl.net/showShout.aspx?id=118

Monday, February 26, 2007 5:42 PM by patrickyong

# re: Announcing the Policy Injection Application Block

I can't wait to see how PIAB work and I intend to test out on my project which already using WCSF and WSSF. Currently I am using NAspect and I see usage of attribute for matching rule offers a different kind of flexibility for me.

I also wonder how the debugging facility will work in PIAB, ie will the debugger goes into the handler class so I can trace what is going on.

Monday, February 26, 2007 5:59 PM by Kevin

# re: Announcing the Policy Injection Application Block

How about including a Transaction handler out of the box?  Wrap the call inside a system.transaction

Monday, February 26, 2007 7:39 PM by Espresso Fueled Agile Development

# Enterprise Library 3.0 will include a new block

Tom and Ed have complimentary articles on the Policy Injection Application Block: Tom's Announcing the

Tuesday, February 27, 2007 5:29 AM by if ( ! blogClogged )

# Can anyone say Shadowfax?

Announcing the Policy Injection Application Block

Wednesday, February 28, 2007 9:49 PM by Tom Hollander's blog

# Just Released: Enterprise Library 3.0 February 2007 CTP

OK, I know the sun has already set on February in some parts of the world, but over here it's still well

Thursday, March 01, 2007 2:29 AM by RSS It All

# Just Released: Enterprise Library 3.0 February 2007 CTP

OK, I know the sun has already set on February in some parts of the world, but over here it's still

Thursday, March 01, 2007 4:36 AM by Dennis' Blog

# "Orcas" March CTP & EntLib 3.0 Feb CTP

What a week! Both the new Visual Studio "Orcas" and Enterprise Library 3.0 CTPs are released. Enterprise

Thursday, March 01, 2007 11:20 AM by David Hayden [MVP C#]

# Enterprise Library 3.0 February 2007 CTP Released

Microsoft Patterns and Practices released the Enterprise Library 3.0 February 2007 CTP last night. A

Thursday, March 01, 2007 12:22 PM by Eisenberg

# re: Announcing the Policy Injection Application Block

I have to agree with Patrik, context interception is butt slow.  I believe benchmarks put it at 50x slower than standard compile time invocation.  That's almost twice as slow as using reflection to call every property and method!  On top of that, as far as I know, you cannot use generics with context bound objects!  Perhaps you guys better rethink this one.  You might want to go look at some of the existing AOP frameworks.  (ie. Castle's Aspect#)

Thursday, March 01, 2007 12:33 PM by Eisenberg

# re: Announcing the Policy Injection Application Block

Apologies. I realized that you guys aren't actually using context interception, but rather Real/Transparent proxy.  However, the performance drawback is virtually identical.

Thursday, March 01, 2007 3:37 PM by Johan Lindfors

# Policy Injection application block = AOP???

Patrik Löwendahl har gjort mig uppmärksam på att Patterns och Practices gruppen hos Microsoft har annonserat

Friday, March 02, 2007 10:52 AM by Mike Chaliy's Blog

# Що нового?

Цікавий сьогодні день, на новинки... 1) Enterprise Library 3.0 February 2007 CTP . Останній CTP до релізу.

Friday, March 02, 2007 3:09 PM by Sam Gentile

# New and Notable 147

Harry wonders if it has been a slow week. It started that way for me but its certainly not now with both

Sunday, March 04, 2007 10:03 AM by Erwyn van der Meer

# Similarity between EntLib 3.0 Policy Injection Application Block and EDRA

As announced on Tom Hollander's blog, the Microsoft Enterprise Library 3.0 will include a new application

Sunday, March 04, 2007 1:45 PM by ^(?:[^$]*)$ --Matches everything, captures nothing

# Using the Policy Injection Application Block

Since Both Tom and Ed wrote an article explaining whatever the Policy Injection Application Block [PIAB]

Monday, March 05, 2007 11:34 AM by Mount Gellért - az architekt, és aki mögötte van

# Enterprise Library 3.0 februári CTP

Kijött a februári CTPje az Enterprise Library 3.0-nak , ami RCnek is tekinthető. Miért is jó ez nekünk?

Monday, March 05, 2007 2:59 PM by TSHAK

# Enterprise Library Gets Aspects

The next version of Enterprise Library will be getting an AOPish block called the Policy Injection Application...

Wednesday, March 07, 2007 12:49 AM by Tom Hollander's blog

# More ways to inject policies

I wanted to quickly fill you in on a couple of new additions we've made to the Policy Injection Application

Friday, March 09, 2007 12:18 AM by Navs

# re: Announcing the Policy Injection Application Block

This is really a great effort. It will simplify the development by making code clean and configurable. I am concerned about the performance of the PIAB.

Thanks

Sunday, March 18, 2007 6:43 PM by Julius Ganns

# Cross-Cutting Concerns in .NET

By looking at the .NET Framework in earlier versions there has been a notable gap in enterprise functionality

Sunday, March 18, 2007 7:37 PM by Julius Ganns

# Cross-Cutting Concerns in .NET

By looking at the .NET Framework in earlier versions there has been a notable gap in enterprise functionality

Monday, March 19, 2007 2:21 AM by Thomas BT

# Question regarding runtime configuration change

Do we have a possibility to change configurations (Logging configurations for Example) at runtime without restarting the application. Kindly provide links if explanation already available.

Tuesday, March 20, 2007 5:02 AM by 阿伦.NET

# Policy Injection Application Block

PolicyInjectionApplicationBlock

NEW!ThisreleaseofEnterpriseLibraryincludesanewPolicyIn...

Friday, March 23, 2007 12:03 PM by boj

# re: Enterprise Library 3.0 februári CTP

Azért ez nem teljes RC, pont a PIAB döcögős még benne.

Különben is jobb többször mint egyszer sem:)

Thursday, April 05, 2007 6:14 PM by Tom Hollander's blog

# Just Released! Enterprise Library 3.0 - April 2007

Yes, it's finally here. The patterns & practices team is pleased to announce the official release

Thursday, April 05, 2007 7:23 PM by Girish

# re: Announcing the Policy Injection Application Block

How is this different then EDRA?

Thursday, April 05, 2007 7:37 PM by tomholl

# re: Announcing the Policy Injection Application Block

Girish - EDRA used a similar pattern at the service interface level, however the deliverables are very different. EDRA included a framework and reference implementation that provided guidance on the logical and physical deployment of services. PIAB is an application block that lets you externalize cross-cutting concerns from your business code, and doesn't target service interfaces since WCF Behaviors already provide similar capabilities for that scenario.

Tom

Thursday, April 05, 2007 8:15 PM by neuhawk

# Enterprise Library 3.0 – April 2007 发布

Enterprise Library 3.0 – April 2007 发布

Thursday, April 05, 2007 8:42 PM by Bashmohandes

# Enterprise Library 3.0 Released

Enterprise Library 3.0 Released

Friday, April 06, 2007 2:15 AM by Guy Burstein's Blog

# Just Released! Enterprise Library 3.0 - April 2007

The patterns & practices team has announced the official release of Enterprise Library 3.0 - April

Friday, April 06, 2007 2:12 PM by Loser-X

# Enterprise Library 3.0 Released

Most excellent. I have looked at Enterprise Library in the past, but never quite found a suitable project

Friday, April 06, 2007 4:30 PM by Clic Compulsivo

# Enterprise Library 3.0 April 2007

Overview The patterns & practices Enterprise Library is a library of application blocks designed

Friday, April 06, 2007 5:36 PM by Tom Hollander's blog

# Effective Policy Viewer for the Policy Injection Application Block

I hope you're enjoying the new release of Enterprise Library ! One of the more interesting inclusions

Sunday, April 08, 2007 12:47 PM by Tecnocrata Blog

# Enterprise Library 3.0 - Abril 2007 Liberado !!!

Despues de mucho esperaracion oficial del Enterprise Library 3.0 - April 2007 para.NET Framework 2.0 / 3.0. Punntos sobresalientes Si estuvieron tan atentos como yo los CTPs no habra muchas sorpres ...

Monday, April 09, 2007 7:12 AM by El Bruno

# EntLib 3.0: Policy Injection Viewer (cerrando la semana santa, pero sin easter eggs ...)

Buenas hace un par de días se liberó Enterprise Library 3.0 ; y ya empezamos a tener pequeños aplicativos

Monday, April 09, 2007 7:12 AM by El Bruno

# EntLib 3.0: Policy Injection Viewer (cerrando la semana santa, pero sin easter eggs ...)

Buenas hace un par de días se liberó Enterprise Library 3.0 ; y ya empezamos a tener pequeños aplicativos

Monday, April 09, 2007 7:12 AM by El Bruno

# EntLib 3.0: Policy Injection Viewer (cerrando la semana santa, pero sin easter eggs ...)

Buenas hace un par de días se liberó Enterprise Library 3.0 ; y ya empezamos a tener pequeños aplicativos

Monday, April 09, 2007 9:03 AM by JrzyShr Dev Guy

# Enterprise Library 3.0 is Here!

EntLib 3.0 just dropped . Go get the bits here . For more details, see Tom Hollander's most excellent

Tuesday, April 10, 2007 11:56 PM by ...

# re: Announcing the Policy Injection Application Block

Luogo molto buon:) Buona fortuna!

Wednesday, April 11, 2007 5:50 PM by Rags

# re: Announcing the Policy Injection Application Block

Can we use PIAB in WCF services? What I mean is I want to intercept my service call and do some preprocessing before the real service api gets invoked. For ex. one of the api in my service is AddCustomer(Customer obj). What I want is before this api gets called I want  Customer object to go through some validation, default value filler and what not. In my whole Microsoft career I worked for short duration on Java EJB 3.0 recently. I used Interceptor to do such kind of stuff on EJB Bean api's. So can we use PIAB on WCF services to do similar task?

Wednesday, April 11, 2007 7:46 PM by tomholl

# re: Announcing the Policy Injection Application Block

Hi Rags -

While it would be possible to use the PIAB to apply policies against your service implementation class, the preferred way of applying cross-cutting concerns at WCF service boundaries is through Behaviors. In EntLib 3.0 we ship behaviors for a couple of the blocks (exception handling and validation) that are also PIAB enabled.

Tom

Monday, April 23, 2007 4:34 PM by Erics Blog

# Enterprise Library 3.0 Released

While I was gone (three weeks in the US on vacation) pattern and practices have released Enterprise Library

Tuesday, April 24, 2007 5:03 AM by petersm

# Enterprise Library 3.0 februári CTP

Kijött a februári CTPje az Enterprise Library 3.0-nak , ami RCnek is tekinthető. Miért is jó ez nekünk

Tuesday, May 15, 2007 4:10 PM by Guy Burstein's Blog

# Policy Injection Application Block in Enterprise Library 3.0 - Part 1

Continuing my journey in Enterprise Library 3.0 mysterious ways, I decided to check out the Policy Injection

Sunday, May 20, 2007 6:16 AM by BonzBlog Michaela Juřka

# Skryté poklady v Enterpise Library 3.0

Když byla před měsícem zveřejněna Enterprise Library 3.0, stěží jsem tomu věnoval pozornost. Hmmm, co

Saturday, September 08, 2007 6:07 PM by accredited high school diplomas

# accredited high school diplomas

accredited high school diplomas

Monday, October 01, 2007 10:43 PM by generic zoloft no prescription overnight shipping

# generic zoloft no prescription overnight shipping

generic zoloft no prescription overnight shipping

Tuesday, June 24, 2008 5:46 AM by Simon Ince's Blog

# Aspect Oriented Interception

Have you used Aspect Oriented Programming (AOP) or Policy Injection? They’re pretty much the same thing.

Tuesday, July 15, 2008 10:12 AM by My Software Factory

# Separation of Concerns with Policy Injections

It's been a long time that I've been thinking of writing this blog post but was strugling to find some

New Comments to this post are disabled
 
Page view tracker