예제 #1
0
            public void CorrectlyCommitsTransaction()
            {
                using (var uow = new UnitOfWork<TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository<IDbContextCustomerRepository>();
                    var productRepository = uow.GetRepository<IDbContextProductRepository>();
                    var orderRepository = uow.GetRepository<IDbContextOrderRepository>();

                    uow.BeginTransaction();

                    var customer = EFTestHelper.CreateCustomer(451);
                    customerRepository.Add(customer);

                    var product = EFTestHelper.CreateProduct(451);
                    productRepository.Add(product);

                    var order = new DbContextOrder { OrderCreated = DateTime.Now, Amount = 1, CustomerId = 451, ProductId = 451 };
                    orderRepository.Add(order);

                    uow.CommitTransaction();
                }

                using (var uow = new UnitOfWork<TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository<IDbContextCustomerRepository>();
                    var productRepository = uow.GetRepository<IDbContextProductRepository>();
                    var orderRepository = uow.GetRepository<IDbContextOrderRepository>();

                    var customer = customerRepository.GetByKey(451);
                    Assert.IsNotNull(customer);

                    var product = productRepository.GetByKey(451);
                    Assert.IsNotNull(product);

                    var order = orderRepository.FirstOrDefault(x => x.CustomerId == 451 && x.ProductId == 451);
                    Assert.IsNotNull(order);
                }
            }
예제 #2
0
            public void CorrectlyRollbacksTransactionWhenAnErrorOccursWhileSaving()
            {
                using (var uow = new UnitOfWork<TestDbContextContainer>())
                {
                    var orderRepository = uow.GetRepository<IDbContextOrderRepository>();

                    uow.BeginTransaction();

                    var order = new DbContextOrder { Amount = 1, CustomerId = 999, ProductId = 999 };
                    orderRepository.Add(order);

                    try
                    {
                        uow.CommitTransaction();

                        Assert.Fail("Expected an exception");
                    }
                    catch (Exception)
                    {
                        Assert.IsFalse(uow.IsInTransaction);
                    }
                }
            }