Windsor Castle resolving multiple dependencies with same interface

江枫思渺然 提交于 2019-12-24 13:08:31

问题


Problem:

Class constructor is as below:

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

We have two rules:

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

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

Questions:

How to register in code:

  1. ParsingRulesProvider with rule A and B?
  2. ParsingRulesProvider with all objects implementing IParsingRule?
  3. ARule and BRule?

回答1:


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"));


来源:https://stackoverflow.com/questions/13700177/windsor-castle-resolving-multiple-dependencies-with-same-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!