我有以下课程。
public class NHibernateInstaller : IWindsorInstaller
{
private string _overrideConnectionstring;
private IPersistenceConfigurer _persistenceConfigurer;
private static IPersistenceConfigurer _defaultConfiguration;
public IPersistenceConfigurer PersistenceConfigurer
{
get
{
return _persistenceConfigurer ?? (_persistenceConfigurer = MsSqlConfiguration.MsSql2008
.ConnectionString(Config.DefaultConnection));
}
protected set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (_persistenceConfigurer != null)
{
throw new InvalidOperationException("Property has already been set");
}
_persistenceConfigurer = value;
}
}
public void Install(IWindsorContainer container, IConfigurationStore store)
{
var fluentConfiguration = Fluently.Configure()
.Mappings(m =>
{
var autoPersistenceModel = AutoMap.AssemblyOf<Log>()
.UseOverridesFromAssemblyOf<LogMappingOverride>()
.Where(t => t != typeof(SomeNonEntity));
m.AutoMappings.Add(autoPersistenceModel);
});
fluentConfiguration.Database(PersistenceConfigurer);
var sessionFactory = fluentConfiguration.BuildSessionFactory();
container.Register(Component.For<ISessionFactory>()
.Instance(sessionFactory)
.LifestyleSingleton());
container.Register(Component.For<ISession>()
.UsingFactory((ISessionFactory factory) => sessionFactory.OpenSession())
.LifestylePerWebRequest());
}
}
我添加了一个允许我更改persistenceconfigurer的属性,以便我可以在测试时更改它。目前,默认的persistenceconfigurer用于从包含对ConfigurationManager的调用的公共静态方法获取连接字符串。我想使用Castle Dictionary Adapter来获取我的appSettings等,并想知道将defaultConnection连接字符串注入NHibernateInstaller的最佳方法。
我知道安装程序的文档需要一个默认的公共构造函数。
任何建议都会很棒。
答案 0 :(得分:2)
文档说:
“当安装程序由Windsor实例化时,它们必须具有公共默认构造函数”,即使用InstallerFactory
或FromAssembly
加载它们。
如果要手动将安装程序添加(或可以添加)到容器中,则不需要默认构造函数,因此这会将连接字符串传递给构造函数,并将其存储在字段中:
container.Install(new NHibernateInstaller(defaultConnectionString))