这是将NHibernate Isession作为PerWebRequest处理的好解决方案 [英] Is this a good solution to handle NHibernate Isession as PerWebRequest
问题描述
我一直在为NHibernate会话管理而苦苦挣扎,现在最终有了两种可能的解决方案来满足每个Web请求的会话.
我正在ASPNET mvc项目中使用Windsor for IoC
第一个解决方案是在begin_request中打开一个会话,然后在end_request中再次关闭/处置它.在温莎的设置中,我将拥有 container.Register(Component.For().UsingFactoryMethod(()=> SessionFactory.GetCurrentSession()).LifeStyle.Transient;
此解决方案为每个请求创建会话,并通过GetCurrentSession共享它.
第二种解决方案是像使用温莎一样
container.Register(Component.For().UsingFactoryMethod(()=> SessionFactory.OpenSession()).LifeStyle.PerWebRequest);
这个灵魂还为每个Web请求提供了一个会话,并支持构造函数注入. simpel比较简单,但是我需要第二意见.
请让我知道您想使用什么,
致以最诚挚的问候 拉斯穆斯我使用工作单元,该工作单元是根据容器中每个Web请求配置的.工作单元不仅创建会话,而且还提交并回滚事务.使用工作单元的主要原因是使数据库更稳定.有关工作单元模式的更多信息,请参见 解决方案
I use a unit of work, which I configure per web request in the container. The unit of work does not just create a session, but it also commits and rolls back transactions. The main reason to use a unit of work is to make the database more stable. More information about the unit of work pattern can be found on here.
public interface INHiberanteUnitOfWork
{
ISession session { get; }
void Commit();
void RollBack();
}
这篇关于这是将NHibernate Isession作为PerWebRequest处理的好解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!