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
);
精彩评论