Enterprise library session slides and samples
I believe we did a good job in explaining real world use cases and usages in “just” 3 hours.
The slides and samples can be downloaded from Enterprise Library – Slides and Examples
The one thing I didn’t have time (and strength) to cover in the session is example of policy injection application block very simple examples. The zip file contain that example and I would just make a couple of comments here which would make understanding of those examples an easier task
Both examples cover the use case of having the page which would upon the button click retrieve current time from SessionClock helper class. Both examples would implement the PIAB caching and logging handlers.
covers the use case when our object which calls we would like to intercept inherits from MarshalByRefObj so there are no attributes decoration.
Session clock looks really simple
To define matching rule: (in my example I used the Member one)
And then in property window of that Member name rule pick Matches and Add the name of method which retrieves the time GetCurrentTime (click on image to load more readable screen shoot)
With this defining of matching rules we defined that the “targets” of the policy we defines are all the types which have GetCurrentTime() method in it.
When we defined the appliance scope of policy, we have to define what that policy would contain. For this example, I pick the logging and caching handlers.
To define handlers
And to define then the handler attributes use the property window (in this example I set caching to 10 seconds)
When we set the policy scope and implementation, all we need is to create a proxy object by using factory method to wrap up our type
And that’s it!
If we run the site and click the button we would see that the CachedTime text box would be caching output on 10 seconds and if we would check the event log we would see the method access event log entry
Timestamp: 25.7.2007 10:52:19 AM
Message: —- PIAB LOG START —
Application Domain: 6079b8a-1-128298341641858405
Process Id: 6832
Process Name: C:Program FilesMicrosoft Visual Studio 8Common7IDEWebDev.WebServer2.exe
Win32 Thread Id: 2812
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
covers the use case of class implementing the interface and not inheriting the MarshalByRefObject, so the interception occurs on interface member implementation. This example is instead of using Matching rules using the attribute declaration approach. It also examples tagging concept.
In this example, implementation starts with interface definition (which is IMHO a great architectural and TDD practice, off course used in reasonable means)
Decorating GetCurrentTime() method interface contract with CachingCallHandlerAttribute makes the same effect like declaring it in a policy handler configuration. Second attribute is Tag(“Some_string”) which allows us to tag us desired number of type members for which we would like to have uniform policy behavior.
An example use case could be that all the methods (regardless of type, namespace etc) which are reading sensitive data (credit card etc) could be tagged with the same tag. That would allow us later to set Tag matching rule and define/alter on single place desired behavior of all those methods.
To define a tag matching rule you should do something like this:
In my example, I’ve defined a policy that all the access to members compliant with tag matching rule should be logged
That could be interpreted as:”For all the things tagged as SECURE do this logging”. Very, very powerful concept!
The button click implementation is slightly different because now we have to inform policy injection factory method about the interface been intercepted (That is why we use Create overload with two generic parameters)
The behavior of the application is the same. Output is cached on 10 second basis and accesses are logged in Application event log
That would be it! I hope you all enjoyed the presentation and found a lot of useful things there. 🙂