public void Can_attach()
        {
            var customer = new Customer
            {
                FirstName = "Jane",
                LastName = "Doe"
            };
            var session = NHTestUtil.OrdersDomainFactory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            session.Save(customer);
            transaction.Commit();
            session.Evict(customer); //Detching from owning session
            session.Dispose(); //Auto flush

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new NHRepository<Customer,int>();
                repository.Attach(customer);
                customer.LastName = "Changed";
                scope.Commit(); //Should change since the customer was attached to repository.
            }

            using (var testData = new NHTestData(NHTestUtil.OrdersDomainFactory.OpenSession()))
            {
                Customer savedCustomer = null;
                testData.Batch(x => savedCustomer = x.GetCustomerById(customer.CustomerID));
                Assert.IsNotNull(savedCustomer);
                Assert.AreEqual(savedCustomer.LastName, "Changed");
            }
        }
        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();
                }
            }
        }
        public void NHUOW_Issue_6_Replication()
        {
            var readCustomerFunc = new Func <Customer>(() =>
            {
                using (var scope = new UnitOfWorkScope())
                {
                    var customer = new NHRepository <Customer>().First();
                    scope.Commit();
                    return(customer);
                }
            });

            var updateCustomerFunc = new Func <Customer, Customer>(customer =>
            {
                using (var scope = new UnitOfWorkScope())
                {
                    var repository = new NHRepository <Customer>();
                    repository.Attach(customer);
                    scope.Commit();
                    return(customer);
                }
            });

            var newCustomerName = "Changed" + new Random().Next(0, int.MaxValue);

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

                using (var masterScope = new UnitOfWorkScope())
                {
                    using (var childScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew))
                    {
                        var customer = readCustomerFunc();
                        customer.FirstName = newCustomerName;
                        updateCustomerFunc(customer);
                        childScope.Commit();
                    }
                } //Rollback

                var checkCustomer = readCustomerFunc();
                Assert.That(checkCustomer.FirstName, Is.EqualTo(newCustomerName));
            }
        }
        public void NHUOW_Issue_6_Replication()
        {
            var readCustomerFunc = new Func<Customer>(() =>
            {
                using (var scope = new UnitOfWorkScope())
                {
                    var customer = new NHRepository<Customer>().First();
                    scope.Commit();
                    return customer;
                }
            });

            var updateCustomerFunc = new Func<Customer, Customer>(customer =>
            {
                using (var scope = new UnitOfWorkScope())
                {
                    var repository = new NHRepository<Customer>();
                    repository.Attach(customer);
                    scope.Commit();
                    return customer;
                }
            });

            var newCustomerName = "Changed" + new Random().Next(0, int.MaxValue);
            using (var testData = new NHTestDataGenerator(Factory.OpenSession()))
            {
                testData.Batch(actions => actions.CreateCustomer());

                using (var masterScope = new UnitOfWorkScope())
                {
                    using (var childScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew))
                    {
                        var customer = readCustomerFunc();
                        customer.FirstName = newCustomerName;
                        updateCustomerFunc(customer);
                        childScope.Commit();
                    }
                } //Rollback

                var checkCustomer = readCustomerFunc();
                Assert.That(checkCustomer.FirstName, Is.EqualTo(newCustomerName));
            }
        }
        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();
                }
            }
        }
Esempio n. 6
0
 public void Attach(Employee employee)
 {
     rep.Attach(employee);
 }