Your SlideShare is downloading. ×
Policy Injection in ASP.NET using Enterprise Library 3.0
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Policy Injection in ASP.NET using Enterprise Library 3.0

6,072
views

Published on

Terminology and Buzzwords …

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

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,072
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
142
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Policy Injection in ASP.NET using Enterprise Library 3.0 Alex Homer [email_address] http://www.daveandal.net
  • 2. Agenda
    • 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
  • 3. Terminology and Buzzwords
    • Aspect Oriented Programming (AOP)
    • Policy Injection
    • Remoting Proxies
    • Intercepting Proxies
    • Method Interception
    • The overall aim is better management of Crosscutting Concerns
  • 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. What is Policy Injection?
    • “ ... techniques for creating policies that help to manage crosscutting concerns, and applying these policies across all relevant objects and applications ...”
    • Using the Policy Injection Block you can:
      • Create handlers for each crosscutting concern
      • Define the conditions where they will apply
      • Define the behavior in specific circumstances
      • Adjust runtime behavior through configuration
      • Manage runtime behavior using Group Policy
  • 6. The Handlers Pipeline
      • Objects must be “interceptable”
      • Must use factory Create or Wrap method
      • Each handler can execute code in the pre- and/or post-processing stages
      • Handlers can short-circuit (abort) execution
  • 7. Interceptable Object Types (1)
    • C#:
    • object = PolicyInjection.Create< type >( ctor_params )
    • object = PolicyInjection.Create< type >(IConfigurationSource, ctor_params )
    • object = PolicyInjection.Wrap< type >( existing_object )
    • object = PolicyInjection.Wrap< type >(IConfigurationSource, existing_object )
      • Types that inherit MarshalByRefObject:
    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. Interceptable Object Types (2)
    • interface = PolicyInjection.Create< type , interface >( ctor_params )
    • interface = PolicyInjection.Create< type , interface >
    • (IConfigurationSource, ctor_params )
    • interface = PolicyInjection.Wrap< interface >( existing_object )
    • interface = PolicyInjection.Wrap< interface >
    • (IConfigurationSource, existing_object )
      • Types that implement a known interface:
    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. Configuring Policies
    • Enterprise Library Configuration Console
    • Visual Studio 2005 Configuration Editor
    • Manually ( recommended for masochists only )
    • Add the PIAB to your app configuration
    • Create the required Policies
      • Each policy has one or more matching rules
        • Must all evaluate to TRUE to select a target
      • Each policy has one or more handlers
        • Processed in the order they occur in configuration
  • 10. Configuring Policies Demo Demo
  • 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. 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. Using Attributes (1)
    • Use an attribute to apply a handler, and optionally set properties, at design-time:
    [CachingCallHandler(0, 0, 30)] public DataTable GetCustomerList() { … } C# <CachingCallHandler(0, 0, 30)> _ Public Sub GetCustomerList() As DataTable … End Sub Visual Basic .NET
  • 14. Using Attributes (2)
    • Use Validation attributes ( from the Validation Application Block ) to validate parameters:
    [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. Using Attributes (3)
    • Prevent configuration of any policies:
    [ApplyNoPolicies] public DataTable GetCustomerList() … C# < ApplyNoPolicies > _ Public Sub GetCustomerList() As DataTable … Visual Basic .NET
    • Other handler attributes:
      • [AuthorizationCallHandler], [ExceptionHandlingCallHandler], [LoggingCallHandler], [PerformanceCounterCallHandler]
  • 16. Using Attributes Demo Demo
  • 17. Factory Proxy Handlers Matching Rules Pipeline
  • 18. Extending the PIAB
    • Create a new Matching Rule
      • implement the IMatchingRule interface
    • Create a new Handler
      • implement the ICallHandler interface
    • Create a new Handler Attribute
      • derive from HandlerAttribute or Attribute
  • 19. Inside a Matching Rule
    • public interface IMatchingRule
    • {
    • bool Matches(MethodBase member );
    • }
    // 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. Inside a Pipeline Handler
    • public interface ICallHandler
    • {
    • IMethodReturn Invoke(IMethodInvocation input ,
    • GetNextHandlerDelegate getNext );
    • }
    • public delegate IMethodReturn InvokeHandlerDelegate(IMethodInvocation input ,
    • GetNextHandlerDelegate getNext );
    • public delegate InvokeHandlerDelegate GetNextHandlerDelegate();
    • Handlers must implement the Invoke method
    • IMethodInvocation contains method invocation or property access information (including parameters)
  • 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. 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. 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. 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. References
    • Book covering Enterprise Library:
      • &quot;Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications and Services&quot; (Addison-Wesley ISBN 0-321-33421-3).
    • All about Enterprise Library:
      • http://www.codeplex.com/entlib/
    • Code and Slides for this session:
      • http://www.daveandal.net/download/
    • Contact: [email_address]
    http://www.codeplex.com