Using FluentNHibernate in a web application, I created a single-class SessionFactory class to be able to do the following:
SessionFactory.Instance
Is it common / best practice to open / close sessions as follows?
using(ISession session = SessionFactory.Instance.OpenSession())
{
using(ITransaction transaction = session.BeginTransaction())
{
}
}
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())
{
}
}
source
share