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);
}
//......
}
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询