构造函数注入问题

发布于 2024-10-21 14:24:48 字数 1769 浏览 0 评论 0原文

创建 MainClass 时,将调用 public MyWindsorClass(IMyInterface obj) {} ;创建 MainClass2 时,当我期望调用 public MyWindsorClass() {} 时,也会调用 public MyWindsorClass(IMyInterface obj) {} 。我究竟做错了什么?我在构造函数注入方面遇到问题。这是一个示例:

class Program
{
    static void Main(string[] args)
    {
        var container = new WindsorContainer(new XmlInterpreter());
        var objs = container.ResolveAll<IMyWindsorInterface>();
    }
}

public interface IMyWindsorInterface { }

public class MyWindsorClass : IMyWindsorInterface
{
    public MyWindsorClass() {}
    public MyWindsorClass(IMyInterface obj) {}
}

public interface IMyInterface { }
public class MyInjectedClass : IMyInterface{ }

<castle>
     <components>
        <component id="MainClass"
             service="CastleTest.IMyWindsorInterface, CastleTest"
             type="CastleTest.MyWindsorClass, CastleTest"
             inspectionBehavior="None"
             lifestyle="Transient">
       <parameters>
          <obj>${blah}</obj>
       </parameters>
        </component>
    <component id="MainClass2"
               service="CastleTest.IMyWindsorInterface, CastleTest"
               type="CastleTest.MyWindsorClass, CastleTest"
               inspectionBehavior="None"
               lifestyle="Transient" />
         <component id="blah" 
             service="CastleTest.IMyInterface, CastleTest" 
             type="CastleTest.MyInjectedClass, CastleTest" 
             inspectionBehavior="None"/>
     </components>
</castle>

创建 MainClass 时,将调用 public MyWindsorClass(IMyInterface obj) {} ;创建 MainClass2 时,当我期望调用 public MyWindsorClass() {} 时,也会调用 public MyWindsorClass(IMyInterface obj) {} 。我到底做错了什么?感谢任何可以提供帮助的人!

When MainClass is created public MyWindsorClass(IMyInterface obj) {} is called and when MainClass2 is created public MyWindsorClass(IMyInterface obj) {} is also called when I'm expecting public MyWindsorClass() {} to be called. What exactly am I doing wrong?I'm having an issue with constructor injection. Here is an example:

class Program
{
    static void Main(string[] args)
    {
        var container = new WindsorContainer(new XmlInterpreter());
        var objs = container.ResolveAll<IMyWindsorInterface>();
    }
}

public interface IMyWindsorInterface { }

public class MyWindsorClass : IMyWindsorInterface
{
    public MyWindsorClass() {}
    public MyWindsorClass(IMyInterface obj) {}
}

public interface IMyInterface { }
public class MyInjectedClass : IMyInterface{ }

<castle>
     <components>
        <component id="MainClass"
             service="CastleTest.IMyWindsorInterface, CastleTest"
             type="CastleTest.MyWindsorClass, CastleTest"
             inspectionBehavior="None"
             lifestyle="Transient">
       <parameters>
          <obj>${blah}</obj>
       </parameters>
        </component>
    <component id="MainClass2"
               service="CastleTest.IMyWindsorInterface, CastleTest"
               type="CastleTest.MyWindsorClass, CastleTest"
               inspectionBehavior="None"
               lifestyle="Transient" />
         <component id="blah" 
             service="CastleTest.IMyInterface, CastleTest" 
             type="CastleTest.MyInjectedClass, CastleTest" 
             inspectionBehavior="None"/>
     </components>
</castle>

When MainClass is created public MyWindsorClass(IMyInterface obj) {} is called and when MainClass2 is created public MyWindsorClass(IMyInterface obj) {} is also called when I'm expecting public MyWindsorClass() {} to be called. What exactly am I doing wrong? Thanks to anyone that can help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2024-10-28 14:24:48

默认情况下,Windsor 将自动装配所有可用的依赖项。在您的情况下,IMyInterface 可作为组件使用,因此 Windsor 将选择可以满足最多依赖项的构造函数。

您可以通过从组件模型中删除构造函数来临时更改此行为,以便 Windsor 不再“看到”它:

var container = new WindsorContainer();
container.Kernel.ComponentModelCreated += model => {
    if (model.Name == "MainClass2")
        model.Constructors.Remove(model.Constructors.First(c => c.Dependencies.Any(m => m.TargetType == typeof(IMyInterface))));
};
container.Install(Configuration.FromAppConfig());

Windsor will by default autowire all available dependencies. In your case, IMyInterface is available as a component so Windsor will choose the constructor that can satisfy the most dependencies.

You may change this behavior ad-hoc by removing the constructor from the component model so Windsor doesn't 'see' it any more:

var container = new WindsorContainer();
container.Kernel.ComponentModelCreated += model => {
    if (model.Name == "MainClass2")
        model.Constructors.Remove(model.Constructors.First(c => c.Dependencies.Any(m => m.TargetType == typeof(IMyInterface))));
};
container.Install(Configuration.FromAppConfig());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文