0

我试图弄清楚如何使用 Castle Windsor 注册以下装饰器场景。我有以下界面:

public interface ICalculate<T> where T : class 
{
   void Calculate(T value);
}

还有几个实现,最后一个是装饰器。

public class FooCalculator : ICalculate<Foo> 
{
   public void Calculate(Foo value)
   {
       // do something here with the value..
   }
}

public class BarCalculator : ICalculate<Bar>
{
    public void Calculate(Bar value)
    {
       // do something else here....
    }
}

public class CalculatorDecorator<T> : ICalculate<T> where T : class
{
    private readonly ICalculate<T> _calculator;

    public CalculatorDecorator(ICalculate<T> calculator)
    {
         _calculator = calculator;
    }

    public void Calculate(T value)
    {
       // do for example some logging...
       _calculator.Calculate(value);
    }
}

这是我的注册码

container.Register(Classes.FromAssembly()
                          .BasedOn(typeof(ICalculate<>))
                          .WithService.Base());

当我通过其通用接口请求其中一个实现时,我希望 Windsor 使用在构造函数中注入的请求实现来解析 CalculatorDecorator。

// I would like calculator to be CalculatorDecorator<Foo> in 
// this case but it is FooCalculator.
var calculator = container.Resolve<ICalculate<Foo>>();

// The same goes for this one...
var calculator = containr.Resolve<ICalculate<Bar>>();

提前致谢!

编辑:

如果我这样做,它会起作用

container.Register(Component.For<ICalculate<Foo>>()
                            .ImplementedBy<CalculatorDecorator<Foo>>(),
                   Component.For<ICalculate<Foo>>()
                            .ImplementedBy<FooCalculator>());

container.Register(Component.For<ICalculate<Bar>>()
                            .ImplementedBy<CalculatorDecorator<Bar>>(),
                   Component.For<ICalculate<Bar>>()
                            .ImplementedBy<BarCalculator>());

但如果可能的话,我更愿意注册所有组件。

4

1 回答 1

0
container.Register(AllTypes.FromAssembly()
                      .BasedOn(typeof(ICalculate<>))
                      .WithService.Base());

你试过这个吗?我们有类似的情况,这对我们有用。

更新

我认为这是不可能的,因为您将创建一个循环依赖项。我确实让它像下面这样工作:

登记

 var container = new WindsorContainer();


        container.Register(Component.For(typeof(IDecorator<>)).ImplementedBy(typeof(CalculatorDecorator<>)));

        container.Register(AllTypes.FromThisAssembly()
                               .BasedOn(typeof (ICalculate<>))
                               .WithService.Base());



        var fc = container.Resolve<IDecorator<Foo>>();
        var bc = container.Resolve<IDecorator<Bar>>();

接口:

   public interface ICalculate<T> where T : class
{
    void Calculate(T value);
}

public interface IDecorator<T> : ICalculate<T> where T : class
{

}

执行:

public class FooCalculator : ICalculate<Foo>
{
    public void Calculate(Foo value)
    {
        // do something here with the value..
    }
}

public class BarCalculator : ICalculate<Bar>
{
    public void Calculate(Bar value)
    {
        // do something else here....
    }
}

public class CalculatorDecorator<T>: IDecorator<T> where T : class
{
    private readonly ICalculate<T> _calculator;

    public CalculatorDecorator(ICalculate<T> calculator)
    {
        _calculator = calculator;
    }

    public void Calculate(T value)
    {
        // do for example some logging...
        _calculator.Calculate(value);
    }
}
于 2012-08-09T13:35:49.243 回答