這是基於:http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx幫助理解這個NHibernate的會話管理代碼片斷
代碼是在這裏:https://gist.github.com/852307
我的問題是,在DAO:
//Example of dao
public class Dao<T> : IDao<T>
{
private readonly ISessionFactory sessionFactory;
public Dao(ISessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public void Save(T transient)
{
sessionFactory.GetCurrentSession().Save(transient);
}
}
我猜ISessionFactory獲得的使用windsor自動連線了嗎?
public class NHibernateInstaller : IWindsorInstaller
{
#region IWindsorInstaller Members
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<ISessionFactory>()
.UsingFactoryMethod(k => BuildSessionFactory()));
container.Register(Component.For<NHibernateSessionModule>());
container.Register(Component.For<ISessionFactoryProvider>().AsFactory());
container.Register(Component.For<IEnumerable<ISessionFactory>>()
.UsingFactoryMethod(k => k.ResolveAll<ISessionFactory>()));
HttpContext.Current.Application[SessionFactoryProvider.Key]
= container.Resolve<ISessionFactoryProvider>();
}
#endregion
public ISessionFactory BuildSessionFactory() { ... }
}
但在DAO,當方法被調用:
sessionFactory.GetCurrentSession()
請問這個片段,並在打開的會話工作的HttpModule?
由於GetCurrentSession是一個內置方法,我沒有看到GetCurrentSession()掛接到由HttpModule打開的會話中?
所以在一個web應用程序中,它保存在哪裏?在HttpContext.Current.Request.Items? – codecompleting
從鏈接中的源代碼看起來它將它存儲在HttpContext.Current.Items [CurrentSessionContextKey]中。 –