Changing Policies in the pipeline won't work?

May 20, 2008 at 1:44 PM
Hi

Kind of a OB2 question... but it is inside an extension to Unity, so maybe I'm following the wrong approach, j

I want to write an extension to unity that registers a mapping when a mapping was requested for an interface, and it doesn't exist.

Something which
UnityContainer container = new UnityContainer();
container.AddNewExtension<MyExtension>();
IMyCalculator calculator = container.Resolve<IMyCalculator>();

which would resolve to MyDummyCalculator, by somehow inspecting my types. (particular requisite which is not important for this discussion... :))
i thought on doing this in the OB2 pipeline, with a strategy running on the TypeMapping stage (2nd stage), as this:

public override void PreBuildUp(IBuilderContext context)
{
    IBuildKey originalBuildKey =
        context.OriginalBuildKey as IBuildKey;

        //1) - Registering the policy for future calls
    IBuildKeyMappingPolicy policy = context.Policies.Get<IBuildKeyMappingPolicy>(originalBuildKey);
    if (policy == null && originalBuildKey.Type.IsInterface)
    {

        Type myDestinationType = null; // obtain the type somehow and validate it

 

        //2) - Registering the policy for future calls
        IBuildKeyMappingPolicy buildKeyMappingPolicy;
        if (originalBuildKey.Type.IsGenericType)
            context.Policies.Set<IBuildKeyMappingPolicy>(
                    new GenericTypeBuildKeyMappingPolicy(myDestinationType),
                    originalBuildKey.Type
                );

        else
            context.Policies.Set<IBuildKeyMappingPolicy>(
                    new BuildKeyMappingPolicy(myDestinationType),
                    originalBuildKey.Type
                );

        //3) - Registering the mapping for this call
        context.BuildKey = originalBuildKey.ReplaceType(defaultMapping.ToType);

 

    }


My problem is that step 2) does not persist the policy on the policy list. I want my next resolve on the container to have a policy on step 1), and bypass steps 2 and 3.

Any thoughts? Wrong approach? Can't the policies list be changed on the strategy pipeline? Wrong stage?
txs

hugo
http://

May 20, 2008 at 3:55 PM
Answer to my own question.  I found the problem...
First of all, i have to use PersistentPolicies. It looks like Policies is only for the current BuilderContext.
Another mistake i was making was the way i was adding the policy. I was giving the wrong key. Should use originalBuildKey instead of originalBuildKey.Type. I was assuming that it was the same, but the key should be a IBuildKey and not a Type.

here's the code for future reference:

public override void PreBuildUp(IBuilderContext context)
{
    IBuildKey originalBuildKey =
        context.OriginalBuildKey as IBuildKey;

//1) - Obtaining the policy
    IBuildKeyMappingPolicy policy = context.Policies.Get<IBuildKeyMappingPolicy>(originalBuildKey);
    if (policy == null && originalBuildKey.Type.IsInterface)
    {

Type myDestinationType = null; // obtain the type somehow and validate it

//            2) - Registering the policy for future calls
            if (originalBuildKey.Type.IsGenericType)
                context.PersistentPolicies.Set<IBuildKeyMappingPolicy>(
                            new GenericTypeBuildKeyMappingPolicy(destinationType),
                            originalBuildKey
                            );

            else
                context.PersistentPolicies.Set<IBuildKeyMappingPolicy>(
                            new BuildKeyMappingPolicy(destinationType),
                            originalBuildKey
                            );

            3) - Registering the mapping for this call
            context.BuildKey = originalBuildKey.ReplaceType(destinationType);

       

    }

    base.PreBuildUp(context);
}