对于特定控制器使 Windsor 实例化不同的类
我使用 S#arp 架构,它使用温莎城堡进行 IoC。我现在有了一个新控制器,与项目中的所有其他控制器不同,它需要相同接口的不同实现。即所有控制器都使用 ProductsRepository: IProductsRepository 作为实现,但新控制器必须使用 SpecificProductsRepository。
我如何配置它以自动识别和管理它?要么采用纯粹的 Windsor 方式,要么使用 ASP.NET MVC 帮助(例如在我的自定义控制器工厂中)。
好的,看来我需要子容器。仍在寻找。
I use S#arp Architecture which uses Windsor Castle for IoC. I got a new controller now that, unlike all other controllers in the project, need a different implementation of the same interfaces. I.e. all controllers use ProductsRepository: IProductsRepository as implementation, but the new one has to use SpecificProductsRepository.
How do I configure it to recognize and manage this automatically? Either in pure Windsor way, or with ASP.NET MVC help (e.g. in my custom controllers factory).
OK looks like I need subcontainers. Still searching.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种更简单的方法是使用 Windsor 的服务覆盖。
例如,像这样注册您的存储库:
这将确保默认实现是
ProductsRepository
。现在,对于您的特定控制器,添加一个服务覆盖,如下所示:您可以阅读文档 这里。
编辑:如果您想使用
AllTypes
注册存储库,您可以调整注册密钥,例如:其中
GetKey
例如可能类似于:An easier and much simpler way would be to use Windsor's service overrides.
E.g. register your repos like so:
which will ensure that the default implementation is
ProductsRepository
. Now, for your specific controller, add a service override like so:You can read the docs here.
Edit: If you want to register your repositories with
AllTypes
, you can adjust the registration key e.g. like so:where
GetKey
e.g. could be something like:好吧,这些天我倾向于回答自己的问题……所以这里是为那些需要它的人提供的。
控制器工厂根据名称选择适当的容器。最大的挑战是在请求结束时调用适当容器的Release(controller),即记住哪个容器用于实例化控制器。但我认为这可以通过多种方式解决 - 记住在特定于线程的(在 HttpContext 中),记住在 BaseController 属性中,记住在内部字典中,等等。
OK, these days I tend to answer my own questions... so here it is for those who need it.
The controller factory chooses appropriate container based on name. The biggest challenge there is to call appropriate container's Release(controller) at the end of request, i.e. remember which container was used to instantiate controller. But this can be solved in several ways I suppose - remember in thread-specific (in HttpContext), remember in BaseController property, remember in internal dictionary, etc.