Understanding the Policy Injection Block

Use the Policy Injection Block to Manage Cross-cutting Concerns in Your Applications

Joydip Kanjilal

October 30, 2009

3 Min Read
ITPro Today logo

asp:Feature

LANGUAGES:C#

ASP.NETVERSIONS: 2.0

 

Understanding the Policy Injection Block

Use the Policy Injection Block to Manage Cross-cutting Concernsin Your Applications

 

By Joydip Kanjilal

 

The Policy Injection Block from Microsoft is great forseparation of concerns. The Policy Injection Block is a part of the February2007 CTP of the Microsoft Enterprise Library and can be used to provide AspectOriented Programming (AOP) features in your applications. You can use it toisolate the core concerns from the cross-cutting concerns of your application.This article presents the features of this block and their applicability.

 

Using the Policy Injection Block

According to MSDN:

 

Applications include a mix of business logic andcrosscutting concerns, and the two are typically intermingled which can makethe code harder to read and maintain. Each task or feature of an application isreferred to as a concern. Concerns that implement the features of an objectwithin the application, such as the business logic, are core concerns.Crosscutting concerns are the necessary tasks, features, or processes that arecommon across different objects for example, logging, authorization,validation, and instrumentation. The purpose of the Policy InjectionApplication Block is to separate the core concerns and crosscutting concerns.

 

To use the Policy Injection Block, you must add referencesto the assembly files listed here:

  • Microsoft.Practices.ObjectBuilder

  • Microsoft.Practices.EnterpriseLibrary.Common

  • Microsoft.Practices.EnterpriseLibrary.PolicyInjection

  • Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers

 

Then, in your program, you must include the followingnamespaces:

  • Microsoft.Practices.EnterpriseLibrary.PolicyInjection

  • Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers

 

To store the policy information, you must useconfiguration in your config file similar to that shown here:

 

   

       

           

               

               

           

           

               

               

           

       

       

           

               

               

           

           

               

               

           

       

   

 

Fine, but, how do you achieve the AOP functionality wetalked about earlier? You can achieve AOP functionality by intercepting themethod calls in your program and executing the handlers that are defined in theconfiguration file (as shown in the code snippet above).

The AOP functionality is achieved by intercepting themethod calls in your program, then executing the handlers that are pre-definedin the configuration file (see the example code snippet shown earlier). Thehandlers are executed both before and after the method calls. The handlers areused to specify what action needs to be performed once a method call isintercepted. The method calls are actually intercepted based on the definedrules in the configuration file. If these rules match, the method isintercepted and executed is the respective handler as defined in theconfiguration file for that rule that has matched.

 

A policy is actually a combination of the pipeline of thehandlers and the set of the matching rules that are defined in theconfiguration file of an application that uses the Policy Injection Block. Oncethe static Create method that belongs to the factory class namedPolicyInjection is executed, a method call is intercepted by the block, the handlers to be executed are identified and injectedbefore and after those method calls. You can even use your own custom handlersto apply your custom policies.

 

For more information on the Policy Injection Block, lookat the MSDN article Policy Injection Application Block at http://msdn2.microsoft.com/en-us/library/bb410104.aspx.

 

Conclusion

This article has explored the Policy Injection Block fromMicrosoft and how it is beneficial for separation of concerns. Please send meyour comments. Happy reading!

 

Working extensively in Microsoft technologies for more than 10years, Joydip Kanjilal is a MicrosoftMVP in ASP.NET and a Senior Technical Leader in the Design and Architectureteam for a reputed company in Hyderabad, India. He has more than 12 years ofindustry experience in IT. His programming skills include C, C++, Java, C#, VB,.NET, AJAX, VC++, ASP.NET, XML, UMLand Design patterns. He has worked with .NET and C# for more than five years.Reach Joydip at mailto:[email protected]or at his blog at http://aspadvice.com/blogs/joydip/.He also is the author of ASP.NET DataPresentation Controls Essentials (http://www.packtpub.com/author_view_profile/id/176).His hobbies include watching cricket and soccer and playing chess.

 

 

 

 

Read more about:

Microsoft
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like