开发者

Can I resolve service in windsor castle through some method call with parameters?

开发者 https://www.devze.com 2023-03-26 10:06 出处:网络
I have IRFConfigurationSection which is instanced like this: (RFConfigurationSection)ConfigurationManager.GetSection(\"userSettings/ABZReportFactoryServer\");

I have IRFConfigurationSection which is instanced like this:

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

I want to put this cal开发者_高级运维l into Windsor castle and make this class singleton. So when I need to instance this RFConfigurationSection class, I'll like to do it this way.

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

Is it possible to somehow configure Windsor Castle config to do this?


Would a better approach to use a factory, e.g:

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

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

That way, you can add the factory to the container, and resolve an instance of that:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号