NHibernate Facility Further Examples - haf/Castle.Facilities.NHibernate GitHub Wiki

If you have multiple databases, register multiple INHibernateInstaller components before adding the NHibernateFacility. Only one installer may return true for 'IsDefault' and each installer must have a unique key as returned by the SessionFactoryKey-property.

If you have registered two installers:

new WindsorContainer().Register(
    Component.For<INHibernateInstaller>().ImplementedBy<FirstDb>(),
    Component.For<INHibernateInstaller>().ImplementedBy<SecondDb>()
);

where

class FirstDb : INHibernateInstaller {
    // ..    
    public static const string SessionFactoryKey = "sf.default";
    bool INHibernateInstaller.IsDefault { get { return true; } }
    string INHibernateInstaller.SessionFactoryKey { get { return SessionFactoryKey; } }
    // ..
}

class SecondDb : INHibernateInstaller {
    // ..    
    public static const string SessionFactoryKey = "sf.second";
    bool INHibernateInstaller.IsDefault { get { return false; } }
    string INHibernateInstaller.SessionFactoryKey { get { return SessionFactoryKey; } }
    // ..
}

You could resolve ISession for the second database as such:

static class IoC {
    private static IWindsorContainer _container;
    public static void SetContainer(IWindsorContainer container) { _container = container; }
    public static T Resolve<T>(string t) { return _container.Resolve<T>(t); }
}

class MyService : IMyService {
    void IMyService.DoWork() {
        IoC.Resolve<ISession>(SecondDb.SessionFactoryKey + NHibernateFacility.SessionTransientSuffix).Save(new Thing(17.0));
    }
}   
⚠️ **GitHub.com Fallback** ⚠️