I have the following class.
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());
}
}
I have added a property that allows me to change the persistenceconfigurer so that I can change this when testing. At the moment the default persistenceconfigurer is for gets a connection string from a public static method that wraps a call to ConfigurationManager. I want to use Castle Dictionary Adapter to get my appSettings etc and wanted to know the best way to inject the defaultConnection connection string into the NHibernateInstaller.
I know the documentation for installers requires a default public constructor.
Any suggestions would be great.
The docs say:
"When installers are instantiated by Windsor, they must have public default constructor", i.e. if using the InstallerFactory
or FromAssembly
to load them.
If you are adding (or can add) the installers to the container manually, then no default constructor is required, so this would pass the connection string to the constructor, where it can be stored in a field:
container.Install(new NHibernateInstaller(defaultConnectionString))