I just start learning castle windsor. Have a quesiton about if I should add another unit of work on top of Nhibernate ISession.
I found this on windsor tutorial.
http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Six-Persistence-Layer.ashx
"There's one important, although invisible effect of what we just did. By registering the components we didn't just tell Windsor how to create them. Windsor will also take care of properly destroying the instances for us, thus taking care of managing their full lifetime. In layman terms, Windsor will dispose both objects when they are no longer being used. This means it will flush the changes we made to ISession to the database for us, and it will clean up the ISessionFactory. And we get all of this for free. "
it sounds that we dont need to commit our changes to the Database, Windsor will take care of that. I am assuming Windsor will do that after the page is closed or fully loaded.
now I see other people add another Unit of work on top of Nhibrenate like this one.
Just curious which one is considered the best practice?
UnitOfWork unitOfWork = new UnitOfWork(session);
Repository<Truck> repository = new Repository<Truck>(unitOfWork.Session);
Truck truck = CreateTruck(string.Format("Truck {0}", i + 1), 1000);
repository.Add(truck);
unitOfWork.Commit();
namespace RepositoryPattern.Data.Orm.nHibernate
{
public class UnitOfWork : IUnitOfWork
{
public ISession Session { get; private set; }
private readonly ITransaction _transaction;
public UnitOfWork(ISession session)
{
Session = session
Session.FlushMode = FlushMode.Auto;
_transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void Commit()
{
if (!_transaction.IsActive)
{
throw new InvalidOperationException("Oops! We don't have an active transaction");
}
_transaction.Commit();
}
public void Rollback()
{
if (_transaction.IsActive)
{
_transaction.Rollback();
}
}
public void Dispose()
{
if (Session.IsOpen)
{
Session.Close();
}
}
}
}