Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection with two independent singleton paths of same class

please consider this dependency chart: https://i.sstatic.net/FFeS4.jpg

I am trying to figure out how to solve my scenario with Castle Windsor. There are some interfaces and classes depend of each other. Now what I need to solve register components like this:

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

both of them needed own singleton path Alfa > AlfaClient > RateLimiter > ...

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

both of them needed second own singleton path Beta > BetaClient > RateLimiter > ...

  • If I resolve Alfa1 and Alfa2, they have same own singleton RateLimiter
  • If I resolve Beta1 anb Beta2, they have same own singleton RateLimiter but different then RateLimiter in Alfa.

How can I do this ?

EDIT: more implementation details based on answers

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

Can be done without Named ?

like image 656
Filip Honzárek Avatar asked Dec 19 '25 09:12

Filip Honzárek


2 Answers

Did you try ?

container.Register(Component.For<IRateLimiter>().ImplementedBy<RateLimiter>().LifestyleSingleton().Named("RateLimiter"));
like image 108
Mike Ezzati Avatar answered Dec 20 '25 23:12

Mike Ezzati


Service overriding works here:

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"));
like image 43
Jan Muncinsky Avatar answered Dec 20 '25 21:12

Jan Muncinsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!