How do I tell Windsor to add an Interceptor to all components registered that implement IMustBeIntercepted

孤人 提交于 2019-11-30 15:16:13

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.

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 + "}"
                ))));
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!