How to tell Windsor about adding Interceptor to all registered components that implement IMustBeIntercepted

If I registered several components using Windsor.

IAnimal provides BigAnimal IPerson provides SmellyPerson IWhale provides BlueWhale

etc .. pretty standard component registration

all of the above types implement IMustBeIntercepted, as I can say that the container adds an interceptor to all types that implement IMustBeImplemented, so when Resolve is called, BigAnimal is returned with the interceptor, as defined, as it matches. I know that I can do this for everyone, but its an additional XML configurator or software configuration that I want to avoid.

+5
castle-windsor
source share
2 answers

Just create an interface like this:

public interface IMustBeIntercepted {} 

and an object like this:

 public class InterceptionFacility : AbstractFacility { protected override void Init() { Kernel.ComponentRegistered += new Castle.MicroKernel.ComponentDataDelegate(Kernel_ComponentRegistered); } void Kernel_ComponentRegistered(string key, Castle.MicroKernel.IHandler handler) { if(typeof(IMustBeIntercepted).IsAssignableFrom(handler.ComponentModel.Implementation)) { handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(TestInterceptor))); } } } 

Then register the object in the container using the <facility> . Now all components implementing IMustBeIntercepted will be intercepted by the TestInterceptor interceptor.

+5
source share

Just wrote this baby:

  public static BasedOnDescriptor WithInterceptor(this BasedOnDescriptor reg, string interceptorComponentName) { return reg.Configure(x=> x.Configuration( Child.ForName("interceptors").Eq( Child.ForName("interceptor").Eq( "${" + interceptorComponentName + "}" )))); } 
+2
source share

All Articles