具有两个相同类的独立单一路径的依赖注入

时间:2018-03-04 13:46:31

标签: c# .net dependency-injection castle-windsor

请考虑此依赖关系图: https://i.stack.imgur.com/FFeS4.jpg

我想弄清楚如何用Castle Windsor解决我的情景。   有些接口和类彼此依赖。现在我需要解决这样的寄存器组件:

container.Register(Component.For<IWrapper>().ImplementedBy<Alfa>().LifestyleSingleton().Named("Alfa1"));
container.Register(Component.For<IWrapper>().ImplementedBy<Alfa>().LifestyleSingleton().Named("Alfa2"));

他们都需要自己的单一路径Alfa&gt; AlfaClient&gt; RateLimiter&gt; ......

container.Register(Component.For<IWrapper>().ImplementedBy<Beta>().LifestyleSingleton().Named("Beta1"));
container.Register(Component.For<IWrapper>().ImplementedBy<Beta>().LifestyleSingleton().Named("Beta2"));

他们都需要第二个自己的单身路径Be​​ta&gt; BetaClient&gt; RateLimiter&gt; ......

  • 如果我解析Alfa1和Alfa2,他们有相同的单身 RateLimiter
  • 如果我解析Beta1 anb Beta2,他们就拥有相同的自己 单身RateLimiter但与Alfa中的RateLimiter不同。

我该怎么做?

编辑:基于答案的更多实施细节

        container.Register(
            Component
                .For<IIntervalRateLimiter>()
                .ImplementedBy<IntervalRateLimiter>()
                .LifestyleTransient());

        container.Register(
            Component
                .For<IDurationRateLimiter>()
                .ImplementedBy<DurationRateLimiter>()
                .LifestyleTransient());

        container.Register(
            Component
                .For<IRateLimiter>()
                .ImplementedBy<RateLimiter>()
                .LifestyleSingleton()
                .Named("AlfaClientRateLimiter"));

        container.Register(
            Component.For<IAlfaClient>().ImplementedBy<AlfaClient>()
                 .DependsOn(Dependency.OnComponent(typeof(IRateLimiter), "AlfaClientRateLimiter"))
                 .Named("AlfaClient")
                 .LifestyleSingleton());

        container.Register(
            Component.For<IWrapper>().ImplementedBy<Alfa>()
                 .DependsOn(Dependency.OnComponent(typeof(IAlfaClient), "AlfaClient"))
                     .Named("AlfaWrapper")
                     .LifestyleSingleton());

        container.Register(
            Component.For<IClient>().ImplementedBy<Client>()
                 .DependsOn(Dependency.OnComponent(typeof(IWrapper), "AlfaWrapper"))
         .Named("Alfa1"));


        container.Register(
            Component.For<IClient>().ImplementedBy<Client>()
                 .DependsOn(Dependency.OnComponent(typeof(IWrapper), "AlfaWrapper"))
         .Named("Alfa2"));

var alfa1 = container.Resolve<IClient>("Alfa1");
var alfa2 = container.Resolve<IClient>("Alfa2");

可以在没有命名的情况下完成吗?

2 个答案:

答案 0 :(得分:1)

你试过吗?

container.Register(Component.For<IRateLimiter>().ImplementedBy<RateLimiter>().LifestyleSingleton().Named("RateLimiter"));

答案 1 :(得分:1)

服务覆盖在这里工作:

container.Register(Component.For<IAlfaClient>().ImplementedBy<AlfaClient>()
    .DependsOn(Property.ForKey<IRateLimiter>().Is("RateLimiterAlfa"))
    .LifestyleSingleton());

container.Register(Component.For<IBetaClient>().ImplementedBy<BetaClient>()
    .DependsOn(Property.ForKey<IRateLimiter>().Is("RateLimiterBeta"))
    .LifestyleSingleton());

container.Register(Component.For<IRateLimiter>().ImplementedBy<RateLimiter>().LifestyleSingleton().Named("RateLimiterAlfa"));
container.Register(Component.For<IRateLimiter>().ImplementedBy<RateLimiter>().LifestyleSingleton().Named("RateLimiterBeta"));