public void Should_Insert_And_Update_In_Separetad_Transactions()
        {
            Customer customer = null;

            try
            {
                customer = Singleton.TestDataProvider.CreateNewRandomCustomer();
                using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
                {
                    unitOfWork.BeginTransaction();

                    ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                    customerRepository.Save(customer);
                    unitOfWork.Commit();
                }

                string updatedName = Singleton.TestDataProvider.ProvideRandomString(50);
                using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
                {
                    unitOfWork.BeginTransaction();

                    ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                    var customer2 = customerRepository.FirstOrDefault(customer.Id);
                    customer2.Name = updatedName;
                    customerRepository.Save(customer2);
                    unitOfWork.Commit();
                }

                using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
                {
                    ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                    var updatedCustomer = customerRepository.AsQueryable(f => f.Name == updatedName).FirstOrDefault();
                    Assert.IsNotNull(updatedCustomer);
                    Assert.AreEqual(customer.Code, updatedCustomer.Code);
                }
            }
            finally
            {
                if (customer != null)
                {
                    using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
                    {
                        unitOfWork.Session.Delete(customer);
                        unitOfWork.Commit();
                    }
                }
            }
        }
        public void Should_Use_Two_Transactions_In_One_TransactionScope()
        {
            string test_name = Guid.NewGuid().ToString();

            using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
            {
                ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                var customer = customerRepository.AsQueryable(f => f.Name == test_name).FirstOrDefault();
                Assert.IsNull(customer);
            }

            using (new TransactionScope(TransactionScopeOption.Required))
            {
                using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
                {
                    unitOfWork.BeginTransaction();

                    ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                    customerRepository.Save(
                        new Customer
                    {
                        Code      = "test_code",
                        Name      = test_name,
                        Type      = CustomerType.LoyalCustomers,
                        CreatedOn = DateTime.Now,
                        DeletedOn = null
                    });

                    unitOfWork.Commit();
                }

                using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
                {
                    unitOfWork.BeginTransaction();

                    ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                    var customer = customerRepository.AsQueryable(f => f.Name == test_name).FirstOrDefault();
                    Assert.IsNotNull(customer);

                    unitOfWork.Commit();
                }
            }
        }
        public void Should_Use_Two_Transactions_In_One_TransactionScope()
        {
            string test_name = Guid.NewGuid().ToString();

            using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
            {
                ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                var customer = customerRepository.AsQueryable(f => f.Name == test_name).FirstOrDefault();
                Assert.IsNull(customer);
            }

            using (new TransactionScope(TransactionScopeOption.Required))
            {
                using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
                {
                    unitOfWork.BeginTransaction();

                    ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                    customerRepository.Save(
                        new Customer
                        {
                            Code = "test_code",
                            Name = test_name,
                            Type = CustomerType.LoyalCustomers,
                            CreatedOn = DateTime.Now,
                            DeletedOn = null
                        });

                    unitOfWork.Commit();
                }

                using (IUnitOfWork unitOfWork = new UnitOfWork(Singleton.SessionFactoryProvider))
                {
                    unitOfWork.BeginTransaction();

                    ICustomerRepository customerRepository = new CustomerRepository(unitOfWork);
                    var customer = customerRepository.AsQueryable(f => f.Name == test_name).FirstOrDefault();
                    Assert.IsNotNull(customer);

                    unitOfWork.Commit();
                }
            }
        }
        public CustomerOrderDetailResponse GetCustomerOrderDetails(CustomerOrderDetailRequest request)
        {
            List <CustomerOrderDetailDto> customerOrderDetails = new List <CustomerOrderDetailDto>();
            CustomerRepository            customers            = northWindUnitOfWork.CustomerRepository;
            OrderRepository       orders       = northWindUnitOfWork.OrderRepository;
            OrderDetailRepository orderDetails = northWindUnitOfWork.OrderDetailRepository;
            ProductRepository     products     = northWindUnitOfWork.ProductRepository;

            var testQuery = from c in customers.AsQueryable()
                            join o in orders.Table() on c.CustomerId equals o.CustomerId
                            join od in orderDetails.Table() on o.OrderId equals od.OrderId
                            join p in products.Table() on od.ProductId equals p.ProductId
                            orderby o.OrderId descending
                            orderby c.CompanyName ascending
                            select new CustomerOrderDetailDto()
            {
                OrderId      = o.OrderId,
                ProductId    = p.ProductId,
                ProductName  = p.ProductName,
                UnitPrice    = od.UnitPrice,
                Quantity     = od.Quantity,
                Discount     = od.Discount,
                CompanyName  = c.CompanyName,
                ContactName  = c.ContactName,
                Phone        = c.Phone,
                OrderDate    = o.OrderDate,
                RequiredDate = o.RequiredDate,
                ShippedDate  = o.ShippedDate
            };

            customerOrderDetails = testQuery.Top(1).NoLock().ToList();
            return(new CustomerOrderDetailResponse()
            {
                CustomerOrderDetails = customerOrderDetails
            });
        }