When I use autofac, I use the same method with the container, but instead of passing the same session to my / DAO repository objects, I pass UnitOfWork, which is the container. This unit of work has this in the constructor.
private readonly ISession _session; private ITransaction _transaction; public UnitOfWork(ISession session) { _session = session; _transaction = session.BeginTransaction(); }
And utility:
public void Dispose() { try { if (_transaction != null && !_transaction.WasCommitted && !_transaction.WasRolledBack) _transaction.Commit(); _transaction = null; } catch (Exception) { Rollback(); throw; } }
I (ab) use autofac's deterministic material for deletion to handle this, and I kind of love it.
Another thing is that I am mainly focused only on ASPNet and made an informed decision that the transaction is tied to a web request. Therefore, the transaction to the web request template.
Because of this, I can do this error handling code in the IHttpModule module:
void context_Error(object sender, System.EventArgs e) { _containerProvider.RequestContainer.Resolve<IUnitOfWork>().Rollback(); }
I didn't look too closely at NHibernate.Burrow, but I'm sure there is something out there that does most of this.
Min
source share