NHibernate 连接多数据库怎么配置

NHibernate 连接多数据库怎么配置,第1张

sqlserver数据库连接放在配置文件中,只是针对外部程序访问sqlserver的场景,几种不同连接方式(odbc,ef,nhibernate,mybitas,等等),连接字符串也有所不同,你在网上搜一下,sqlserver连接字符串格式就知道了,配置文件的话,分2种,一种是建立项目时自带的config文件,这种文件的读写访问.net自己就有方法支持,文件格式和对应节点的说明也很详细;自己建立一个配置文件(可以是txt,xml等,格式可以自己控制,不过需要自己去编写相应的读写方法);

上面2种方式,各有优劣,第1种,使用系统自带的配置文件, *** 作方便,但是这个文件有个要注意的地方,就是它一般是在系统启动时去加载文件信息,在程序运行过程中,修改了配置文件的值,需要下次重启软件才会生效。

不过一般数据库连接字符串这种,在程序运行过程中,变化的几率小,直接用自带config文件就可以满足需求

在开发一些项目时,会使用到多个数据库。例如类A保存在数据库A,类B保存在数据库B。NHibernate在BuildSessionFactory之后,ISessionFactory就不能改变数据库的连接,即是说一个ISessionFactory只能对应一个数据库连接。但NHibernate可以在同一个应用中实例化多个ISessionFactory。实例化多个ISessionFactory,并让类A或类B找到自己所对应的ISessionFactory,获取ISession,即可实现多数据库连接。

如何通过类型获取ISessionFactory呢?ISessionFactory的Statistics.EntityNames中保存了所有映射了的实体类的类名。我们可以判断实体类的类名是否在EntityNames中,确定实体类所对应的ISessionFactory。

根据类型获取ISessionFactory:

public interface ISessionFactoryHolder

{

    ISessionFactory GetSessionFactoryForEntity<TEntity>() where TEntity : IEntity

}

 public class SessionFactoryHolder : ISessionFactoryHolder

{

    private IDictionary<string, int> entityDictionary

    private IDictionary<int, ISessionFactory> factoryDictionary

    public SessionFactoryHolder()

    {

        this.entityDictionary = new Dictionary<string, int>()

        this.factoryDictionary = new Dictionary<int, ISessionFactory>()

    }

    #region ISessionFactoryHolder Members

    public ISessionFactory GetSessionFactoryForEntity<TEntity>() where TEntity : IEntity

    {

        int hashCode = 0

        Asserts.Assert<MappingException>(

            this.EntityInDictionary(typeof(TEntity).FullName, out hashCode) == false

            , string.Format("No persister for:{0}", typeof(TEntity).FullName))

        return this.factoryDictionary[hashCode]

    }

    #endregion

    public void RegisterSessionFactory(ISessionFactory sessionFactory)

    {

        Asserts.IsNotNull(sessionFactory, "sessionFactory")

        this.factoryDictionary[sessionFactory.GetHashCode()] = sessionFactory

        this.MapingEntityNameToSessionFactoryHashCode(sessionFactory.Statistics.EntityNames

            , sessionFactory.GetHashCode())

    }

    private bool EntityInDictionary(string entityName, out int sessionFactoryHashCode)

    {

        return this.entityDictionary.TryGetValue(entityName, out sessionFactoryHashCode)

    }

    private void MapingEntityNameToSessionFactoryHashCode(string[] entityNames, int sessionFactoryHashCode)

    {

        foreach (var entityName in entityNames)

        {

            this.entityDictionary[entityName] = sessionFactoryHashCode

        }

    }

}

根据类型获取ISession:

public interface ISessionHolder : IDisposable

{

    ISession GetSessionForEntity<TEntity>() where TEntity : IEntity

}

 public class SessionHolder : ISessionHolder, IUnitOfWork

{

    private readonly ISessionFactoryHolder factoryHolder

    private IDictionary<int, ISession> sessionDictionary

    public SessionHolder(ISessionFactoryHolder factoryHolder)

    {

        Asserts.IsNotNull(factoryHolder, "factoryHolder")

        this.factoryHolder = factoryHolder

        this.sessionDictionary = new Dictionary<int, ISession>()

    }

    #region ISessionHolder Members

    public ISession GetSessionForEntity<TEntity>() where TEntity : IEntity

    {

        if (this.sessionDictionary.ContainsKey(this.GetFactoryHashCode<TEntity>()) == false)

        {

            this.sessionDictionary[this.GetFactoryHashCode<TEntity>()] = this.OpenNewSession<TEntity>()

        }

        return this.sessionDictionary[this.GetFactoryHashCode<TEntity>()]

    }

    #endregion

    #region IDisposable Members

    //Dispose Code

     #endregion

    #region IUnitOfWork

    //IUnitOfWork

    #endregion

    private ISessionFactory GetFactory<TEntity>() where TEntity : IEntity

    {

        return this.factoryHolder.GetSessionFactoryForEntity<TEntity>()

    }

    private int GetFactoryHashCode<TEntity>() where TEntity : IEntity

    {

        return this.GetFactory<TEntity>().GetHashCode()

    }

    private ISession OpenNewSession<TEntity>() where TEntity : IEntity

    {

        return this.GetFactory<TEntity>().OpenSession()

    }

}

Repository:

public interface IRepository<TEntity> where TEntity : IEntity

{

    void Save(TEntity entity)

    //......

}

public class NHibernateRepository<TEntity> : IRepository<TEntity> where TEntity : IEntity

{

    private readonly ISessionHolder sessionHolder

    public NHibernateRepository(ISessionHolder sessionHolder)

    {

        Asserts.IsNotNull(sessionHolder, "sessionHolder")

        this.sessionHolder = sessionHolder

    }

    protected virtual ISession Session

    {

        get

        {

            return this.sessionHolder.GetSessionForEntity<TEntity>()

        }

    }

    public override void Save(TEntity entity)

    {

        this.Session.Save(entity)

    }

    //......

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6704131.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-27
下一篇2023-03-27

发表评论

登录后才能评论

评论列表(0条)

    保存