public async Task TestRepositoryAsync() { var uow = serviceProvider.GetService <IUnitOfWork>(); var customerRepository = serviceProvider.GetService <ICustomerRepository>(); var orderHeaderRepository = serviceProvider.GetService <IOrderHeaderRepository>(); var orderDetailRepository = serviceProvider.GetService <IOrderDetailRepository>(); //Test insert,update,get,delete var customer = new Customer() { Name = "testCustomer" }; await customerRepository.InsertAsync(customer); await customerRepository.Where(it => it.Name == "testCustomer") .SetValue(it => it.Age, 5) .SetValue(it => it.TotalConsumptionAmount, 100) .ExecuteUpdateAsync(); var ccc = customerRepository.Where(it => it.Name == "testCustomer").Distinct().FirstOrDefault(); var age5Customers = customerRepository.Where(it => it.Name == "testCustomer").ToList(); Assert.Single((IEnumerable)age5Customers); Assert.Equal(5, age5Customers[0].Age); Assert.Equal(100, age5Customers[0].TotalConsumptionAmount); var orderHeader = new OrderHeader(); orderHeader.CreateTime = DateTime.UtcNow; orderHeader.CustomerId = customer.Id; orderHeader.State = 1; orderHeader.OrderNo = Guid.NewGuid().ToString("N"); await orderHeaderRepository.InsertAsync(orderHeader); var orderDetail = new OrderDetail(); orderDetail.OrderHeaderId = orderHeader.Id; orderDetail.ProductName = "apple"; orderDetail.Quantity = 1; await orderDetailRepository.InsertAsync(orderDetail); var orderDetail2 = new OrderDetail(); orderDetail2.OrderHeaderId = orderHeader.Id; orderDetail2.ProductName = "orange"; orderDetail2.Quantity = 2; await orderDetailRepository.InsertAsync(orderDetail2); var result = await customerRepository.QueryAllBuyProductByNameAsync("testCustomer"); Assert.Contains(result, t => t.ProductName == "apple"); Assert.Contains(result, t => t.ProductName == "orange"); orderDetail.Quantity = 2; await orderDetailRepository.UpdateAsync(orderDetail); var orderDetailTmp = await orderDetailRepository.GetAsync(orderDetail.Id); Assert.Equal(2, orderDetailTmp.Quantity); await orderDetailRepository.DeleteAsync(orderDetail2); var result2 = await customerRepository.QueryAllBuyProductByNameAsync("testCustomer"); Assert.Single(result2); Assert.Contains(result2, t => t.ProductName == "apple"); //Test unitOfWork try { uow.BeginTransaction(); await customerRepository.InsertAsync(new Customer() { Name = "testCustomer2" }); var orderDetail3 = new OrderDetail { OrderHeaderId = orderHeader.Id, ProductName = "ball", Quantity = 3 }; await orderDetailRepository.InsertAsync(orderDetail3); uow.Commit(); } catch (Exception e) { uow.RollBack(); } var allCustomer = await customerRepository.GetAllAsync(); Assert.Equal(2, allCustomer.Count); Assert.Contains(allCustomer, t => t.Name == "testCustomer2"); var allOrderDetails = await orderDetailRepository.GetAllAsync(); Assert.Equal(2, allCustomer.Count); Assert.Contains(allOrderDetails, t => t.ProductName == "ball"); try { uow.BeginTransaction(); await customerRepository.InsertAsync(new Customer() { Name = "testCustomer3" }); throw new Exception("testException"); var orderDetail4 = new OrderDetail(); orderDetail4.OrderHeaderId = orderHeader.Id; orderDetail4.ProductName = "basketball"; orderDetail4.Quantity = 4; await orderDetailRepository.InsertAsync(orderDetail4); uow.Commit(); } catch (Exception e) { uow.RollBack(); } allCustomer = await customerRepository.GetAllAsync(); Assert.Equal(2, allCustomer.Count); Assert.Contains(allCustomer, t => t.Name == "testCustomer2"); allOrderDetails = await orderDetailRepository.GetAllAsync(); Assert.Equal(2, allCustomer.Count); Assert.Contains(allOrderDetails, t => t.ProductName == "ball"); //Test page var customers = new List <Customer>(); for (int i = 0; i < 100; i++) { customers.Add(new Customer() { Age = i, Name = "page" + i }); } var newCount = await customerRepository.InsertAsync(customers); Assert.Equal(100, newCount.Count); var pageable = new Pageable(1, 10); var page = await customerRepository.GetCustomerByPageAsync(pageable, 5); //0-99岁,大于5的只有94个 Assert.Equal(94, page.TotalPages); Assert.Equal(10, page.Data.Count); var page2 = await customerRepository.Where(it => it.Age > 5).Skip(0).Take(10).ToPageAsync(); Assert.Equal(94, page2.TotalPages); Assert.Equal(10, page2.Data.Count); //测试bindWhere构造条件 var nameEmpty = WhereBuilder.Empty <string>(); var ageEmpty = WhereBuilder.Empty <int>(); var nameWhereItem = WhereBuilder.HasValue("page5"); var ageWhereItem = WhereBuilder.HasValue(5); var bindResult = await customerRepository.GetCustomerByConditionAsync(nameWhereItem, ageEmpty); Assert.Single(bindResult); var bindResult2 = await customerRepository.GetCustomerByConditionAsync(nameEmpty, ageEmpty); Assert.Equal(102, bindResult2.Count); var bindResult3 = await customerRepository.GetCustomerByConditionAsync(nameEmpty, ageWhereItem); Assert.Equal(2, bindResult3.Count); var bindResult4 = await customerRepository.GetCustomerByConditionAsync(nameWhereItem, ageWhereItem); Assert.Single(bindResult4); var bindResult5 = await customerRepository.GetCustomerByPageByConditionAsync(pageable, nameWhereItem, ageEmpty); Assert.Single(bindResult5.Data); var bindResult6 = await customerRepository.GetCustomerByPageByConditionAsync(pageable, nameEmpty, ageEmpty); Assert.Equal(10, bindResult6.Data.Count); var bindResult7 = await customerRepository.GetCustomerByPageByConditionAsync(pageable, nameEmpty, ageWhereItem); Assert.Equal(2, bindResult7.Data.Count); var bindResult8 = await customerRepository.GetCustomerByPageByConditionAsync(pageable, nameWhereItem, ageWhereItem); Assert.Single(bindResult8.Data); //var customers= customerRepository.Where(it => it.Age > 5).OrderBy(it => it.Id).Take(10).ToList(); //Test update var newCount2 = await customerRepository.Where(it => it.Age > 5).SetValue(it => it.Name, "a") .ExecuteUpdateAsync(); Assert.Equal(94, newCount2); //Test delete var newCount3 = await customerRepository.DeleteAsync(it => it.Age > 5); Assert.Equal(94, newCount3); await customerRepository.DeleteAsync(it => it.Age > 5); var newCount4 = await customerRepository.GetAllAsync(); Assert.Equal(8, newCount4.Count); }