在这种情况下如何使用Castle Windsor型工厂?

时间:2016-09-03 11:10:38

标签: c# dependency-injection castle-windsor

简单的DI示例:

public interface INumberToWordConverter
{
    string ConvertNumber(int number);
}

public interface IOutputManager
{
    void Write<T>(string who, T what);
}

public interface INumberProvider
{
    int GenerateNumber();
}

public class PlayWithDI
{
    private IOutputManager _outputManagerService;
    private INumberProvider _numberProviderService;
    private INumberToWordConverter _numberToWordConverterService;

    private PlayWithDI() { }
    public PlayWithDI(
        IOutputManager outputManagerService,
        INumberProvider numberProviderService,
        INumberToWordConverter numberToWordConverterService)
    {
        if (outputManagerService == null)
            throw new ArgumentNullException(nameof(outputManagerService));
        if (numberProviderService == null)
            throw new ArgumentNullException(nameof(numberProviderService));
        if (numberToWordConverterService == null)
            throw new ArgumentNullException(nameof(numberToWordConverterService));

        _outputManagerService = outputManagerService;
        _numberProviderService = numberProviderService;
        _numberToWordConverterService = numberToWordConverterService;
    }

    public void Execute()
    {
        int number = _numberProviderService.GenerateNumber();
        string wordOfNumber = _numberToWordConverterService.ConvertNumber(number);
        _outputManagerService.Write(nameof(PlayWithDI), wordOfNumber);
    }

示例实现(只是ctors):

// Implements INumberProvider
public RandomNumberProvider(
        int min, int max,
        IOutputManager outputManagerService)
{
    ...
}

// Implements INumberToWordConverter
public ItalianNumberToWordConverter(
      IOutputManager outputManagerService)
{
    ...
}

// Implements IConsoleManager
public ConsoleOutputManager()
{
    ...
}

如果我知道哪minmaxRandomNumberProvider,我会这样解决:

    public void Install(
        IWindsorContainer container, 
        IConfigurationStore store)
    {
        container.Register(
            Component.For<PlayWithDI>());

        container.Register(
            Component.For<IOutputManager>()
            .ImplementedBy<ConsoleOutputManager>());

        container.Register(
            Component.For<INumberProvider>()
            .ImplementedBy<RandomNumberProvider>()
            .DependsOn(
                Dependency.OnValue("min", 2),
                Dependency.OnValue("max", 20)));

        container.Register(
            Component.For<INumberToWordConverter>()
            .ImplementedBy<ItalianNumberToWordConverter>());
    }

...
container.Install(new DependenciesConfiguration1());
var testDI = container.Resolve<PlayWithDI>();
testDI.Execute();

当我想在运行时向RandomNumberProvider提供自定义参数时会出现问题。

我看了一下TypedFactory,但我在这个例子中并没有真正理解它,因为如果我先解决一个工厂,我该如何解决PlayWithDi?我应该传递给它的构造函数一个INumberProviderFactory而不是一个INumberProvider吗?

在这种情况下,我想到了这样的工厂:

public interface INumberProviderFactory
{
    INumberProvider Create(
        IOutputManager outputManager, 
        int min, int max);
}

当我打电话给Create时,我应该如何解决outputManager呢?我很困惑。

1 个答案:

答案 0 :(得分:1)

  

当我想在运行时向RandomNumberProvider提供自定义参数时会出现问题。

这是你出错的地方。 Injecting runtime data into components during construction is ananti-pattern

  

在构建期间不要将运行时数据注入应用程序组件;它会导致歧义,使组合根变得更加复杂,并且使得验证DI配置的正确性变得非常困难。 [...]让运行时数据流过构造对象图的方法调用。

换句话说,您的RandomNumberProvider应接受minmax参数是GenerateNumber方法的输入参数。