我如何告诉Windsor向所有实现IMustBeIntercepted的已注册组件添加拦截器 [英] How do I tell Windsor to add an Interceptor to all components registered that implement IMustBeIntercepted
问题描述
如果我在温莎注册了多个组件。
If I registered several components with Windsor.
IAnimal提供BigAnimal
IPerson提供SmellyPerson
IWhale提供BlueWhale
IAnimal provides BigAnimal IPerson provides SmellyPerson IWhale provides BlueWhale
等。相当标准的组件注册
etc.. pretty standard component registeration
以上所有类型均实现IMustBeIntercepted,如何告诉容器向所有类型添加拦截器,实现IMustBeImplemented,以便在调用Resolve时返回一个BigAnimal,该BigAnimal具有匹配的定义的拦截器。我知道我可以为每个人做到这一点,但是我要避免使用它的额外XML配置或程序化配置
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
推荐答案
只需创建一个这样的界面:
Simply create an interface like this:
public interface IMustBeIntercepted {}
以及类似的设施:
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)));
}
}
}
然后将设施注册到使用< facility>
标签的容器。现在,所有实现 IMustBeIntercepted
的组件都将被拦截器 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
.
这篇关于我如何告诉Windsor向所有实现IMustBeIntercepted的已注册组件添加拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!