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
Ms Motilal Padampat Sugar Mills vs. State of Uttar Pradesh & Ors. - A Milesto...
Policy Injection in ASP.NET using Enterprise Library 3.0
1. Policy Injection in ASP.NET using Enterprise Library 3.0 Alex Homer [email_address] http://www.daveandal.net
2.
3.
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
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 = "AuditFile" and "EventLog" Caching Handler with cache duration 20 seconds Exception Handler with Exception Policy = "CustomerModelPolicy” AttributeModelPolicy Method Signature, Name = * and parameter = System.String Type Name = AttributedCustomerModel Logging Handler with Categories = "AuditFile" CacheByTagPolicy Tag Attribute = "ApplyTenSecondCaching" Caching Handler with cache duration 10 seconds
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("Available on weekdays only"); 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); } }