简体   繁体   English

针对不同依赖关系的不同Castle拦截器实例

[英]Different Castle interceptor instances for different dependencies

I have an Castle Windsor interceptor that I want to use for two different interfaces (just call them IOne and ITwo). 我有一个Castle Windsor拦截器,我想用于两个不同的接口(只需将它们称为IOne和ITwo)。 Similar to this post http://thatextramile.be/blog/2009/07/protecting-your-application-from-remote-problems/ I want the interceptor to be a singleton with respect to the same interface, but be a different instance for each interface. 与此文章类似http://thatextramile.be/blog/2009/07/protecting-your-application-from-remote-problems/我希望拦截器相对于同一个接口是一个单例,但是是一个不同的实例对于每个接口。 Obviously registering the interceptor as singleton causes the same instance to be reused across all instances of IOne and ITwo. 显然,将拦截器注册为singleton会导致在IOne和ITwo的所有实例中重用相同的实例。 I need the interceptors to behave this way because I need them to preserve state for all calls through that interface, but they need to be distinct from each other. 我需要拦截器以这种方式运行,因为我需要它们通过该接口保留所有调用的状态,但它们需要彼此不同。

I've come across lots of possible ways to approach this, but since I'm not an expert on Windsor, what is the preferred way to handle this situation? 我遇到过很多可能的方法,但由于我不是温莎的专家,处理这种情况的首选方法是什么?

Documentation says it's recommended to use the interceptors with transient lifecycle. 文档说,建议使用具有瞬态生命周期的拦截器。

Make interceptors transient 使拦截器短暂

It is strongly advised that you always make your interceptors transient. 强烈建议您始终使您的拦截器短暂。 Since interceptors can intercept multiple components with various lifestyles it's best if their own lifespan is no longer than the component they intercept. 由于拦截器可以拦截具有不同生活方式的多个组件,因此最好是他们自己的生命周期不超过他们拦截的组件。 So unless you have a very good reason not to, always make them transient. 因此,除非你有充分的理由不这样做,否则总是让它们变得短暂。

I suggest refactoring the shared data interaction to another interface then use that as singleton. 我建议将共享数据交互重构为另一个接口,然后将其用作单例。

An alternative approach is to make the interceptor generic. 另一种方法是使拦截器具有通用性。 Then you can specify the interceptor with the generic argument of your interface. 然后,您可以使用接口的泛型参数指定拦截器。

public class MyInterceptor<T> : IInterceptor
{
    ...
}

Then register like this 然后像这样注册

container.Register(Component.For<IOne>()
                            .ImplementedBy<OneImplementation>()
                            .Interceptors<MyInterceptor<IOne>>());
container.Register(Component.For<ITwo>()
                            .ImplementedBy<TwoImplementation>()
                            .Interceptors<MyInterceptor<ITwo>>());

.Net will recognize MyInterceptor<IOne> and MyInterceptor<ITwo> as separate classes, and so Windsor will create different singleton instances for them. .Net将MyInterceptor<IOne>MyInterceptor<ITwo>识别为单独的类,因此Windsor将为它们创建不同的单例实例。

Of course, this only works if you're making the interceptor. 当然,这只有在你制作拦截器时才有效。 If it's in some library that you don't have access to, then your out of luck with this approach. 如果它在某些你无法访问的库中,那么你对这种方法感到不快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM