InterfaceInterceptor with Registration by Conventation

Jun 24, 2013 at 12:20 PM
Edited Jun 24, 2013 at 1:23 PM
I use registration by Convention to create all objects using unity (No configuration, just attributes). This works fine.
container.RegisterTypes(UnityHelpers.GetTypesWithCustomAttribute<UnityIoCTransientLifetimeAttribute>(AppDomain.CurrentDomain.GetAssemblies()),
                                    WithMappings.FromMatchingInterface,
                                    WithName.Default,
                                    WithLifetime.Transient
                                );
I have also a InterfaceInterceptor with a custom Behaviour which works for a single Interface.
DiagnosisBehaviour d = new DiagnosisBehaviour();

            container.AddNewExtension<Interception>();

            container.RegisterType<IBusinessClass2>(
                new InterceptionBehavior(d),
                new Interceptor(new InterfaceInterceptor()));
It it possible to create a InterfaceInterceptor for all Types found in the RegisterTypes method?

I could only get it to work when I defined each interface for each InterfaceInterceptor .

Thanks in advance for your help
Jun 24, 2013 at 4:44 PM
You should be able to do this:
var container = new UnityContainer();

container.AddNewExtension<Interception>();

container.RegisterTypes(
    UnityHelpers.GetTypesWithCustomAttribute<UnityIoCTransientLifetimeAttribute>
        (AppDomain.CurrentDomain.GetAssemblies()),    
    WithMappings.FromMatchingInterface,
    WithName.Default,
    WithLifetime.Transient
    , getInjectionMembers: t => new InjectionMember[]
    {
        new Interceptor<InterfaceInterceptor>(),
        new InterceptionBehavior<DiagnosisBehavior>()
    });

See Registration by Convention for more info.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jun 24, 2013 at 5:30 PM
Thanks a million, that worked.