Is this a good solution to handle NHibernate Isession as PerWebRequest

I struggled with NHibernate session management and now I got two possible solutions to satisfy a session on a web request.

I am using Windsor for IoC in an ASPNET mvc project

The first solution is to open the session in begin_request and close / delete it again in end_request. In Windsor setup, I would have container.Register (Component.For (). Using FactMethod (() => SessionFactory.GetCurrentSession ()). LifeStyle.Transient;

This solution creates a session for the request and shares it through GetCurrentSession.

The second solution is to use Windsor as

container.Register (Component.For (). UsingFactoryMethod (() => SessionFactory.OpenSession ()). LifeStyle.PerWebRequest);

This will also give me a session for every request and constructor support. This is a bit more simpel, but I need a second opinion.

Please let me know what you prefer to use,

Regards Rasmus

+3
source share
2 answers

I use the unit of work that I configure for each request in the container. A unit of work does not just create a session, but also completes transactions and rolls back transactions. The main reason for using a unit of work is to make the database more stable. More information about the device work template can be found here .

public interface INHiberanteUnitOfWork
{
    ISession session { get; }
    void Commit();
    void RollBack();
}
+1
source

. , , NHibernate.

+3

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


All Articles