如何识别特定实体的会话工厂用流利的NHibernate和多个数据库 [英] How to identify a particular entity's Session Factory with Fluent NHibernate and Multiple Databases

查看:95
本文介绍了如何识别特定实体的会话工厂用流利的NHibernate和多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题从 http://stackoverflow.com/questions/2655861/fluent-nhibernate-multiple是继。-databases (不需要遵循这个环节,应该有足够的背景知识在这里)

Question follows on from http://stackoverflow.com/questions/2655861/fluent-nhibernate-multiple-databases (no need to follow this link,there should be enough background here).

我的问题是这样的:

我使用功能NHibernate。我的应用程序使用多个数据库。每个数据库都有注册了自己的实体(映射)反对。其结果是,具有多个会议工厂,每一个与单个DB中,并且每个包含其自己的一组映射的实体。

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.

有关装载实体我已经创建了一个通用工厂类,提供了可用于任何注册的实体(在任何DB)一些标准的load方法。现在的问题是:负载方法需要使用正确的会话工厂的实体类,我忙于处理。我将如何确定我需要使用的会话工厂?我有'手'的所有会话工厂(和数据库名称索引),我只是需要一种方法,只知道我即将加载选择合适的会话工厂使用的,实体的类型。

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.

例如:

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;
}



什么需要了getSessionFactory(boType)?去

What needs to go on in GetSessionFactory(boType) ?

感谢您的阅读!

推荐答案

我会用看ISessionFactory.GetClassMetaData启动。方法

I'd start with looking at the ISessionFactory.GetClassMetaData method.

IClassMetadata metadata = sessionfactory.GetClassMetadata(boType); 

如果你能保持你的ISessionFactory实例的列表,你可以通过他们列举你的方式,直到找到一个有你的类型boType元。

If you can maintain a list of your ISessionFactory instances, you can enumerate your way through them until you find the one which has metadata for your type boType.

我从来没有使用它,没有我的手引用,但我期望它要么回null或抛出一个异常,如果它不识别的类型。如果你得到了元数据的值没有出现错误,那么这就是你的会话工厂。

I've never used it and don't have my references to hand, but I'd expect it to either return null or raise an exception if it doesn't recognise the type. If you get a value for metadata without an error, then that's your session factory.

好运气

尼尔

这篇关于如何识别特定实体的会话工厂用流利的NHibernate和多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆