views:

48

answers:

1

What is the difference between using

using (var scope = new TransactionScope(TransactionScopeOption.Required))
        {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(entity);
                transaction.Commit();
            }
        }
        scope.Complete();
        } 

and simply using ?

using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(entity);
                transaction.Commit();
            }
        }

What are the advantages and disadvanteges and when they are appropriate to use?

A: 

Since you are using both TransactonScope and NHibernate transaction you have some kind of duplicated logic. If you want to operate with simple SQL Transaction than you should use NHibernate transaction.

TransactionScope class is designed to use in any general transaction scenario. For example it is used in EntityFramework. While using NHibernate you don't need it, but it becames very useful when you need to implement custom transaction mechanism.

Restuta
That's not correct. TransactionScope is to manage a distributed transaction, NH ITransaction is to manage the NH transaction and is always needed.
Diego Mijelshon
Please clarify your comment to make me able to correct my answer. What exactly is not correct?
Restuta
Might I suggest having a look at the ncommon framework (http://github.com/riteshrao/ncommon) it demonstrates an ORM-agnostic unit of work implementation that uses a TransactionScope
DanP
Good example, but useless in real life applications.
Restuta