This is probably not so simple, but here's my trick. Create a unit of work that basically ends the session and transaction and saves it for the request, and also commits or rolls back after the request is completed.
public interface IUnitOfWork : IDisposable { ISession Session { get; } void Commit(); void Rollback(); }
The implementation may look like this:
public class UnitOfWork : IUnitOfWork { private readonly ITransaction _tx; public ISessionFactory SessionFactory { get; set; } public UnitOfWork() { SessionFactory = ObjectFactory.GetNamedInstance<ISessionFactory>(Keys.SessionFactoryName); Session = SessionFactory.OpenSession(); _tx = Session.BeginTransaction(); } public UnitOfWork(ISessionFactory sessionFactory) { SessionFactory = sessionFactory; Session = SessionFactory.OpenSession(); _tx = Session.BeginTransaction(); } public ISession Session { get; private set; } public void Commit() { if (_tx.IsActive) _tx.Commit(); } public void Rollback() { _tx.Rollback(); } }
Just remove the unit of work with endrequest.
source share