public void Dispose()
 {
     if (allowedDispose)
     {
         UnityContainerDispatcher.DisposeContainer();
         scopeMapping.Remove(ScopeId);
     }
 }
 public static void Commit()
 {
     UnityContainerDispatcher.GetContainer().Resolve <IUnitOfWork>().Commit();
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer()
                                        .RegisterType(typeof(IRepository <>), typeof(Repository <>), new HierarchicalLifetimeManager())
                                        .RegisterType <IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager())
                                        .RegisterType <DbContext, RETAILContext>(new HierarchicalLifetimeManager())
                                        .RegisterType <DbContextAdapter>(new HierarchicalLifetimeManager())
                                        .RegisterType <IObjectSetFactory, DbContextAdapter>(new HierarchicalLifetimeManager())
                                        .RegisterType <IObjectContext, DbContextAdapter>(new HierarchicalLifetimeManager());

            UnityContainerDispatcher.InjectParentContainer(container);

            Mapper.CreateMap <DomainModels.Customer, Customer>();
            Mapper.CreateMap <Customer, DomainModels.Customer>();

            // =============== 测试事务回滚 ===============
            Console.ForegroundColor = ConsoleColor.Red;

            DomainModels.Customer customer1 = new DomainModels.Customer()
            {
                Name    = "Dennis Gao",
                Address = "Beijing",
                Phone   = "18888888888",
            };
            DomainModels.Customer customer2 = new DomainModels.Customer()
            {
                //Name = null, // 创造一个无效的对象,此处客户名称不能为空
                Address = "Beijing",
                Phone   = "16666666666",
            };

            try
            {
                using (var transactionScope = new TransactionScope(
                           TransactionScopeOption.RequiresNew,
                           new TransactionOptions()
                {
                    IsolationLevel = IsolationLevel.ReadUncommitted
                }))
                {
                    using (UnityContainerScope scope = UnityContainerScope.NewScope())
                    {
                        Customer entity1 = Mapper.Map <DomainModels.Customer, Customer>(customer1);
                        Customer entity2 = Mapper.Map <DomainModels.Customer, Customer>(customer2);

                        Repository.Customers.Insert(entity1);
                        Repository.Customers.Insert(entity2);
                        Repository.Commit();

                        customer1.Id = entity1.Id;
                        customer2.Id = entity2.Id;
                    }

                    transactionScope.Complete();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(FlattenException(ex));
            }

            Console.WriteLine(customer1);
            Console.WriteLine(customer2);
            Console.WriteLine("=====================================");

            // =============== 查询回滚结果 ===============
            List <DomainModels.Customer> customers = new List <DomainModels.Customer>();

            using (var transactionScope = new TransactionScope(
                       TransactionScopeOption.RequiresNew,
                       new TransactionOptions()
            {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
            {
                using (UnityContainerScope scope = UnityContainerScope.NewScope())
                {
                    List <Customer> entities = Repository.Customers.Query().ToList();

                    foreach (var entity in entities)
                    {
                        DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity);
                        customers.Add(customer);
                    }
                }
                transactionScope.Complete();
            }

            foreach (var customer in customers)
            {
                Console.WriteLine(customer);
            }

            // =============== 清理 ===============
            Console.WriteLine();
            Console.WriteLine("Press any key to clean database...");
            Console.ReadKey();

            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                List <Customer> entities1 = Repository.Customers.Query().ToList();

                foreach (var entity in entities1)
                {
                    Repository.Customers.Delete(entity);
                }

                Repository.Commit();
            }
        }
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer()
                                        .RegisterType(typeof(IRepository <>), typeof(Repository <>), new HierarchicalLifetimeManager())
                                        .RegisterType <IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager())
                                        .RegisterType <DbContext, RETAILContext>(new HierarchicalLifetimeManager())
                                        .RegisterType <DbContextAdapter>(new HierarchicalLifetimeManager())
                                        .RegisterType <IObjectSetFactory, DbContextAdapter>(new HierarchicalLifetimeManager())
                                        .RegisterType <IObjectContext, DbContextAdapter>(new HierarchicalLifetimeManager())
                                        .RegisterType <ICustomerRepository, CustomerRepository>(new HierarchicalLifetimeManager());

            UnityContainerDispatcher.InjectParentContainer(container);

            Mapper.CreateMap <DomainModels.Customer, Customer>();
            Mapper.CreateMap <Customer, DomainModels.Customer>();
            Mapper.CreateMap <DomainModels.Supplier, Supplier>();
            Mapper.CreateMap <Supplier, DomainModels.Supplier>();

            ICustomerRepository customerRepository = container.Resolve <ICustomerRepository>();

            // =============== 增 ===============
            Console.ForegroundColor = ConsoleColor.DarkRed;

            DomainModels.Customer customer1 = new DomainModels.Customer()
            {
                Name    = "Dennis Gao",
                Address = "Beijing",
                Phone   = "18888888888",
            };
            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                Customer entity = Mapper.Map <DomainModels.Customer, Customer>(customer1);

                Repository.Customers.Insert(entity);
                Repository.Commit();

                customer1.Id = entity.Id;
            }
            Console.WriteLine(customer1);

            DomainModels.Customer customer2 = new DomainModels.Customer()
            {
                Name    = "Degang Guo",
                Address = "Beijing",
                Phone   = "16666666666",
            };
            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                Customer entity = Mapper.Map <DomainModels.Customer, Customer>(customer2);

                Repository.Customers.Insert(entity);
                Repository.Commit();

                customer2.Id = entity.Id;
            }
            Console.WriteLine(customer2);

            DomainModels.Supplier supplier1 = new DomainModels.Supplier()
            {
                CompanyName  = "Microsoft",
                Address      = "Beijing",
                ContactName  = "Bill Gates",
                ContactTitle = "CEO",
            };
            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                Supplier entity = Mapper.Map <DomainModels.Supplier, Supplier>(supplier1);

                Repository.Suppliers.Insert(entity);
                Repository.Commit();

                supplier1.Id = entity.Id;
            }
            Console.WriteLine(supplier1);

            // =============== 事务 ===============
            Console.ForegroundColor = ConsoleColor.Green;

            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                customer1.Address     = Guid.NewGuid().ToString();
                customer2.Address     = Guid.NewGuid().ToString();
                supplier1.ContactName = "Steven Ballmer";

                Customer entity1 = Mapper.Map <DomainModels.Customer, Customer>(customer1);
                Customer entity2 = Mapper.Map <DomainModels.Customer, Customer>(customer2);
                Supplier entity3 = Mapper.Map <DomainModels.Supplier, Supplier>(supplier1);

                Repository.Customers.Update(entity1); // good, we are in same transaction
                Repository.Customers.Update(entity2); // good, we are in same transaction
                Repository.Suppliers.Update(entity3); // good, we are in same transaction
                Repository.Commit();
            }

            // =============== 清理 ===============
            Console.WriteLine();
            Console.WriteLine("Press any key to clean database...");
            Console.ReadKey();

            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                List <Customer> entities1 = Repository.Customers.Query().ToList();
                List <Supplier> entities2 = Repository.Suppliers.Query().ToList();

                foreach (var entity in entities1)
                {
                    Repository.Customers.Delete(entity);
                }
                foreach (var entity in entities2)
                {
                    Repository.Suppliers.Delete(entity);
                }

                Repository.Commit();
            }
        }
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer()
                                        .RegisterType(typeof(IRepository <>), typeof(Repository <>), new HierarchicalLifetimeManager())
                                        .RegisterType <IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager())
                                        .RegisterType <DbContext, RETAILContext>(new HierarchicalLifetimeManager())
                                        .RegisterType <DbContextAdapter>(new HierarchicalLifetimeManager())
                                        .RegisterType <IObjectSetFactory, DbContextAdapter>(new HierarchicalLifetimeManager())
                                        .RegisterType <IObjectContext, DbContextAdapter>(new HierarchicalLifetimeManager())
                                        .RegisterType <ICustomerRepository, CustomerRepository>(new HierarchicalLifetimeManager());

            UnityContainerDispatcher.InjectParentContainer(container);

            ICustomerRepository customerRepository = container.Resolve <ICustomerRepository>();

            // =============== 增 ===============
            Console.ForegroundColor = ConsoleColor.DarkRed;

            DomainModels.Customer customer1 = new DomainModels.Customer()
            {
                Name    = "Dennis Gao",
                Address = "Beijing",
                Phone   = "18888888888",
            };
            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                customerRepository.InsertCustomer(customer1);
            }
            Console.WriteLine(customer1);

            DomainModels.Customer customer2 = new DomainModels.Customer()
            {
                Name    = "Degang Guo",
                Address = "Beijing",
                Phone   = "16666666666",
            };
            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                customerRepository.InsertCustomer(customer2);
            }
            Console.WriteLine(customer2);

            // =============== 并发 ===============
            Console.ForegroundColor = ConsoleColor.Green;
            Mapper.CreateMap <DomainModels.Customer, DomainModels.Customer>();
            List <Task> tasks = new List <Task>();

            for (int i = 0; i < 16; i++)
            {
                DomainModels.Customer modifiedCustomer = Mapper.Map <DomainModels.Customer, DomainModels.Customer>(customer1);
                modifiedCustomer.Name = modifiedCustomer.Name + i;

                Task t = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        using (UnityContainerScope scope = UnityContainerScope.NewScope())
                        {
                            customerRepository.UpdateCustomer(modifiedCustomer);
                            Console.WriteLine("Modified " + modifiedCustomer.Name + " in thread " + Thread.CurrentThread.ManagedThreadId);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception occurred in thread " + Thread.CurrentThread.ManagedThreadId);
                        Console.WriteLine(ex.Message);
                    }
                });
                tasks.Add(t);
            }
            Task.WaitAll(tasks.ToArray());

            // =============== 清理 ===============
            Console.WriteLine();
            Console.WriteLine("Press any key to clean database...");
            Console.ReadKey();

            using (UnityContainerScope scope = UnityContainerScope.NewScope())
            {
                customerRepository.DeleteAllCustomers();
            }
        }