我可以将其他组件传递到Castle Windsor配置吗?

时间:2015-05-12 16:54:03

标签: dependency-injection castle-windsor

假设我有一个主要组件,我想以特定的方式初始化,我有它的构造函数为此目的采取接口。有没有办法在我的xml中为这个接口定义我想要的实现,然后将它作为参数注入主组件?像这样:

public interface IComponent2 {
    void DoStuff();
}

public class ConcreteCompImpl2 : IComponent2 {

    IComponent1 _comp;
    public ConcreteCompImpl2(IComponent1 comp) {
        _comp = comp;
    }

    public void DoStuff(){
        //do stuff
    }
}



<component id="component1" service="ABC.IComponent1, ABC" type="ABC.ConcreteCompImpl1, ABC" />
<component id="component2" service="ABC.IComponent2, ABC" type="ABC.ConcreteCompImpl2, ABC" >         
    <parameters>
       <component1>???</component1>
    </parameters>
</component>

或者我是否认为这一切都错了,有一种更简单的方法来实现这一目标?我想要做的主要是配置IComponent1在创建IComponent2时将注入的“种类”。感谢

1 个答案:

答案 0 :(得分:1)

如果您只有一个实施byte[] b = new byte[]{ 0, 0, 0, -127 }; // 0x00000081 String s = new String(b,StandardCharsets.UTF_8); // UTF8 = 0x0000, 0x0000, 0x0000, 0xfffd b = s.getBytes(StandardCharsets.UTF_8); // [0, 0, 0, -17, -65, -67] 0x000000efbfbd != 0x00000081 的具体类,则在您解析IComponent1时会自动注入该类。

如果您有多个类实现IComponent2并希望每次IComponent1解决时都需要特定的一个,那么您需要具体inline dependency

IComponent2

我不完全确定你可以在XML配置中指定它,但老实说你应该使用Fluent API而不是XML configuration,除非你有一个非常令人信服的理由使用它。如上面的链接所述:

  

在创建Fluent Registration API之前,使用XML注册组件的能力主要是从Windsor早期开始的。它比代码中的注册要强大得多,而且许多任务只能通过代码完成。