Adding a Policy Injection extension to Unity - Part II

Yesterday I blogged about on how to add a Policy Injection extension to Unity. To do this, I had to modify some of the code of both Unity and ObjectBuilder2 to get all the references I needed. Today Francois Tanguay pointed me to the new drop of Unity released on Monday.

A couple of improvements have been made in this drop and one of these is the availbility of the original build key. This is the key before any mapping has occured and just what I need to create my extension without making modifications to Unity and ObjectBuilder2.

The code of the extension is now:

    1   public class PolicyInjectionStrategy : BuilderStrategy

    2   {

    3     public override void PreBuildUp( IBuilderContext context )

    4     {

    5       ITypeBasedBuildKey buildKeyTo  = context.BuildKey as ITypeBasedBuildKey;

    6       ITypeBasedBuildKey buildKeyFrom = context.OriginalBuildKey as ITypeBasedBuildKey;

    7 

    8       if( buildKeyFrom != null && buildKeyFrom.Type != null && buildKeyFrom.Type.IsInterface )

    9       {

   10         context.Existing = Wrap( context.Existing, buildKeyFrom.Type );

   11       }

   12       else if( buildKeyTo != null && buildKeyTo.Type != null && buildKeyTo.Type.IsMarshalByRef )

   13       {

   14         context.Existing = Wrap( context.Existing, buildKeyTo.Type );

   15       }

   16 

   17       base.PreBuildUp( context );

   18     }

   19 

   20     private static object Wrap(object existing, Type t)

   21     {

   22       if (existing != null)

   23       {

   24         existing = (new PolicyInjectorFactory()).Create().Wrap(existing, t);

   25       }

   26       return existing;

   27     }

   28   }

The BuilderStrategy base class now also has PreBuildUp and PostBuildUp methods to override. The PreBuildUp methods are excuted through the chain in a forward direction, just like the former BuildUp method. The PostBuildUp methods are called in reverse order when all PreBuildUp methods have been executed.

kick it on DotNetKicks.com AddThis Social Bookmark Button

Comments

# Adding a Policy Injection extension to Unity - Part II

Tuesday, February 19, 2008 5:33 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Adding a Policy Injection extension to Unity - Part II

Monday, July 28, 2008 8:40 PM by Graham

Any chance of updating this with the new Unity block?

Some of the code you've used doesn't work (e.g. Initialize doesn't accept an ExtensionContext)

Thanks

# re: Adding a Policy Injection extension to Unity - Part II

Tuesday, July 29, 2008 10:49 AM by Martijn Veken

I've created a blogpost with updated code here.

# re: Adding a Policy Injection extension to Unity - Part II

Wednesday, August 06, 2008 4:06 AM by Artech

namespace Artech.PolicyInjectionIntegratedInUnity

{

   public class PolicyInjectionStrategy : EnterpriseLibraryBuilderStrategy

   {

       public override void PreBuildUp(IBuilderContext context)

       {

           base.PreBuildUp(context);

           if (context.Policies.Get<IPolicyInjectionPolicy>(context.BuildKey) == null)

           {

               context.Policies.Set<IPolicyInjectionPolicy>(new PolicyInjectionPolicy(true), context.BuildKey);

           }

       }

       public override void PostBuildUp(IBuilderContext context)

       {

           base.PostBuildUp(context);

           IPolicyInjectionPolicy policy = context.Policies.Get<IPolicyInjectionPolicy>(context.BuildKey);

           if ((policy != null) && policy.ApplyPolicies)

           {

               policy.SetPolicyConfigurationSource(EnterpriseLibraryBuilderStrategy.GetConfigurationSource(context));

               context.Existing = policy.ApplyProxy(context.Existing, BuildKey.GetType(context.OriginalBuildKey));

           }

       }

   }

}

www.cnblogs.com/.../1261637.html

# re: Adding a Policy Injection extension to Unity - Part II

Wednesday, August 06, 2008 4:08 AM by Artech

www.cnblogs.com/.../1261637.html

you can use Microsoft.Practices.EnterpriseLibrary.PolicyInjection.ObjectBuilder.PolicyInjectionPolicy

Leave a Comment

(required) 
(required) 
(optional)
(required)