Most of our current NHibernate saving methods look like below:

        public void SaveCustomer(CustomerEntity customer)
        {
            var transaction = GetActiveTransaction();

            transaction.Begin();
            try
            {
                // some code
                Session.SaveOrUpdate(customer);

                transaction.Commit();
            }
            catch (Exception e)
            {
                transaction.Rollback();
                throw e;
            }
        }

this works great if you need to save just Customer or just Vendor. But in recent time we got kindof challenge to save Customer and Vendor at once in scope of one transaction for piece of functionality, and at the same time method should work for another piece of functionality.
For this we change our methods as following:

        public void SaveVendor(VendorEntity vendor)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required)) // if there is no transaction scope, new will be created...
            {
                using (var transaction = Session.BeginTransaction())
                {
                    // some code
                    Session.SaveOrUpdate(vendor);

                    transaction.Commit();
                }
                scope.Complete();
            }
        }

Now we can utilize our methods higher in the call stack:

        public void SaveBunchOfInformationAtOnce(CustomerEntity customer, VendorEntity vendor)
        {
            try
            {
                using(var scope = new TransactionScope(TransactionScopeOption.RequiresNew)) // always new transaction will be created
                {
                    CustomersRepository.SaveCustomer(customer);

                    VendorsRepository.SaveVendor(vendor);

                    scope.Complete();
                }
            }
            catch(Exception e)
            {
                // log errors...
                throw;
            }
        }

This all works just fine both for existing method calls and for new one. Hope it helps.
Next thing that I might need to put into place is to distribute transactions through WCF services. That should be fun.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone