Enterprise Library Policy Injection Block supports Composite Oriented Programming

My last post Composite Oriented Programming spike on Unity Application Block turned out rather lengthy. Yet I forgot to embellish one thing: I don't only use the Unity Application Block to make it work. I also use a couple of methods from the Policy Injection Application Block which is one of the Application Blocks in Enterprise Library.

It's not a lot I use. Actually I used it more as a roadmap in my spike than anything else. Yet I felt that it was better to copy some behavior from the PI block rather than implementing the same things in my spike! I new that the PI block implements AOP type behavior and thus makes use of proxies and the System.Runtime.Remoting namespace. The difference between my code in my COP sample and the code in the PI block is that they create a proxy with one real implementation and I create a proxy with a composite implementation. Thus I could not use it straight of but had to code a bit of remoting myself.

I wrote a simple RealProxy subclass that overrides the .Invoke(IMessage) method. All I do is simply pass my composite parts into the CompositionProxy and map incoming calls to the right parts.

public class CompositionProxy : RealProxy
{
    private readonly Type toProxy;

    public CompositionProxy(Type toProxy, Dictionary<Type, object> traits)
        : base(toProxy)
    {
        this.toProxy = toProxy;
        Traits = traits;
    }

    public Dictionary<Type, object> Traits { get; protected set; }

    public override IMessage Invoke(IMessage msg)
    {
        var callMessage = (IMethodCallMessage) msg;
        Type targetType = callMessage.MethodBase.ReflectedType;


object target; try { target = Traits[targetType]; } catch (KeyNotFoundException knfe) { throw new ApplicationException(string.Format("Something went wrong while trying to call method {0} on {1}", callMessage.MethodName, callMessage.TypeName), knfe); } var invocation = new RemotingMethodInvocation(callMessage, target); object returnValue = callMessage.MethodBase.Invoke(target, invocation.Arguments); var methodReturn = (RemotingMethodReturn) invocation.CreateMethodReturn(returnValue, invocation.Arguments); return methodReturn.ToMethodReturnMessage(); }
}
Just wanted to make this part of my spike more clear! ;~)
Cheers,

/Magnus

Technorati Tags: ,,,

posted @ Wednesday, August 27, 2008 6:15 PM

Print

Comments on this entry:

# re: Enterprise Library Policy Injection Block supports Composite Oriented Programming

Left by software developer at 8/17/2009 8:22 PM
Gravatar
Quite inspiring,

it works like a charm,

Thanks for writing, most people don't bother.

Your comment:



 (will not be displayed)


 
 
 
Please add 8 and 4 and type the answer here:
 

Live Comment Preview: