我正在尝试从一组基本注册中分支多个子容器,以便于进行不同的配置设置。
我认为基于Mark Seemanns关于子容器如何工作的回复 ,我可以使用子容器来覆盖基本注册中的特定组件。 然而,我似乎没有按照Seemann的说法工作。
根据马克,这应该工作:
[TestMethod] public void OverrideRegistrationInParentContainer() { //IBusinessComponent depends on IBasicComponent var parentContainer = new WindsorContainer(); parentContainer.Register(Component.For<IBasicComponent>().ImplementedBy<BasicComponent>()); //Returns 42 parentContainer.Register(Component.For<IBusinessComponent>().ImplementedBy<RealBusinessComponent>()); //Returns the result of IBasicComponent var childContainer = new WindsorContainer(); childContainer.Register(Component.For<IBasicComponent>().ImplementedBy<BasicComponent2>()); //Returns 40 parentContainer.AddChildContainer(childContainer); var service = childContainer.Resolve<IBusinessComponent>(); Assert.AreEqual(40, service.GetBusinessValue()); //This fails with the actual value being 42 }但是,所有依赖关系显然都是从父级解析的。
如果我从parentContainer中删除IBasicComponent注册,我甚至无法解决由于缺少注册而导致的依赖关系。
任何人都可以解释你如何使容器像Seemann声称的那样表现,或者WindsorContainer真的无法以优雅的方式处理这种类型的配置?
I'm am trying to branch a number of child containers from a basic set of registrations in order to facilitate different configuration settings.
I thought based on Mark Seemanns reply on how child containers work that I could use child containers to override specific components in a base registration. However I does not seem to work as Seemann claims.
According to Mark this should work:
[TestMethod] public void OverrideRegistrationInParentContainer() { //IBusinessComponent depends on IBasicComponent var parentContainer = new WindsorContainer(); parentContainer.Register(Component.For<IBasicComponent>().ImplementedBy<BasicComponent>()); //Returns 42 parentContainer.Register(Component.For<IBusinessComponent>().ImplementedBy<RealBusinessComponent>()); //Returns the result of IBasicComponent var childContainer = new WindsorContainer(); childContainer.Register(Component.For<IBasicComponent>().ImplementedBy<BasicComponent2>()); //Returns 40 parentContainer.AddChildContainer(childContainer); var service = childContainer.Resolve<IBusinessComponent>(); Assert.AreEqual(40, service.GetBusinessValue()); //This fails with the actual value being 42 }However all dependencies are apparently resolved from the parent.
If i remove the IBasicComponent registration from the parentContainer, I am not even able to resolve the dependency due to missing registrations.
Can anyone explain how you get the container to behave as Seemann claims, or is the WindsorContainer really unable to handle this type of configuration in a graceful manner?
最满意答案
您所指的行为曾用于旧Windsor版本,但在更新版本中已被更改。
基本上这是一个错误,它将允许子容器中的组件在其范围之外可见(当组件形式父对子从子组件依赖于组件时)
所以依赖关系允许来自child - > parent,而不是相反。
The behavior you're referring to used to work in old Windsor versions but was altered in more recent versions.
Basically this is a bug, and it would allow components from child container to be visible outside its scope (when a component form parent takes a dependency on a component from child)
So the dependencies are allowed to go from child --> parent but not the other way around.
更多推荐
发布评论