I have an ICallHandler that I want to register in all instances of the Unity container.
For example, take the following handler:
public class ProfilerHandler : ICallHandler { public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) {
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 with some pretty detailed 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 I not only have to write the same interception code in all my type registers (imagine if I have more than 100 types registered), but I also need to enable HandlerAttribute on each interface (again, not bad if I have more 100 interfaces to apply this).
Is this my only option or is there a way to do this at the container level to avoid having to apply it to each individual registration and type interface?
source share