Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Policy Injection in ASP.NET using Enterprise Library 3.0


Published on

Terminology and Buzzwords
The Policy Injection Application Block
what it does, how it works
Configuring Policies
Using Attributes
Extending the Block
custom Handlers and Matching Rules

Published in: Business, Technology
  • Be the first to comment

Policy Injection in ASP.NET using Enterprise Library 3.0

  1. 1. Policy Injection in ASP.NET using Enterprise Library 3.0 Alex Homer [email_address]
  2. 2. Agenda <ul><li>Terminology and Buzzwords </li></ul><ul><li>The Policy Injection Application Block </li></ul><ul><ul><li>what it does, how it works </li></ul></ul><ul><li>Configuring Policies </li></ul><ul><li>Using Attributes </li></ul><ul><li>Extending the Block </li></ul><ul><ul><li>custom Handlers and Matching Rules </li></ul></ul>
  3. 3. Terminology and Buzzwords <ul><li>Aspect Oriented Programming (AOP) </li></ul><ul><li>Policy Injection </li></ul><ul><li>Remoting Proxies </li></ul><ul><li>Intercepting Proxies </li></ul><ul><li>Method Interception </li></ul><ul><li>The overall aim is better management of Crosscutting Concerns </li></ul>
  4. 4. What are Crosscutting Concerns? Core Concerns Application Business Object Business Object Data Access Object Application Business Object Data Access Object Validation Logging Exception Handling Caching Authorization Crosscutting Concerns Performance Counters
  5. 5. What is Policy Injection? <ul><li>“ ... techniques for creating policies that help to manage crosscutting concerns, and applying these policies across all relevant objects and applications ...” </li></ul><ul><li>Using the Policy Injection Block you can: </li></ul><ul><ul><li>Create handlers for each crosscutting concern </li></ul></ul><ul><ul><li>Define the conditions where they will apply </li></ul></ul><ul><ul><li>Define the behavior in specific circumstances </li></ul></ul><ul><ul><li>Adjust runtime behavior through configuration </li></ul></ul><ul><ul><li>Manage runtime behavior using Group Policy </li></ul></ul>
  6. 6. The Handlers Pipeline <ul><ul><li>Objects must be “interceptable” </li></ul></ul><ul><ul><li>Must use factory Create or Wrap method </li></ul></ul><ul><ul><li>Each handler can execute code in the pre- and/or post-processing stages </li></ul></ul><ul><ul><li>Handlers can short-circuit (abort) execution </li></ul></ul>
  7. 7. Interceptable Object Types (1) <ul><li>C#: </li></ul><ul><li>object = PolicyInjection.Create< type >( ctor_params ) </li></ul><ul><li>object = PolicyInjection.Create< type >(IConfigurationSource, ctor_params ) </li></ul><ul><li>object = PolicyInjection.Wrap< type >( existing_object ) </li></ul><ul><li>object = PolicyInjection.Wrap< type >(IConfigurationSource, existing_object ) </li></ul><ul><ul><li>Types that inherit MarshalByRefObject: </li></ul></ul>VB.NET: object = PolicyInjection.Create(Of type )( ctor_params ) object = PolicyInjection.Create(Of type )(IConfigurationSource, params ) object = PolicyInjection.Wrap(Of type )( existing_object ) object = PolicyInjection.Wrap(Of type )(IConfigurationSource, object ) C# Visual Basic .NET
  8. 8. Interceptable Object Types (2) <ul><li>interface = PolicyInjection.Create< type , interface >( ctor_params ) </li></ul><ul><li>interface = PolicyInjection.Create< type , interface > </li></ul><ul><li>(IConfigurationSource, ctor_params ) </li></ul><ul><li>interface = PolicyInjection.Wrap< interface >( existing_object ) </li></ul><ul><li>interface = PolicyInjection.Wrap< interface > </li></ul><ul><li>(IConfigurationSource, existing_object ) </li></ul><ul><ul><li>Types that implement a known interface: </li></ul></ul>C# interface = PolicyInjection.Create(Of type , interface )( ctor_params ) interface = PolicyInjection.Create (Of type , interface ) _ (IConfigurationSource, ctor_params ) interface = PolicyInjection.Wrap (Of interface ) ( existing_object ) interface = PolicyInjection.Wrap (Of interface ) _ (IConfigurationSource, existing_object ) Visual Basic .NET
  9. 9. Configuring Policies <ul><li>Enterprise Library Configuration Console </li></ul><ul><li>Visual Studio 2005 Configuration Editor </li></ul><ul><li>Manually ( recommended for masochists only ) </li></ul><ul><li>Add the PIAB to your app configuration </li></ul><ul><li>Create the required Policies </li></ul><ul><ul><li>Each policy has one or more matching rules </li></ul></ul><ul><ul><ul><li>Must all evaluate to TRUE to select a target </li></ul></ul></ul><ul><ul><li>Each policy has one or more handlers </li></ul></ul><ul><ul><ul><li>Processed in the order they occur in configuration </li></ul></ul></ul>
  10. 10. Configuring Policies Demo Demo
  11. 11. Demo Business Object Members Public members exposed by the two business objects Member Attributed Customer Model Interface Customer Model Notes GetCustomerList()   GetCustomerName(customerID)   GetCustomerNameWithWildcard (customerID)   Uses GetCustomerName method with a wildcard. GetCustomerDetails(customerID)   Not defined in interface. GetCityList(minimumCount)   CityCount   Property. Uses GetCityList
  12. 12. Demo Policies The policies defined in the example application Policy Name Matching Rules Call Handlers InterfaceModelPolicy Member Name = GetCustomerNameWithWildcard or GetCustomerList Type Name = InterfaceCustomerModel Logging Handler with Categories = &quot;AuditFile&quot; and &quot;EventLog&quot; Caching Handler with cache duration 20 seconds Exception Handler with Exception Policy = &quot;CustomerModelPolicy” AttributeModelPolicy Method Signature, Name = * and parameter = System.String Type Name = AttributedCustomerModel Logging Handler with Categories = &quot;AuditFile&quot; CacheByTagPolicy Tag Attribute = &quot;ApplyTenSecondCaching&quot; Caching Handler with cache duration 10 seconds
  13. 13. Using Attributes (1) <ul><li>Use an attribute to apply a handler, and optionally set properties, at design-time: </li></ul>[CachingCallHandler(0, 0, 30)] public DataTable GetCustomerList() { … } C# <CachingCallHandler(0, 0, 30)> _ Public Sub GetCustomerList() As DataTable … End Sub Visual Basic .NET
  14. 14. Using Attributes (2) <ul><li>Use Validation attributes ( from the Validation Application Block ) to validate parameters: </li></ul>[ValidationCallHandler] [ValidationCallHandler] public String GetCustomerName( [StringLengthValidator(3, RangeBoundaryType.Inclusive, 5, RangeBoundaryType.Inclusive)] String customerID) C# <ValidationCallHandler()> _ Public Function GetCustomerName( _ <StringLengthValidator(3, RangeBoundaryType.Inclusive, _ 5, RangeBoundaryType.Inclusive)> _ ByVal customerID As String) As String Visual Basic .NET
  15. 15. Using Attributes (3) <ul><li>Prevent configuration of any policies: </li></ul>[ApplyNoPolicies] public DataTable GetCustomerList() … C# < ApplyNoPolicies > _ Public Sub GetCustomerList() As DataTable … Visual Basic .NET <ul><li>Other handler attributes: </li></ul><ul><ul><li>[AuthorizationCallHandler], [ExceptionHandlingCallHandler], [LoggingCallHandler], [PerformanceCounterCallHandler] </li></ul></ul>
  16. 16. Using Attributes Demo Demo
  17. 17. Factory Proxy Handlers Matching Rules Pipeline
  18. 18. Extending the PIAB <ul><li>Create a new Matching Rule </li></ul><ul><ul><li>implement the IMatchingRule interface </li></ul></ul><ul><li>Create a new Handler </li></ul><ul><ul><li>implement the ICallHandler interface </li></ul></ul><ul><li>Create a new Handler Attribute </li></ul><ul><ul><li>derive from HandlerAttribute or Attribute </li></ul></ul>
  19. 19. Inside a Matching Rule <ul><li>public interface IMatchingRule </li></ul><ul><li>{ </li></ul><ul><li>bool Matches(MethodBase member ); </li></ul><ul><li>} </li></ul>// for example… the built-in Tag Attribute Matching Rule uses: public bool Matches(MethodBase member) { foreach (TagAttribute tagAttribute in GetAllAttributes(member, true)) { if (string.Compare(tagAttribute.Tag, tagToMatch, ignoreCase) == 0) return true; } return false; }
  20. 20. Inside a Pipeline Handler <ul><li>public interface ICallHandler </li></ul><ul><li>{ </li></ul><ul><li>IMethodReturn Invoke(IMethodInvocation input , </li></ul><ul><li>GetNextHandlerDelegate getNext ); </li></ul><ul><li>} </li></ul><ul><li>public delegate IMethodReturn InvokeHandlerDelegate(IMethodInvocation input , </li></ul><ul><li>GetNextHandlerDelegate getNext ); </li></ul><ul><li>public delegate InvokeHandlerDelegate GetNextHandlerDelegate(); </li></ul><ul><li>Handlers must implement the Invoke method </li></ul><ul><li>IMethodInvocation contains method invocation or property access information (including parameters) </li></ul>
  21. 21. Handler Process Overview public class ExampleHandler : ICallHandler { public IMethodReturn Invoke(IMethodInvocation input , GetNextHandlerDelegate getNext ) { // Perform any pre-processing tasks required in the custom handler. ... // Invoke next handler that the block should execute. This code gets the current return message that you must pass back to the caller: IMethodReturn msg = getNext()(input, getNext); ... // Perform any post-processing tasks required in the custom handler. // You can modify the return message if required. // Return the message to the previous handler or the client. return msg; }
  22. 22. Messages and Short-Circuiting Each handler invokes the next one, passing the invocation message along the pipeline and the return message back again A handler can instead simply refuse to call the next handler, and can add information (e.g. an exception) to the return message. Note that previous handlers still execute
  23. 23. Short-Circuiting Execution public IMethodReturn Invoke(IMethodInvocation input , GetNextHandlerDelegate getNext ) { GregorianCalendar cal = new GregorianCalendar(); DayOfWeek weekDay = cal.GetDayOfWeek(DateTime.Now); if (weekDay == DayOfWeek.Saturday || weekDay == DayOfWeek.Sunday) { // create an Exception to return and the return message. Exception ex = new Exception(&quot;Available on weekdays only&quot;); IMethodReturn msg = input.CreateExceptionMethodReturn(ex) return msg; } else { return getNext()(input, getNext); // invoke the next handler }
  24. 24. Custom Handler Attributes [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method)] public class CachingCallHandlerAttribute : HandlerAttribute { private TimeSpan expirationTime; public CachingCallHandlerAttribute(int hours , int minutes , int seconds ) { // constructor, save the parameter values expirationTime = new TimeSpan(hours, minutes, seconds); } public override ICallHandler CreateHandler() { // create the appropriate handler instance and return it return new CachingCallHandler(expirationTime); } }
  25. 25. References <ul><li>Book covering Enterprise Library: </li></ul><ul><ul><li>&quot;Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications and Services&quot; (Addison-Wesley ISBN 0-321-33421-3). </li></ul></ul><ul><li>All about Enterprise Library: </li></ul><ul><ul><li> </li></ul></ul><ul><li>Code and Slides for this session: </li></ul><ul><ul><li> </li></ul></ul><ul><li>Contact: [email_address] </li></ul>