public void AddProducts() { for (var x = 0; x < 2; x++) { var products = new List<Product>(); for (var i = 0; i < 100; i++) { products.Add(new Product { ProductName = Guid.NewGuid().ToString(), Discontinued = false, ObjectState = ObjectState.Added }); } using (IDataContextAsync context = new NorthwindContext()) using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context)) { var northwindContext = (NorthwindContext)context; Assert.IsFalse(northwindContext.ChangeTracker.Entries().Any()); IRepositoryAsync<Product> productRepository = new Repository<Product>(context, unitOfWork); productRepository.InsertGraphRange(products); products.Clear(); unitOfWork.SaveChanges(); Assert.IsTrue(northwindContext.ChangeTracker.Entries().Count() == 100); } System.Threading.Thread.Sleep(5000); } }
public void InsertProducts() { using (IDataContextAsync context = new NorthwindContext()) using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context)) { IRepositoryAsync<Product> productRepository = new Repository<Product>(context, unitOfWork); var newProducts = new[] { new Product {ProductName = "One", Discontinued = false, ObjectState = ObjectState.Added}, new Product {ProductName = "12345678901234567890123456789012345678901234567890", Discontinued = true, ObjectState = ObjectState.Added}, new Product {ProductName = "Three", Discontinued = true, ObjectState = ObjectState.Added}, new Product {ProductName = "Four", Discontinued = true, ObjectState = ObjectState.Added}, new Product {ProductName = "Five", Discontinued = true, ObjectState = ObjectState.Added} }; foreach (var product in newProducts) { try { productRepository.Insert(product); unitOfWork.SaveChanges(); } catch (DbEntityValidationException ex) { var sb = new StringBuilder(); foreach (var failure in ex.EntityValidationErrors) { sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType()); foreach (var error in failure.ValidationErrors) { sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage); sb.AppendLine(); } } Debug.WriteLine(sb.ToString()); TestContext.WriteLine(sb.ToString()); } catch (Exception ex) { Debug.WriteLine(ex.Message); TestContext.WriteLine(ex.Message); } } var insertedProduct = productRepository.Query(x => x.ProductName == "One").Select().FirstOrDefault(); Assert.IsTrue(insertedProduct.ProductName == "One"); } }
public void UnitOfWork_Dispose_Test() { IDataContextAsync context = new NorthwindContext(); IUnitOfWorkAsync unitOfWork = new UnitOfWork(context); // opening connection unitOfWork.BeginTransaction(); unitOfWork.Commit(); // calling dispose 1st time unitOfWork.Dispose(); var isDisposed = (bool) GetInstanceField(typeof (UnitOfWork), unitOfWork, "_disposed"); Assert.IsTrue(isDisposed); // calling dispose 2nd time, should not throw any excpetions unitOfWork.Dispose(); context.Dispose(); // calling dispose 3rd time, should not throw any excpetions context.Dispose(); unitOfWork.Dispose(); }
public void UnitOfWork_Transaction_Test() { using(IDataContextAsync context = new NorthwindContext()) using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context)) { IRepositoryAsync<Customer> customerRepository = new Repository<Customer>(context, unitOfWork); IService<Customer> customerService = new CustomerService(customerRepository); try { unitOfWork.BeginTransaction(); customerService.Insert(new Customer { CustomerID = "YODA", CompanyName = "SkyRanch", ObjectState = ObjectState.Added}); customerService.Insert(new Customer { CustomerID = "JEDI", CompanyName = "SkyRanch", ObjectState = ObjectState.Added}); var customer = customerService.Find("YODA"); Assert.AreSame(customer.CustomerID, "YODA"); customer = customerService.Find("JEDI"); Assert.AreSame(customer.CustomerID, "JEDI"); // save var saveChangesAsync = unitOfWork.SaveChanges(); //Assert.AreSame(saveChangesAsync, 2); // Will cause an exception, cannot insert customer with the same CustomerId (primary key constraint) customerService.Insert(new Customer { CustomerID = "JEDI", CompanyName = "SkyRanch", ObjectState = ObjectState.Added }); //save unitOfWork.SaveChanges(); unitOfWork.Commit(); } catch (Exception e) { unitOfWork.Rollback(); } } }
public void CreateOrderObjectGraphTest() { using (IDataContextAsync context = new NorthwindContext()) using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context)) { IRepositoryAsync<Order> orderRepository = new Repository<Order>(context, unitOfWork); var orderTest = new Order { CustomerID = "LLE39", EmployeeID = 10, OrderDate = DateTime.Now, ObjectState = ObjectState.Added, Employee = new Employee { EmployeeID = 10, FirstName = "Test", LastName = "Le", ObjectState = ObjectState.Added }, OrderDetails = new List<OrderDetail> { new OrderDetail { ProductID = 1, Quantity = 5, ObjectState = ObjectState.Added }, new OrderDetail { ProductID = 2, Quantity = 5, ObjectState = ObjectState.Added } } }; orderRepository.InsertOrUpdateGraph(orderTest); try { unitOfWork.SaveChanges(); } catch (DbEntityValidationException ex) { var sb = new StringBuilder(); foreach (var failure in ex.EntityValidationErrors) { sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType()); foreach (var error in failure.ValidationErrors) { sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage); sb.AppendLine(); } } Debug.WriteLine(sb.ToString()); TestContext.WriteLine(sb.ToString()); } catch (Exception ex) { Debug.WriteLine(ex.Message); TestContext.WriteLine(ex.Message); } } }