please consider this dependency chart:

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 ?
Did you try ?
container.Register(Component.For<IRateLimiter>().ImplementedBy<RateLimiter>().LifestyleSingleton().Named("RateLimiter"));
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With