如何覆盖在温莎城堡中注册的组件? [英] How can I override a component registered in Castle Windsor?
问题描述
我只是从温莎开始,所以请保持柔和:)我有一种情况,我希望能够覆盖/替换放置在温莎容器中的组件。继续阅读...
I'm just starting with Windsor, so please be gentle :) I have a scenario where I want to be able to override/replace components placed inside a windsor container. Read on ...
在我的产品代码中,我希望能够注册一个实现基类的组件,并使用一个容器来解析实现者。到目前为止,使用
In my prod code, I want to be able to register a component which implements a base class, and use a container to resolve the implementer. So far, using
container.Register(Component.For<LoggerBase>().ImplementedBy<DebugLogger>());
和
container.Resolve<LoggerBase>();
在我的测试中,我想添加一个存根/模拟实现来覆盖 DebugLogger实现,这样当我的prod代码调用 container.Resolve< LoggerBase>();
时,它将获得重写的实现。
In my tests, I'd like to add a stub/mock implementation to override the "DebugLogger" implementation so that when my prod code calls container.Resolve<LoggerBase>();
it gets the overridden implementation.
任何指针都将受到欢迎!
Any pointers would be welcome!
推荐答案
与其沿该路径走,不如针对不同的场景使用不同的容器实例。每个实例的配置可以不同。这就是DI容器的全部内容。
Instead of chasing down that path, you should rather use different container instances for different scenarios. Each instance could be configured differently. That's the whole point of a DI Container.
但是,通常 DI容器不应用于单元测试。
这篇关于如何覆盖在温莎城堡中注册的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!