Filip Honzárek
Filip Honzárek

Reputation: 35

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 > ...

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 ?

Upvotes: 3

Views: 156

Answers (2)

Jan Muncinsky
Jan Muncinsky

Reputation: 4408

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

Upvotes: 1

Mike Ezzati
Mike Ezzati

Reputation: 3166

Did you try ?

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

Upvotes: 1

Related Questions