How to identify a particular entity's Session Factory with Fluent NHibernate and Multiple Databases

Posted by Trevor on Stack Overflow See other posts from Stack Overflow or by Trevor
Published on 2010-04-23T12:39:06Z Indexed on 2010/04/23 12:43 UTC
Read the original article Hit count: 298

I've already asked this question as part of an answer to another question ( see http://stackoverflow.com/questions/2655861/fluent-nhibernate-multiple-databases ) but thought it better to ask again here.

My problem is this:

I'm using Fluent NHibernate. My application uses multiple databases. Each database has its own entities registered (mapped) against it. The result is that have multiple Session Factories, each one relating to a single DB, and each 'containing' its own set of mapped entities.

For loading entities I've created a generic Factory class that provides some standard load methods usable for any registered entity (in any DB). The problem is: The load methods need to use the correct session factory for the entity class I'm busy dealing with. How would I determine which session factory I need to use? I have all the Session Factories 'on hand' (and indexed by database name), I just need a way, knowing just the type of Entity I'm about to load, of choosing the right Session Factory to use.

For example:

public IBaseBusinessObject CreatePopulatedInstance(Type boType, Guid instanceKey)
{
  IBaseBusinessObject result = null;
  ISessionFactory sessionFactory = GetSessionFactory(boType);
  using (ISession session = sessionFactory.OpenSession())
  {
    using (session.BeginTransaction())
    {
      result = (IBaseBusinessObject)session.Get(boType, instanceKey);
    }
  }
  return result;
}

What needs to go on in GetSessionFactory(boType) ?

Thanks for reading!

© Stack Overflow or respective owner

Related posts about c#

Related posts about fluent-nhibernate