如何在Castle 3中恢复为旧的CollectionResolver行为? [英] How to revert to old CollectionResolver behavior in Castle 3?

查看:76
本文介绍了如何在Castle 3中恢复为旧的CollectionResolver行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CollectionResolver(更具体地说是ListResolver)来支持我们要注入接口的多个实现的方案。 CollectionResolver的行为已受到Castle 3中对ResolveAll()的更改的影响。以前,ResolveAll()仅返回那些可以解析的实现,而静默忽略任何无法解析的注册实现。现在,如果无法解析任何已注册的实现,则ResolveAll()失败。

I'm using the CollectionResolver (more specifically the ListResolver) to support scenarios where we want to inject multiple implementations of an interface. The CollectionResolver's behavior has been impacted by the changes to ResolveAll() in Castle 3. Previously, ResolveAll() would only return those implementations that could be resolved, and silently ignore any registered implementations that could not be resolved. Now, ResolveAll() fails if ANY one of the registered implementations cannot be resolved.

总的来说,我认为这是一个不错的更改,因为它可以确保我们实际上获得了我们认为的所有实现。但是,在一些情况下,我实际上是在使用旧行为以使自己受益。我有一个基础组件(IBar),由一个普通的安装程序注册,该组件依赖于IFoo的集合(由集合解析器实现)。在通用安装程序中,我还注册了IFoo的一些实现,但是这些实现取决于可能注册或可能未注册的服务。这个想法是,客户端可以通过注册从属服务来激活 IFoo的这种实现。以前,如果客户端未注册特定IFoo的依赖项,则该IFoo的实现将不会被注入到IBar中(其他可解决的实现会被注入)。现在,在这种情况下,IBar的解析失败了,因为无法解析(以前是可选的)IFoo。

In general, I think this is a good change, as it ensures that we are actually getting all the implementations we think we are. However, I have a couple scenarios where I was actually using the old behavior to my benefit. I have an infrastructural component (IBar), registered by a common installer, that depends on a collection of IFoo (to be fulfilled by the collection resolver). In the common installer, I also register some implementations of IFoo, but these implementations depend on services that may or may not be registered. The idea is that a client can "activate" such an implementation of IFoo by registering the dependent service. Previously, if the client did not register the dependency of a particular IFoo, that implementation of IFoo simply wouldn't be injected into IBar (and other resolvable implementations would be). Now the resolution of IBar just fails in this scenario because the (previously optional) IFoo couldn't be resolved.

有什么方法可以轻松地恢复旧的行为吗?理想情况下,我想默认使用新的行为,但是将特定服务恢复为旧的行为(在这种情况下为IFoo)。我尝试创建一个IHandlersFilter实现来完成此操作,但是我发现不可能过滤掉所有候选处理程序(从SelectHandlers返回一个空数组)-如果我尝试这样做,温莎将忽略该过滤器并尝试执行无论如何都要解决所有候选处理程序。

Is there any way to easily restore the old behavior? Ideally, I'd like to use the new behavior by default, but revert to the old behavior for a particular service (IFoo in this case). I've tried creating a IHandlersFilter implementation to accomplish this, but I've discovered that it is not possible to filter out ALL candidate handlers (return an empty array from SelectHandlers) -- if I attempt to, Windsor ignores the filter and tries to resolve all the candidate handlers anyway.

有什么想法吗?如果您认为我的做法不对,我也欢迎采用完全不同的方法来实现目标。

Any ideas? I'm also open to a totally different approach of achieving my goal, if you think I'm going about this the wrong way.

推荐答案

您可以在Windsor中使用名为 IHandlersFilter 的新扩展点来精细控制 ResolveAll 行为。有关示例,请参见此博客文章

You can use a new extension point in Windsor, called IHandlersFilter to finely control ResolveAll behaviour. See this blogpost for an example.

这篇关于如何在Castle 3中恢复为旧的CollectionResolver行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆