打开/关闭NHibernate的会议 [英] Open/Close NHibernate Session

查看:76
本文介绍了打开/关闭NHibernate的会议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Web应用程序中使用FluentNHibernate,我创建了一个单身的SessionFactory类具有以下能力:

  SessionFactory.Instance //返回ISessionFactory 

它是常见的打开/关闭会话如下/最佳做法?

 使用(ISession的会话= SessionFactory.Instance.OpenSession())
{
使用(ITransaction交易= session.BeginTransaction())
{
//一些操作
}
}

上面的代码将住在各自的储存库类给定的实体。



我注意到有创建的一个主题的HttpModule打开在启动和应用程序的停止了会议,但我不知道这是否是情境或更常见。



更新



与HTTP模块展望未来,我也有类似的想法:



通过存储库类,我基本上是做以下(配置使用WebSessionContext):

 使用(ISession的会话= SessionFactory.Instance.GetCurrentSession())
{
使用(ITransaction事务= session.BeginTransaction())
{
//一些操作
}
}


解决方案

如果您使用的是Web应用程序,我会建议使用的HttpModule打开一个会话并关闭它在一个请求周期。该会话工厂我可能会实例上的Application_Start。



有关信息库对象我将在本届会议通过构造函数传递给它。



我个人不认为一个资源库对象,以决定如何处理会话做的足够信息。


Using FluentNHibernate in a web application, I've created a singleton SessionFactory class to have the ability of the following:

SessionFactory.Instance //returns ISessionFactory

Is it common/best practice to open/close sessions as follows?

using(ISession session = SessionFactory.Instance.OpenSession())
{
    using(ITransaction transaction = session.BeginTransaction())
    {
    	//some operation
    }
}

The above code would live in the respective repository classes for a given entity.

I've noticed there is a theme of creating a HttpModule to open the session at the start and stop of the application, but I'm wondering if this is situational or more common.

UPDATE

Going forward with the HttpModule, I have a similar thought:

With a repository class, I'm basically doing the following(config uses WebSessionContext):

using(ISession session = SessionFactory.Instance.GetCurrentSession())
{
    using(ITransaction transaction = session.BeginTransaction())
    {
        //some operation
    }
}

解决方案

If you're using a web application I would recommend using the HttpModule to open a session and close it on a request cycle. The session factory I would probably instantiate on the Application_Start.

For the repository objects I would pass the session to it via a constructor.

I personally don't think a repository object has enough information in order to decide what to do with the session.

这篇关于打开/关闭NHibernate的会议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆