title: Injecting Correct ISessionFactory Into IRepository Using Castle Windsor and NHibernate Facility
body: <p>I have three SQL Server databases that a single application retrieves data from.  I am using NHibernate to retrieve data from the different databases.  I have things set up so that each database has its own repository and class mappings in its own assembly.  In my castle.config file I have the database connections setup using the Castle NHibernate Facility:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;castle&gt;
    &lt;facilities&gt;
      &lt;facility id="factorysupport" type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.Windsor" /&gt;
      &lt;facility id="nhibernate" isWeb="false" type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration"&gt;
        &lt;factory id="databaseone.factory" alias="databaseone"&gt;
          &lt;settings&gt;
              &lt;!--Settings Here --&gt;
          &lt;/settings&gt;
          &lt;assemblies&gt;
             &lt;assembly&gt;DAL.DatabaseOne&lt;/assembly&gt;
          &lt;/assemblies&gt;
        &lt;/factory&gt;
        &lt;factory id="databasetwo.factory" alias="databasetwo"&gt;
          &lt;settings&gt;
            &lt;!--Settings Here --&gt;
          &lt;/settings&gt;
          &lt;assemblies&gt;
            &lt;assembly&gt;DAL.DatabaseTwo&lt;/assembly&gt;
          &lt;/assemblies&gt;
        &lt;/factory&gt;
        &lt;factory id="databasethree.factory" alias="databasethree"&gt;
          &lt;settings&gt;
              &lt;!--Settings Here --&gt;
          &lt;/settings&gt;
          &lt;assemblies&gt;
               &lt;assembly&gt;DAL.DatabaseThree&lt;/assembly&gt;
          &lt;/assemblies&gt;
        &lt;/factory&gt;
      &lt;/facility&gt;
    &lt;/facilities&gt;
  &lt;/castle&gt;
  </code></pre>
  
  <p>All of my repositories have a constructor that take an ISessionFactory as the parameter:</p>
  
  <pre><code>public MyRepository&lt;T&gt; : IRepository&lt;T&gt;
  {
     public MyRepository(ISessionFactory factory)
     {
          //Do stuff here
     }
   }
  </code></pre>
  
  <p>I have an installer class where I would like to define the various repositories:</p>
  
  <pre><code>//In install method of IWindsorInstaller
  container.register(Component.For(typeof(IRepository&lt;&gt;)).ImplementedBy(typeof(MyRepository&lt;&gt;));
  </code></pre>
  
  <p>Using one database things work fine.  When I add the second database to the mix, the same ISessionFactory is injected into all of the repositories.  My question is what is the best way to handle this?  I could manually specify which ISessionFactory should be injected into which Repository&lt;> but I cannot seem to find documentation on this.  The best way would be if I could say something like: For all class mappings in assembly DAL.DatabaseOne, always inject the ISessionFactory corresponding to databaseone.factory; and for all class mappings in assembly DAL.DatabaseTwo, always inject the ISessionFactory corresponding to databasetwo.factory.  </p>
  
  <p>Thoughts or suggestions?</p>