我想弄清楚如何用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"));
他们都需要第二个自己的单身路径Beta&gt; BetaClient&gt; RateLimiter&gt; ......
我该怎么做?
编辑:基于答案的更多实施细节
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");
可以在没有命名的情况下完成吗?
答案 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"));