PIAB should perform some enhancement to control the CallHandler execution order

Topics: Policy Injection Application Block
May 29, 2008 at 8:21 AM
PIAB give me a good approach to implement AOP. With PIAB, we can extract the crosscutting non-business concern and implement them in variety of CallHandlers, and these CallHandler can be easily applied to appropriate target method in Attribute or Configuraiton fashion.
However, in my own opinion, there is a common and practical requirement current PIAB can not meet, that is to make variety of CallHandlers, which are utimately applied to a particular method, executed in the order we expect.

E.g. We have two CallHandlers: ExceptionCallHandler used to perform exception handling and TransactionCallHandler user for Transaction Enlisting. Now the two are both applied to a particular method. Generall speaking, we expect the ExceptionCallHandler can not executed firstly so that it can handle the exception thrown in TransactionCallHandler.

This is a very common and practical requirment?

As with implementaton, this will be a very easy thing. In fact, in my current project, I implement this by adding a few lines of code. This is my basic implemation mechanism:

When an object, which is essentially a transparent proxy instance, is created by Policy Injection factory methods, such as Create and Wrap. PIAB will create a sepcial objection specific to each MethodInfo of the current type: CallHanderPipeline, which is essetially a CallHandler list. The CallHandler Sequence in the pipeline determine the Runtime execution order. So, what we need do is sorting there Callhandlers in the pipeline.

In order to indicating the Ordinal in the CallHandlerPipe line. I create three base classes:
CallHandlerBase: ICallHandler
{
    public int Ordinal 
    {get;set;}
}

CallHandlerAttributeBase: CallHandlerAttribute
{
    public int Ordinal 
    {get;set;}
}

HandlerDataBase: HandlerData
{
       public int Ordinal 
       {get;set;}
}

So, we can sort the CallHandler in CallHandler pipeline based on the Ordinal property.
May 29, 2008 at 4:31 PM

It is important and they added the functionality in EntLib 4.0. In the documentation under About This Release of Enterprise Library:

"All call handlers in the application block now expose an integer property named Order that you can use to specify the order of the handlers within the policy handler chain. The default value is zero, which means that there is no explicit order specified for that handler in relation to other handlers in the same handler chain. To specify an explicit order, set the Order property for each handler starting from 1. If you specify the same value for the Order property of two handlers in the same policy, the application block will add them to the policy handler chain in the order that it creates them. "

Regards,

Dave

http://www.davidhayden.com/
Microsoft MVP C#

May 30, 2008 at 3:38 AM
Thanks, David.
I think I should begin our study about the new version of Enterprise Libray.