I have an ICallHandler that I want to register with all of my Unity container instances.
For example, take the following handler:
public class ProfilerHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
//start timer
IMethodReturn methodReturn = getNext()(input, getNext);
//stop timer
}
public int Order
{
get; set;
}
}
And the following IoC container constructor:
public class IoCContainer : UnityContainer
{
public IoCContainer()
{
this.RegisterType<IUserService, UserService>(new ContainerControlledLifetimeManager());
this.RegisterType<IRepository<User>, UserRepository>(new ContainerControlledLifetimeManager());
}
}
All I want to do is register this handler with all of these types.
I can do this w/ some pretty verbose code:
public class IoCContainer : UnityContainer
{
public IoCContainer()
{
this.AddNewExtension<Interception>();
this.RegisterType<IUserService, UserService>(new ContainerControlledLifetimeManager()).Configure<Interception>().SetInterceptorFor<IUserService>(new InterfaceInterceptor());
this.RegisterType<IRepository<User>, UserRepository>(new ContainerControlledLifetimeManager()).Configure<Interception>().SetInterceptorFor<IRepository<User>>(new InterfaceInterceptor());
}
}
But not only do I have to write the same interception code on all of my type registrations (imagine if I have 100+ type registrations), but I also must include a HandlerAttribute on every interface (again, not good if I have 100+ interfaces to apply this to).
Is this my only option, or is there a way to do this at the container level to avoid having to apply this to each individual type registration & interface?