public void when_ambient_transaction_is_running_and_a_previous_scope_rollsback_new_scope_still_works()
        {
            using (var testData = new NHTestDataGenerator(Factory.OpenSession()))
            {
                testData.Batch(actions => actions.CreateCustomer());

                string oldCustomerName;
                var    newCustomerName = "NewCustomer" + new Random().Next(0, int.MaxValue);
                var    newCustomer     = new Customer
                {
                    FirstName = newCustomerName,
                    LastName  = "Save",
                    Address   = new Address
                    {
                        StreetAddress1 = "This record was inserted via a test",
                        City           = "Fictional City",
                        State          = "LA",
                        ZipCode        = "00000"
                    }
                };

                using (var ambientScope = new TransactionScope())
                {
                    using (var firstUOW = new UnitOfWorkScope())
                    {
                        var customer = new NHRepository <Customer>().First();
                        oldCustomerName    = customer.FirstName;
                        customer.FirstName = "Changed";
                    }  //Rollback

                    using (var secondUOW = new UnitOfWorkScope())
                    {
                        new NHRepository <Customer>().Add(newCustomer);
                        secondUOW.Commit();
                    }
                }

                using (var scope = new UnitOfWorkScope())
                {
                    var repository = new NHRepository <Customer>();
                    Assert.That(repository.First().FirstName, Is.EqualTo(oldCustomerName));
                    Assert.That(repository.Where(x => x.FirstName == newCustomerName).Count(), Is.GreaterThan(0));
                    repository.Attach(newCustomer);
                    repository.Delete(newCustomer);
                    scope.Commit();
                }
            }
        }
Exemplo n.º 2
0
        public void Nested_UnitOfWork_With_Different_Transaction_Compatibility_Works()
        {
            var changedShipDate  = DateTime.Now.AddDays(1);
            var changedOrderDate = DateTime.Now.AddDays(2);

            using (var testData = new NHTestDataGenerator(Factory.OpenSession()))
            {
                testData.Batch(actions =>
                               actions.CreateOrderForCustomer(actions.CreateCustomer()));

                int orderId;
                using (new UnitOfWorkScope())
                {
                    var ordersRepository = new NHRepository <Order>();
                    orderId = ordersRepository.Select(x => x.OrderID).First();
                }

                Assert.NotNull(orderId);
                using (new UnitOfWorkScope())
                {
                    var outerRepository = new NHRepository <Order>();
                    var outerOrder      = outerRepository.Where(x => x.OrderID == orderId).First();
                    outerOrder.OrderDate = changedOrderDate;

                    using (var innerScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew))
                    {
                        var innerRepository = new NHRepository <Order>();
                        var innerOrder      = innerRepository.Where(x => x.OrderID == orderId).First();
                        innerOrder.ShipDate = changedShipDate;
                        innerScope.Commit();
                    }
                }

                using (new UnitOfWorkScope())
                {
                    var ordersRepository = new NHRepository <Order>();
                    var order            = ordersRepository.First();
                    Assert.That(order.OrderDate, Is.Not.EqualTo(changedOrderDate));
                    Assert.That(order.ShipDate, Is.Not.EqualTo(changedShipDate));
                }
            }
        }
        public void when_ambient_transaction_is_running_and_a_previous_scope_rollsback_new_scope_still_works()
        {
            using (var testData = new NHTestDataGenerator(Factory.OpenSession()))
            {
                testData.Batch(actions => actions.CreateCustomer());

                string oldCustomerName;
                var newCustomerName = "NewCustomer" + new Random().Next(0, int.MaxValue);
                var newCustomer = new Customer
                {
                    FirstName = newCustomerName,
                    LastName = "Save",
                    Address = new Address
                    {
                        StreetAddress1 = "This record was inserted via a test",
                        City = "Fictional City",
                        State = "LA",
                        ZipCode = "00000"
                    }
                };

                using (var ambientScope = new TransactionScope())
                {
                    using (var firstUOW = new UnitOfWorkScope())
                    {
                        var customer = new NHRepository<Customer>().First();
                        oldCustomerName = customer.FirstName;
                        customer.FirstName = "Changed";
                    }  //Rollback

                    using (var secondUOW = new UnitOfWorkScope())
                    {

                        new NHRepository<Customer>().Add(newCustomer);
                        secondUOW.Commit();
                    }
                }

                using (var scope = new UnitOfWorkScope())
                {
                    var repository = new NHRepository<Customer>();
                    Assert.That(repository.First().FirstName, Is.EqualTo(oldCustomerName));
                    Assert.That(repository.Where(x => x.FirstName == newCustomerName).Count(), Is.GreaterThan(0));
                    repository.Attach(newCustomer);
                    repository.Delete(newCustomer);
                    scope.Commit();
                }
            }
        }
Exemplo n.º 4
0
        public void Nested_UnitOfWork_With_Different_Transaction_Compatibility_Works()
        {
            var changedShipDate = DateTime.Now.AddDays(1);
            var changedOrderDate = DateTime.Now.AddDays(2);

            using (var testData = new NHTestDataGenerator(Factory.OpenSession()))
            {
                testData.Batch(actions =>
                               actions.CreateOrderForCustomer(actions.CreateCustomer()));

                int orderId;
                using (new UnitOfWorkScope())
                {
                    var ordersRepository = new NHRepository<Order>();
                    orderId = ordersRepository.Select(x => x.OrderID).First();
                }

                Assert.NotNull(orderId);
                using (new UnitOfWorkScope())
                {
                    var outerRepository = new NHRepository<Order>();
                    var outerOrder = outerRepository.Where(x => x.OrderID == orderId).First();
                    outerOrder.OrderDate = changedOrderDate;

                    using (var innerScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew))
                    {
                        var innerRepository = new NHRepository<Order>();
                        var innerOrder = innerRepository.Where(x => x.OrderID == orderId).First();
                        innerOrder.ShipDate = changedShipDate;
                        innerScope.Commit();
                    }
                }

                using (new UnitOfWorkScope())
                {
                    var ordersRepository = new NHRepository<Order>();
                    var order = ordersRepository.First();
                    Assert.That(order.OrderDate, Is.Not.EqualTo(changedOrderDate));
                    Assert.That(order.ShipDate, Is.Not.EqualTo(changedShipDate));
                }
            }
        }