1

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.

Andrew
  • 5,215
  • 1
  • 23
  • 42

1 Answers1

3

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))
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Thanks @stuartd, that is pretty much what I have done. The other options is to change my NHibernateInstaller to internal install it as above and the install other installers with FromAssembly. This would mean I'd need to set internals visible on my unit test class. – Andrew Jan 29 '15 at 12:41
  • @Andrew you could use a custom InstallerFactory and set it to ignore this particular installer, there's an example at http://stackoverflow.com/a/9084690/43846 – stuartd Jan 29 '15 at 12:44