在Castle Windsor通用对象配置中使用组件ID

时间:2010-06-08 16:16:17

标签: c# generics castle-windsor

一个问题,但非常相关。

Castle Windsor是否可以解析配置条目,例如 -

Assembly.Namespace.Object1`2 [[$ {ComponentId1}],[$ {ComponentId2}]],程序集

其中ComponentId1和ComponentId2被定义为组件。 Castle Windsor似乎没有解析ComponentId,它只是在Castle.Windsor程序集中寻找ComponentId1。

如果你不能做第一个问题,第二个问题就出现了。如果必须使用完整的程序集引用而不是ComponentId,那么如何将任何参数传递给正在创建的对象?例如,设置ComponentId1.Field1 =“blah”,或将某些内容传递给ComponentId1的构造函数

希望有意义

更新 -

根据代码请求,我将以下内容组合在一起 -

物件

public class Wrapper<T, T1> where T : ICollector where T1:IProcessor
{
    private T _collector;
    private T1 _processor;

    public Wrapper(T collector, T1 processor)
    {
        _collector = collector;
        _processor = processor;
    }

    public void GetData()
    {
        _collector.CollectData();
        _processor.ProcessData();
    }

}

public class Collector1 : ICollector
{
    public void CollectData()
    {
        Console.WriteLine("Collecting data from Collector1 ...");
    }
}

public class Processor1 : IProcessor
{
    public void ProcessData()
    {
        Console.WriteLine("Processing data from Processor1  ...");
    }
}

重复了示例

中每种类型对象的3个

配置

<components>
<component id="Collector1"
           service="CastleWindsorPlay.ICollector, CastleWindsorPlay"
           type="CastleWindsorPlay.Collector1, CastleWindsorPlay"/>
<component id="Collector2"
           service="CastleWindsorPlay.ICollector, CastleWindsorPlay"
           type="CastleWindsorPlay.Collector2, CastleWindsorPlay"/>
<component id="Collector3"
           service="CastleWindsorPlay.ICollector, CastleWindsorPlay"
           type="CastleWindsorPlay.Collector3, CastleWindsorPlay"/>
<component id="Processor1"
           service="CastleWindsorPlay.IProcessor, CastleWindsorPlay"
           type="CastleWindsorPlay.Processor1, CastleWindsorPlay"/>
<component id="Processor2"
           service="CastleWindsorPlay.IProcessor, CastleWindsorPlay"
           type="CastleWindsorPlay.Processor2, CastleWindsorPlay"/>
<component id="Processor3"
           service="CastleWindsorPlay.IProcessor, CastleWindsorPlay"
           type="CastleWindsorPlay.Processor3, CastleWindsorPlay"/>
<component id="Wrapper1"
           type="CastleWindsorPlay.Wrapper`2[[CastleWindsorPlay.Collector1, CastleWindsorPlay],[CastleWindsorPlay.Processor3, CastleWindsorPlay]], CastleWindsorPlay" />
  </components>

实例化

        var wrapper = (Wrapper<ICollector, IProcessor>) container.Resolve("Wrapper1");
        wrapper.GetData();

这个简短的示例错误,但错误消息 -

无法创建组件'Wrapper1',因为它具有要满足的依赖性。 Wrapper1正在等待以下依赖项:

服务: - 未注册的CastleWindsorPlay.Collector1。 - 未注册的CastleWindsorPlay.Processor3。

关于这一点的好奇部分是我可以在调用包装器之前单独解析Collector1和Processor3,但是包装器仍然看不到它们。

这是一个基本的例子,我希望能够做的下一件事是在实例化Wrapper时,在收集器和/或处理器上设置一个属性。所以它可能类似于Collector.Id = 10,但是在配置包装器的配置中设置。针对收集器组件定义的设置不起作用,因为我希望能够使用不同的Id来实例化每个收集器的多个副本

更新2

我实际上要做的是 -

<components>
<component id="Wrapper1"
           type="CastleWindsorPlay.Wrapper`2[${Collector1}(id=1)],[${Processor3}]], CastleWindsorPlay" />
<component id="Wrapper2"
           type="CastleWindsorPlay.Wrapper`2[${Collector1}(id=3)],[${Processor3}]], CastleWindsorPlay" />
  </components>

然后将另一个对象定义为

<component id="Manager"
           type="CastleWindsorPlay.Manager,CastleWindsorPlay">
  <parameters>
    <wrappers>
      <array>
        <item>${Wrapper1}</item>
        <item>${Wrapper2}</item>
      </array>
    </wrappers>
  </parameters>

然后最后在代码中才能调用 -

var manager = (Manager)container.Resolve("Manager");

这应该返回管理器对象,填充了一个包装器数组,并使用正确的收集器和转换器配置包装器。

我知道Castle配置中有错误,这就是为什么我问这个问题,我不知道如何设置配置来做我想要的事情,或者即使它可以做到这一点在温莎城堡

1 个答案:

答案 0 :(得分:1)

UPDATE2

好吧现在开始变得有意义了:)就像我从一开始就说的那样,看起来你正在寻找服务覆盖:

<components>
<component id="Wrapper1" type="CastleWindsorPlay.Wrapper`2[<<closing types go here>>]/>
   <parameters>
      <collector>${collector1WithId1}</collector>
      <collector>${processor3WithId3}</collector>
   </parameters>
</component>

<component id="Wrapper2" type="CastleWindsorPlay.Wrapper`2[<<closing types go here>>]/>
   <parameters>
      <collector>${collector1WithId5}</collector>
      <collector>${processor3WithId8}</collector>
   </parameters>
</component>
</components>

更新

好的,谢谢你的例子。您收到错误的原因是您将组件 Collector1组件 Processor3注册为服务ICollector和IProcessor。 您对Collector1Processor3没有服务,因此当Windsor尝试提供它们时,它无法找到它们,因此会出现异常消息。

您需要将其注册为适当的服务才能实现此目的。


我不知道你在这里要做什么,但我认为你正在寻找service overridesinline parameters