public void InsertCustomer(DomainModels.Customer customer) { Customer entity = Mapper.Map <DomainModels.Customer, Customer>(customer); entity = Add(entity); customer.Id = entity.Id; }
public void InsertCustomer(DomainModels.Customer customer) { Customer entity = Mapper.Map <DomainModels.Customer, Customer>(customer); Repository.Customers.Insert(entity); Repository.Commit(); customer.Id = entity.Id; }
public void InsertCustomer(DomainModels.Customer customer) { using (RETAILContext context = new RETAILContext()) { Customer entity = Mapper.Map <DomainModels.Customer, Customer>(customer); context.Customers.Add(entity); context.SaveChanges(); customer.Id = entity.Id; } }
public void InsertCustomer(DomainModels.Customer customer) { using (RetailEntities context = new RetailEntities()) { Customer entity = Mapper.Map <DomainModels.Customer, Customer>(customer); context.Customers.AddObject(entity); context.SaveChanges(); customer.Id = entity.Id; } }
public void UpdateCustomer(DomainModels.Customer customer) { Customer entity = Repository.Customers.Query().Single(c => c.Id == customer.Id); entity.Name = customer.Name; entity.Address = customer.Address; entity.Phone = customer.Phone; Repository.Customers.Update(entity); Repository.Commit(); }
public List <DomainModels.Customer> GetAllCustomers() { List <Customer> entities = Repository.Customers.Query().ToList(); List <DomainModels.Customer> customers = new List <DomainModels.Customer>(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity); customers.Add(customer); } return(customers); }
public List <DomainModels.Customer> GetCustomersByAddress(string address) { List <Customer> entities = Repository.Customers.Query().Where(c => c.Address == address).ToList(); List <DomainModels.Customer> customers = new List <DomainModels.Customer>(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity); customers.Add(customer); } return(customers); }
public void UpdateCustomer(DomainModels.Customer customer) { using (RetailEntities context = new RetailEntities()) { Customer entity = context.Customers.AsQueryable().Single(c => c.Id == customer.Id); entity.Name = customer.Name; entity.Address = customer.Address; entity.Phone = customer.Phone; context.SaveChanges(); } }
public List <DomainModels.Customer> GetAllCustomers() { IList <Customer> entities = GetAll(); List <DomainModels.Customer> customers = new List <DomainModels.Customer>(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity); customers.Add(customer); } return(customers); }
public List <DomainModels.Customer> GetCustomersByAddress(string address) { using (RetailEntities context = new RetailEntities()) { List <Customer> entities = context.Customers.AsQueryable().Where(c => c.Address == address).ToList(); List <DomainModels.Customer> customers = new List <DomainModels.Customer>(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity); customers.Add(customer); } return(customers); } }
public List <DomainModels.Customer> GetAllCustomers() { using (RETAILContext context = new RETAILContext()) { List <Customer> entities = context.Customers.AsQueryable().ToList(); List <DomainModels.Customer> customers = new List <DomainModels.Customer>(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity); customers.Add(customer); } return(customers); } }
static void Main(string[] args) { IUnityContainer container = new UnityContainer() .RegisterType(typeof(IRepository <>), typeof(Repository <>), new ContainerControlledLifetimeManager()) .RegisterType <IUnitOfWork, UnitOfWork>(new ContainerControlledLifetimeManager()) .RegisterType <DbContext, RETAILContext>(new ContainerControlledLifetimeManager()) .RegisterType <DbContextAdapter>(new ContainerControlledLifetimeManager()) .RegisterType <IObjectSetFactory, DbContextAdapter>(new ContainerControlledLifetimeManager()) .RegisterType <IObjectContext, DbContextAdapter>(new ContainerControlledLifetimeManager()) .RegisterType <ICustomerRepository, CustomerRepository>(new ContainerControlledLifetimeManager()); ICustomerRepository customerRepository = container.Resolve <ICustomerRepository>(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.DarkRed; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; customerRepository.InsertCustomer(customer1); Console.WriteLine(customer1); DomainModels.Customer customer2 = new DomainModels.Customer() { Name = "Degang Guo", Address = "Beijing", Phone = "16666666666", }; customerRepository.InsertCustomer(customer2); Console.WriteLine(customer2); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkYellow; List <DomainModels.Customer> allCustomers = customerRepository.GetAllCustomers(); foreach (var customer in allCustomers) { Console.WriteLine(customer); } // =============== 改 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; customer2.Address = "Tianjin"; customerRepository.UpdateCustomer(customer2); Console.WriteLine(customer2); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Cyan; List <DomainModels.Customer> filteredCustomers = customerRepository.GetCustomersByAddress("Tianjin"); foreach (var customer in filteredCustomers) { Console.WriteLine(customer); } // =============== 删 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Blue; customerRepository.DeleteCustomersByAddress("Tianjin"); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Magenta; List <DomainModels.Customer> existingCustomers = customerRepository.GetAllCustomers(); foreach (var customer in existingCustomers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
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(); } }
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(); } }
static void Main(string[] args) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.Red; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; DomainModels.Customer customer2 = new DomainModels.Customer() { //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空 Address = "Beijing", Phone = "16666666666", }; //Task t = Task.Factory.StartNew(() => //{ // int i = 0; // while (i < 100) // { // List<DomainModels.Customer> customers = new List<DomainModels.Customer>(); // using (var transactionScope = new TransactionScope( // TransactionScopeOption.RequiresNew, // new TransactionOptions() // { // IsolationLevel = IsolationLevel.ReadUncommitted // })) // { // try // { // using (RetailEntities context = new RetailEntities()) // { // List<Customer> entities = context.Customers.AsQueryable().ToList(); // foreach (var entity in entities) // { // DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); // customers.Add(customer); // } // } // } // catch (Exception ex) // { // Console.WriteLine(FlattenException(ex)); // } // transactionScope.Complete(); // } // Console.WriteLine("-----> " + i + " times"); // foreach (var customer in customers) // { // Console.WriteLine(customer); // } // i++; // Thread.Sleep(1000); // } //}); try { using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew)) { Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1); Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities()) { context.Customers.Add(entity1); context.SaveChanges(); // 顺利提交 context.Customers.Add(entity2); context.SaveChanges(); // 提交时将抛出异常 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> getCustomers = new List<DomainModels.Customer>(); using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { try { using (RetailEntities context = new RetailEntities()) { List<Customer> entities = context.Customers.AsQueryable().ToList(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); getCustomers.Add(customer); } } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } transactionScope.Complete(); } foreach (var customer in getCustomers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
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); 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) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.Red; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; DomainModels.Customer customer2 = new DomainModels.Customer() { //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空 Address = "Beijing", Phone = "16666666666", }; //Task t = Task.Factory.StartNew(() => //{ // int i = 0; // while (i < 100) // { // List<DomainModels.Customer> customers = new List<DomainModels.Customer>(); // using (var transactionScope = new TransactionScope( // TransactionScopeOption.RequiresNew, // new TransactionOptions() // { // IsolationLevel = IsolationLevel.ReadUncommitted // })) // { // try // { // using (RetailEntities context = new RetailEntities()) // { // List<Customer> entities = context.Customers.AsQueryable().ToList(); // foreach (var entity in entities) // { // DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); // customers.Add(customer); // } // } // } // catch (Exception ex) // { // Console.WriteLine(FlattenException(ex)); // } // transactionScope.Complete(); // } // Console.WriteLine("-----> " + i + " times"); // foreach (var customer in customers) // { // Console.WriteLine(customer); // } // i++; // Thread.Sleep(1000); // } //}); try { using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew)) { Customer entity1 = Mapper.Map <DomainModels.Customer, Customer>(customer1); Customer entity2 = Mapper.Map <DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities()) { context.Customers.Add(entity1); context.SaveChanges(); // 顺利提交 context.Customers.Add(entity2); context.SaveChanges(); // 提交时将抛出异常 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> getCustomers = new List <DomainModels.Customer>(); using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { try { using (RetailEntities context = new RetailEntities()) { List <Customer> entities = context.Customers.AsQueryable().ToList(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity); getCustomers.Add(customer); } } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } transactionScope.Complete(); } foreach (var customer in getCustomers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
static void Main(string[] args) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.Red; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; DomainModels.Customer customer2 = new DomainModels.Customer() { //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空 Address = "Beijing", Phone = "16666666666", }; try { Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1); Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities()) { context.Customers.Add(entity1); context.Customers.Add(entity2); using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { context.SaveChanges(); transactionScope.Complete(); } customer1.Id = entity1.Id; customer2.Id = entity2.Id; } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } Console.WriteLine(customer1); Console.WriteLine(customer2); Console.WriteLine("====================================="); // =============== 查询回滚结果 =============== List<DomainModels.Customer> customers = new List<DomainModels.Customer>(); try { using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { using (RetailEntities context = new RetailEntities()) { List<Customer> entities = context.Customers.AsQueryable().ToList(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); customers.Add(customer); } } transactionScope.Complete(); } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } foreach (var customer in customers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
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) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.DarkRed; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; customerRepository.InsertCustomer(customer1); Console.WriteLine(customer1); DomainModels.Customer customer2 = new DomainModels.Customer() { Name = "Degang Guo", Address = "Beijing", Phone = "16666666666", }; customerRepository.InsertCustomer(customer2); Console.WriteLine(customer2); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkYellow; List <DomainModels.Customer> allCustomers = customerRepository.GetAllCustomers(); foreach (var customer in allCustomers) { Console.WriteLine(customer); } // =============== 改 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; customer2.Address = "Tianjin"; customerRepository.UpdateCustomer(customer2); Console.WriteLine(customer2); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Cyan; List <DomainModels.Customer> filteredCustomers = customerRepository.GetCustomersByAddress("Tianjin"); foreach (var customer in filteredCustomers) { Console.WriteLine(customer); } // =============== 删 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Blue; customerRepository.DeleteCustomersByAddress("Tianjin"); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Magenta; List <DomainModels.Customer> existingCustomers = customerRepository.GetAllCustomers(); foreach (var customer in existingCustomers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
static void Main(string[] args) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.Red; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; DomainModels.Customer customer2 = new DomainModels.Customer() { //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空 Address = "Beijing", Phone = "16666666666", }; try { Customer entity1 = Mapper.Map <DomainModels.Customer, Customer>(customer1); Customer entity2 = Mapper.Map <DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities()) { context.Customers.Add(entity1); context.Customers.Add(entity2); using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { context.SaveChanges(); transactionScope.Complete(); } customer1.Id = entity1.Id; customer2.Id = entity2.Id; } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } Console.WriteLine(customer1); Console.WriteLine(customer2); Console.WriteLine("====================================="); // =============== 查询回滚结果 =============== List <DomainModels.Customer> customers = new List <DomainModels.Customer>(); try { using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { using (RetailEntities context = new RetailEntities()) { List <Customer> entities = context.Customers.AsQueryable().ToList(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity); customers.Add(customer); } } transactionScope.Complete(); } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } foreach (var customer in customers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
static void Main(string[] args) { IUnityContainer container = new UnityContainer() .RegisterType(typeof(IRepository<>), typeof(Repository<>), new ContainerControlledLifetimeManager()) .RegisterType<IUnitOfWork, UnitOfWork>(new ContainerControlledLifetimeManager()) .RegisterType<DbContext, RETAILContext>(new ContainerControlledLifetimeManager()) .RegisterType<DbContextAdapter>(new ContainerControlledLifetimeManager()) .RegisterType<IObjectSetFactory, DbContextAdapter>(new ContainerControlledLifetimeManager()) .RegisterType<IObjectContext, DbContextAdapter>(new ContainerControlledLifetimeManager()) .RegisterType<ICustomerRepository, CustomerRepository>(new ContainerControlledLifetimeManager()); UnityServiceLocator locator = new UnityServiceLocator(container); ServiceLocator.SetLocatorProvider(() => locator); ICustomerRepository customerRepository = container.Resolve<ICustomerRepository>(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.DarkRed; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; customerRepository.InsertCustomer(customer1); Console.WriteLine(customer1); DomainModels.Customer customer2 = new DomainModels.Customer() { Name = "Degang Guo", Address = "Beijing", Phone = "16666666666", }; customerRepository.InsertCustomer(customer2); Console.WriteLine(customer2); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkYellow; List<DomainModels.Customer> allCustomers = customerRepository.GetAllCustomers(); foreach (var customer in allCustomers) { Console.WriteLine(customer); } // =============== 改 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; customer2.Address = "Tianjin"; customerRepository.UpdateCustomer(customer2); Console.WriteLine(customer2); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Cyan; List<DomainModels.Customer> filteredCustomers = customerRepository.GetCustomersByAddress("Tianjin"); foreach (var customer in filteredCustomers) { Console.WriteLine(customer); } // =============== 删 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Blue; customerRepository.DeleteCustomersByAddress("Tianjin"); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Magenta; List<DomainModels.Customer> existingCustomers = customerRepository.GetAllCustomers(); foreach (var customer in existingCustomers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }