public async Task TestUowInterceptor() { var id = IdGenerater.GetNextId(IdGenerater.DatacenterId, IdGenerater.WorkerId); var customer = new Customer() { Id = id, Account = "alpha2008", Nickname = IdGenerater.GetNextId().ToString(), Realname = IdGenerater.GetNextId().ToString() }; var cusFinance = new CusFinance { Account = "alpha2008", Id = id, Balance = 0 }; /* * set session transaction isolation level repeatable read * start transaction * INSERT INTO `Customer` (`ID`, `Account`, `CreateBy`, `CreateTime`, `ModifyBy`, `ModifyTime`, `Nickname`, `Realname`) * VALUES (122339207606833152, 'alpha2008', 1600000000000, timestamp('2020-12-03 14:12:20.552579'), NULL, NULL, '1606975940001', '1606975940002') * INSERT INTO `CusFinance` (`ID`, `Account`, `Balance`, `CreateBy`, `CreateTime`, `ModifyBy`, `ModifyTime`) * VALUES (122339207606833152, 'alpha2008', 0, 1600000000000, timestamp('2020-12-03 14:12:20.756977'), NULL, NULL) * commit */ await _cusManger.Register(customer, cusFinance); bool exists = await _cusRsp.ExistAsync(c => c.Id == id); Assert.True(exists); }
public async Task <AppSrvResult <SimpleDto <string> > > Register(RegisterInputDto inputDto) { var exists = await _customerRepo.ExistAsync(t => t.Account == inputDto.Account); if (exists) { return(Problem(HttpStatusCode.Forbidden, "该账号已经存在")); } var customer = _mapper.Map <Customer>(inputDto); customer.ID = IdGenerater.GetNextId(IdGenerater.DatacenterId, IdGenerater.WorkerId); var customerFinace = new CusFinance() { Account = customer.Account , Balance = 0 , ID = customer.ID }; await _cusManagerService.Register(customer, customerFinace); return(new SimpleDto <string>(customer.ID.ToString())); }
public async Task <SimpleDto <string> > Register(RegisterInputDto inputDto) { var exists = await _customerRepo.ExistAsync(t => t.Account == inputDto.Account); if (exists) { throw new BusinessException(new ErrorModel(ErrorCode.Forbidden, "该账号已经存在")); } var customer = _mapper.Map <Customer>(inputDto); customer.ID = IdGenerater.GetNextId(IdGenerater.DatacenterId, IdGenerater.WorkerId); var customerFinace = new CusFinance() { Account = customer.Account , Balance = 0 , ID = customer.ID }; await _cusManagerService.Register(customer, customerFinace); return(new SimpleDto <string> { Result = customer.ID.ToString() }); }
/// <summary> /// 生成测试数据 /// </summary> /// <param name="id"></param> /// <returns></returns> private async Task <CusFinance> InsertCusFinance(long id) { var cusFinance = new CusFinance { Account = "alpha2008", ID = id, Balance = 0 }; await _cusFinanceRsp.InsertAsync(cusFinance); return(cusFinance); }
public async Task TestUOWRollback() { var id = IdGenerater.GetNextId(IdGenerater.DatacenterId, IdGenerater.WorkerId); var id2 = IdGenerater.GetNextId(IdGenerater.DatacenterId, IdGenerater.WorkerId); var account = "alpha008"; var customer = new Customer() { ID = id, Account = account, Nickname = "招财猫", Realname = "张发财" }; var customer2 = new Customer() { ID = id2, Account = account, Nickname = "招财猫02", Realname = "张发财02" }; var cusFinance = new CusFinance { Account = account, ID = id, Balance = 0 }; var newNickName = "招财猫008"; var newRealName = "张发财008"; var newBalance = 100m; try { _unitOfWork.BeginTransaction(); // insert efcore await _cusRsp.InsertAsync(customer); await _cusRsp.InsertAsync(customer2); await _cusFinanceRsp.InsertAsync(cusFinance); //update single customer.Nickname = newNickName; await _cusRsp.UpdateAsync(customer, c => c.Nickname); cusFinance.Balance = newBalance; await _cusFinanceRsp.UpdateAsync(cusFinance, c => c.Balance); //update batchs await _cusRsp.UpdateRangeAsync(x => x.ID == id, c => new Customer { Realname = newRealName }); //delete raw sql await _cusRsp.DeleteAsync(id2); throw new Exception(); _unitOfWork.Commit(); } catch (Exception ex) { _unitOfWork.Rollback(); } finally { _unitOfWork.Dispose(); } var cusTotal = await _cusRsp.CountAsync(x => x.ID == id || x.ID == id2); Assert.Equal(0, cusTotal); var customerFromDb = await _cusRsp.FindAsync(id); var financeFromDb = await _cusRsp.FetchAsync(c => c, x => x.ID == id); Assert.Null(customerFromDb); Assert.Null(financeFromDb); }
public async Task Register(RegisterInputDto inputDto) { var exists = await _customerRepo.ExistAsync(t => t.Account == inputDto.Account); if (exists) { throw new BusinessException(new ErrorModel(ErrorCode.Forbidden, "该账号已经存在")); } var customer = _mapper.Map <Customer>(inputDto); customer.ID = new Snowflake(1, 1).NextId(); var customerFinace = new CusFinance() { Account = customer.Account , Balance = 0 , ID = customer.ID }; await _cusManagerService.Register(customer, customerFinace); }
public virtual async Task Register(Customer customer, CusFinance cusFinance, CancellationToken cancellationToken = default) { await _cusRepo.InsertAsync(customer); await _cusFinaceRepo.InsertAsync(cusFinance); }