Open / Close NHibernate Session

Using FluentNHibernate in a web application, I created a single-class SessionFactory class to be able to do 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 will live in the corresponding repository classes for this object.

I noticed that there is a topic of creating an HttpModule to open a session at the beginning and at the end of an application, but I am wondering if this is situational or more common.

UPDATE

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

With the repository class, I basically do the following (config uses WebSessionContext):

using(ISession session = SessionFactory.Instance.GetCurrentSession())
{
    using(ITransaction transaction = session.BeginTransaction())
    {
        //some operation
    }
}
+3
source share
2

-, HttpModule . factory , , Application_Start.

.

, , , .

+9

,

  • session-per-request-with-detached-objects

, Doa/.

. < , .

Nhibernate ep 13, Asp.Net

+8

Source: https://habr.com/ru/post/1717869/


All Articles