Appending InjectionMembers AFTER RegisterType has happened

Jan 2 at 4:48 PM
Hi,

I am using Unity 3.0

I have this code:
UnityConfig.Container.RegisterType<IMockProvider, MockProvider>(new PerThreadLifetimeManager(), new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<PerfCounterInterceptor>());
Which I believe is just the same as these two statements put together:
UnityConfig.Container.RegisterType<IMockProvider, MockProvider>(new PerThreadLifetimeManager());

UnityConfig.Container.RegisterType<IMockProvider, MockProvider>(new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<PerfCounterInterceptor>());

What I currently have is a long list of statements like
UnityConfig.Container.RegisterType<IMockProvider, MockProvider>(new PerThreadLifetimeManager());

After this long list, I wish to add the same InterceptionBehavior to ALL Registered types, possibly based on certain configuration values.
So, how do I "add the same InterceptionBehavior to ALL Registered types" in some kind of a for loop? I have looked at Container.Registrations, tried using IBuilderContext but to no avail.

Thanks,
Karthik
Editor
Jan 3 at 5:27 AM
You can re-register the types after the initial registration to add the interception. Something like this:
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();

container.RegisterType<IMockProvider, MockProvider>(new PerThreadLifetimeManager());
container.RegisterType<IAnotherMockProvider, AnotherMockProvider>(new PerThreadLifetimeManager());

foreach (var registration in container.Registrations)
{
    // ShouldBeConfigured could be a method you use to determine if you want to intercept
    if (ShouldBeConfigured(registration.RegisteredType, registration.MappedToType))
    {
        container.RegisterType(registration.RegisteredType, registration.MappedToType, registration.Name,
            new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<PerfCounterInterceptor>());
    }
}

Alternately, you might consider using registration by convention to apply the injection members:
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();

container.RegisterTypes(
    GetMyInterceptionTypes(), // method to get the types you want to intercept
    WithMappings.FromMatchingInterface, // Add "I" in front of type name to create "From" mapping
    WithName.Default,
    WithLifetime.PerThread,
    t => new InjectionMember[] { 
            new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<PerfCounterInterceptor>() }
    );

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to