[英]Supplying different configurations of the same type to different implementations?
I have two implementations that each require a different set of configuration data of the same type : 我有两个实现,每个实现都需要一组不同的相同类型的配置数据:
public ConsumerA(Configuration config) : IConsumerA { ... }
public ConsumerB(Configuration config) : IConsumerB { ... }
In my installer, I have Windsor resolving the implementations: 在我的安装程序中,我让温莎解决了这些实现:
container.Register(
Component.For<IConsumerA>().ImplementedBy<ConsumerA>().LifestyleTransient(),
Component.For<IConsumerB>().ImplementedBy<ConsumerB>().LifestyleTransient()
);
How can I ask Windsor to resolve the configurations based on the respective implementations? 如何要求温莎根据各自的实现来解决配置?
What I ended up doing was naming the configurations and using a factory, sort of like so: 我最终要做的是命名配置并使用工厂,就像这样:
Component.For<IConsumerA>().ImplementedBy<ConsumerA>()
.DependsOn(Dependency.OnComponent(typeof(Configuration), "configurationA")).LifestyleTransient(),
Component.For<IConsumerB>().ImplementedBy<ConsumerB>()
.DependsOn(Dependency.OnComponent(typeof(Configuration), "configurationB")).LifestyleTransient(),
Component.For<Configuration>().UsingFactoryMethod(
k => k.Resolve<ConfigurationFetcher>()
.GetConfigurationSection<ConfigurationSection>(ConfigurationSection.ConfigurationASectionName)
.GetConfiguration()).Named("configurationA"),
Component.For<Configuration>().UsingFactoryMethod(
k => k.Resolve<ConfigurationFetcher>()
.GetConfigurationSection<ConfigurationSection>(ConfigurationSection.ConfigurationBSectionName)
.GetConfiguration()).Named("configurationB"),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.