Hi Jason,
Emmm, getting different approaches to this. I'm generally using the
approach defined in scott millets
asp.net professional design patterns
book but it only seems to work with NH managed keys. It gets the
session via a static getcurrentsession method which is tied to the
current request via httpcontext items and calls save/saveorupdate/
delete on that via a contrete implementation and then wraps
getcurrentsession.commit in a transaction & rollback. so it has no
wraps around the various currentsession.save/delete etc.
What I'm now doing is something like below and it's working fine with
auto increment PKs.
As an example in a service method i have code like the following
_unitOfWork.BeginTransaction();
..do work on object a
..do work on object b
_unitOfWork.RegisterNew(a);
_unitOfWork.RegisterNew(b);
_unitOfWork.Commit();
_unitofwork looks like this:
public void RegisterAmended(IAggregateRoot entity)
{
try
{
SessionFactory.GetCurrentSession().SaveOrUpdate(entity);
}
catch (Exception ex)
{
SessionFactory.GetCurrentSession().Transaction.Rollback();
throw;
}
}
public void RegisterNew(IAggregateRoot entity)
{
try
{
SessionFactory.GetCurrentSession().Save(entity);
}
catch (Exception ex)
{
SessionFactory.GetCurrentSession().Transaction.Rollback();
throw;
}
}
public void RegisterRemoved(IAggregateRoot entity)
{
try
{
SessionFactory.GetCurrentSession().Delete(entity);
}
catch (Exception ex)
{
SessionFactory.GetCurrentSession().Transaction.Rollback();
throw;
}
}
public void Commit()
{
try
{
SessionFactory.GetCurrentSession().Transaction.Commit();
}
catch (Exception ex)
{
SessionFactory.GetCurrentSession().Transaction.Rollback();
throw;
}
}
public void BeginTransaction()
{
SessionFactory.GetCurrentSession().BeginTransaction();
}
I 'think' if I was using NH managed keys, I would not have to wrap the
various amend calls in transactions because there would be no database
interaction until commit. What you think?