5

If I registered several components with Windsor.

IAnimal provides BigAnimal IPerson provides SmellyPerson IWhale provides BlueWhale

etc.. pretty standard component registeration

all the above types implement IMustBeIntercepted, how do I tell the container add an interceptor to all types that implement IMustBeImplemented so that when Resolve is called it is returned a BigAnimal with an interceptor as defined since it matches. I know I can do this for each one but its extra XML config or programatic config which I want to avoid

2 Answers2

5

Simply create an interface like this:

public interface IMustBeIntercepted {}

and a facility 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 facility to the container using the <facility> tag. Now all components that implements IMustBeIntercepted will be intercepted by the interceptor TestInterceptor.

Mikael Sundberg
  • 4,607
  • 4
  • 32
  • 36
2

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 + "}"
                ))));
    }
George Mauer
  • 117,483
  • 131
  • 382
  • 612