Windsor Castle使用相同的界面解析多个依赖项

时间:2012-12-04 09:56:59

标签: dependency-injection castle-windsor

问题:

类构造函数如下:

public class ParsingRulesProvider : IParsingRulesProvider{
    public ParsingRulesProvider(List<IParsingRule> rules){}
}

我们有两条规则:

public class ARule : IParsingRule{
    public ARule(IASomeObject someObject){}
}

public class BRule : IParsingRule{
    public BRule(IBSomeObject someObject){}
}

问题:

如何在代码中注册:

  1. ParsingRulesProvider有规则A和B吗?
  2. ParsingRulesProvider所有对象实现了IParsingRule?
  3. ARuleBRule

1 个答案:

答案 0 :(得分:0)

AD1)

container.Kernel.Resolver.AddSubResolver(new ListResolver(container.Kernel, true));

AD2)

container.Register(Component
    .For<IParsingRulesProvider>()
    .ImplementedBy<ParsingRulesProvider>());

AD3)

    container.Register(Component
            .For<IParsingRule>()
            .ImplementedBy<ARule>()
            .LifeStyle.Singleton
            .Named("ARule"));

    container.Register(Component
        .For<IParsingRule>()
        .ImplementedBy<BRule>()
        .LifeStyle.Singleton
        .Named("BRule"));