NHibernate 连接多数据库怎么配置
2016-08-24 · 知道合伙人数码行家
关注
展开全部
在开发一些项目时,会使用到多个数据库。例如类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);
}
//......
}
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家国内优质海外HTTP代理商,拥有一个庞大的IP资源池,覆盖200多个地区,IP数量大且匿名度高。其优点还包括超高并发、稳定高效、技术服务等特点,同时提供HTTP、HTTPS以及SOCKS5协议支持。此外,Sto...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询