WindsorContainers AddChildContainer真的这么糟糕吗?(Is WindsorContainers AddChildContainer really this bad?)

编程入门 行业动态 更新时间:2024-10-26 20:27:35
WindsorContainers AddChildContainer真的这么糟糕吗?(Is WindsorContainers AddChildContainer really this bad?)

我正在尝试从一组基本注册中分支多个子容器,以便于进行不同的配置设置。

我认为基于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.

更多推荐

本文发布于:2023-08-05 21:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:糟糕   WindsorContainers   AddChildContainer   bad

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!