我可以通过一些带参数的方法调用来解决windsor城堡中的服务吗?

时间:2011-08-08 12:13:56

标签: c# castle-windsor

我有IRFConfigurationSection,其实例如下:

(RFConfigurationSection)ConfigurationManager.GetSection("userSettings/ABZReportFactoryServer");

我想把这个电话放到温莎城堡,让这个班级单身。所以当我需要实例化这个RFConfigurationSection类时,我想这样做。

IWindsorContainer container = new WindsorContainer(new XmlInterpreter());
configSection = container.Resolve<IRFConfigurationSection>();

是否有可能以某种方式配置Windsor Castle配置来执行此操作?

2 个答案:

答案 0 :(得分:1)

使用工厂会更好吗,例如:

public interface IConfigurationFactory<out TConfigurationSection>
{
    TConfigurationSection GetConfiguration();
}

public class RFConfigurationFactory : IConfigurationFactory<IRFConfigurationSection>
{
    public IRFConfigurationSection GetConfiguration()
    {
        return ConfigurationManager.GetSection("userSettings/ABZReportFactoryServer") as RFConfigurationSection;
    }
}

这样,您可以将工厂添加到容器中,并解析该实例:

var configFactory = container.Resolve<IConfigurationFactory<IRFConfigurationSection>>();
var config = configFactory.GetConfiguration();

答案 1 :(得分:1)

container.Register(
    Component.For<RFConfigurationSection>()
        .UsingFactoryMethod(() => ConfigurationManager.GetSection("userSettings/ABZReportFactoryServer"))
        .LifeStyle.Singleton
);