展开全部
在开发一些项目时,会使用到多个数据库e5a48de588b662616964757a686964616f31333361313338。例如类A保存在数据库A,类B保存在数据库B。NHibernate在BuildSessionFactory之后,ISessionFactory就不能改变数据库的连接,即是说一个ISessionFactory只能对应一个数据库连接。但NHibernate可以在同一个应用中实例化多个ISessionFactory。实例化多个ISessionFactory,并让类A或类B找到自己所对应的ISessionFactory,获取ISession,即可实现多数据库连接。
如何通过类型获取ISessionFactory呢?ISessionFactory的Statistics.EntityNames中保存了所有映射了的实体类的类名。我们可以判断实体类的类名是否在EntityNames中,确定实体类所对应的ISessionFactory。
根据类型获取ISessionFactory:publicinterfaceISessionFactoryHolder
{
ISessionFactoryGetSessionFactoryForEntity()whereTEntity:IEntity;
}
publicclassSessionFactoryHolder:ISessionFactoryHolder
{
privateIDictionaryentityDictionary;
privateIDictionaryfactoryDictionary;
publicSessionFactoryHolder()
{
this.entityDictionary=newDictionary();
this.factoryDictionary=newDictionary();
}
#regionISessionFactoryHolderMembers
publicISessionFactoryGetSessionFactoryForEntity()whereTEntity:IEntity
{
inthashCode=0;
Asserts.Assert(
this.EntityInDictionary(typeof(TEntity).FullName,outhashCode)==false
,string.Format("Nopersisterfor:{0}",typeof(TEntity).FullName));
returnthis.factoryDictionary[hashCode];
}
#endregion
publicvoidRegisterSessionFactory(ISessionFactorysessionFactory)
{
Asserts.IsNotNull(sessionFactory,"sessionFactory");
this.factoryDictionary[sessionFactory.GetHashCode()]=sessionFactory;
this.MapingEntityNameToSessionFactoryHashCode(sessionFactory.Statistics.EntityNames
,sessionFactory.GetHashCode());
}
privateboolEntityInDictionary(stringentityName,outintsessionFactoryHashCode)
{
returnthis.entityDictionary.TryGetValue(entityName,outsessionFactoryHashCode);
}
privatevoidMapingEntityNameToSessionFactoryHashCode(string[]entityNames,intsessionFactoryHashCode)
{
foreach(varentityNameinentityNames)
{
this.entityDictionary[entityName]=sessionFactoryHashCode;
}
}
}
根据类型获取ISession:publicinterfaceISessionHolder:IDisposable
{
ISessionGetSessionForEntity()whereTEntity:IEntity;
}
publicclassSessionHolder:ISessionHolder,IUnitOfWork
{
privatereadonlyISessionFactoryHolderfactoryHolder;
privateIDictionarysessionDictionary;
publicSessionHolder(ISessionFactoryHolderfactoryHolder)
{
Asserts.IsNotNull(factoryHolder,"factoryHolder");
this.factoryHolder=factoryHolder;
this.sessionDictionary=newDictionary();
}
#regionISessionHolderMembers
publicISessionGetSessionForEntity()whereTEntity:IEntity
{
if(this.sessionDictionary.ContainsKey(this.GetFactoryHashCode())==false)
{
this.sessionDictionary[this.GetFactoryHashCode()]=this.OpenNewSession();
}
returnthis.sessionDictionary[this.GetFactoryHashCode()];
}
#endregion
#regionIDisposableMembers
//DisposeCode
#endregion
#regionIUnitOfWork
//IUnitOfWork
#endregion
privateISessionFactoryGetFactory()whereTEntity:IEntity
{
returnthis.factoryHolder.GetSessionFactoryForEntity();
}
privateintGetFactoryHashCode()whereTEntity:IEntity
{
returnthis.GetFactory().GetHashCode();
}
privateISessionOpenNewSession()whereTEntity:IEntity
{
returnthis.GetFactory().OpenSession();
}
}
Repository:publicinterfaceIRepositorywhereTEntity:IEntity
{
voidSave(TEntityentity);
//......
}
publicclassNHibernateRepository:IRepositorywhereTEntity:IEntity
{
privatereadonlyISessionHoldersessionHolder;
publicNHibernateRepository(ISessionHoldersessionHolder)
{
Asserts.IsNotNull(sessionHolder,"sessionHolder");
this.sessionHolder=sessionHolder;
}
protectedvirtualISessionSession
{
get
{
returnthis.sessionHolder.GetSessionForEntity();
}
}
publicoverridevoidSave(TEntityentity)
{
this.Session.Save(entity);
}
//......
}