2016-09-06 85 views
0

我有兩個實現,各需要一組不同的配置數據的同類型的爲不同的實現提供相同類型的不同配置?

public ConsumerA(Configuration config) : IConsumerA { ... } 
public ConsumerB(Configuration config) : IConsumerB { ... } 

在我安裝,我有溫莎解決實現:

container.Register(
    Component.For<IConsumerA>().ImplementedBy<ConsumerA>().LifestyleTransient(), 
    Component.For<IConsumerB>().ImplementedBy<ConsumerB>().LifestyleTransient() 
); 

我怎麼能請Windsor根據各自的實現解決配置問題?

回答

0

我最終什麼事做了命名的配置和使用的工廠,有點像這樣:

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"), 
相關問題