Enterprise library session slides and samples

The presentation  Antonin Jelinek (webmaster of FC Bohemians and tech lead in my company) and me did in Microsoft on July 17th  (Enterprise Library a její použití v praxi) bring us a lot of fun.

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.

MarshalByRefEx

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

   1: public class SessionClock : MarshalByRefObject
   2: {
   3:     /// <summary>        
   4:     /// /// This is a method which would return current time so the presenter could        
   5:     /// /// know how much time he has more 🙂        
   6:     /// /// </summary>        
   7:     /// /// <returns>Current time</returns>        
   8:     public DateTime GetCurrentTime()
   9:     {
  10:         return DateTime.Now;
  11:     }
  12: }
  13:  

Matching rule

To define matching rule: (in my example I used the Member one)

PIAB_MatchRuleDef

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)

PIAB_MatchRule

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.

Handlers

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

PIAB_HandlersDef 

And to define then the handler attributes use the property window (in this example I set caching to 10 seconds)

PIAB_Handlers

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

   1: protected void Button1_Click(object sender, EventArgs e)
   2: {
   3:     SessionClock clock = PolicyInjection.Create<SessionClock>();
   4:     CurrentTime.Text = string.Format("System  clock time: {0}", DateTime.Now);
   5:     CachedTime.Text = string.Format("Session clock time: {0}", clock.GetCurrentTime());
   6: }

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 —
Category: General
Priority: -1
EventId: 0
Severity: Information
Title:Call Logging
Machine: NIKOLA
Application Domain: 6079b8a-1-128298341641858405
Process Id: 6832
Process Name: C:Program FilesMicrosoft Visual Studio 8Common7IDEWebDev.WebServer2.exe
Win32 Thread Id: 2812
Thread Name:
Extended Properties:

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

InterfaceAttributes

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)

Interface definition

   1: using System;
   2: using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
   3: using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;
   4: using PIAB=Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;
   5:  
   6: namespace InterfaceAttributes
   7: {
   8:     public interface ISessionClock
   9:     {
  10:         /// <summary>        
  11:         /// /// This is a method which would return current time so the presenter could        
  12:         /// /// know how much time he has more 🙂        
  13:         /// /// </summary>        
  14:         /// /// <returns>Current time</returns>        
  15:         [Tag("SECURE")]
  16:         [CachingCallHandler(0, 0, 10)]
  17:         DateTime GetCurrentTime();
  18:     }
  19: }

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.

Tagging

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:

PIAB_Tag

 

In my example, I’ve defined a policy that all the access to members compliant with tag matching rule should be logged

PIAB_TagHandler

That could be interpreted as:”For all the things tagged as SECURE do this logging”. Very, very powerful concept!

Factory method

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)

   1: protected void Button1_Click(object sender, EventArgs e)
   2: {
   3:     ISessionClock clock = PolicyInjection.Create<SessionClock, ISessionClock>();
   4:     CurrentTime.Text = string.Format("System  clock time: {0}", DateTime.Now);
   5:     CachedTime.Text = string.Format("Session clock time: {0}", clock.GetCurrentTime());
   6: }

 

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. 🙂

Advertisements

Posted on July 25, 2007, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: