推广您的产品

外观
在服务方法上增加 [AppTransaction] 特性,使用当前项目主库事务
提示
virtual
虚方法,才能正常使用事务。例如下面的用户更新,存在多个 cud 操作,操作要么全部成功,要么全部失败。
[AppTransaction]
public virtual async Task UpdateAsync(UserUpdateInput input)
{
if (!(input?.Id > 0))
{
//事务回滚
throw ResultOutput.Exception("请选择用户");
}
//查询用户
var user = await _userRepository.GetAsync(input.Id);
if (!(user?.Id > 0))
{
//事务回滚
throw ResultOutput.Exception("用户不存在");
// throw new AppException("用户不存在");
}
//数据映射
_mapper.Map(input, user);
//更新用户
await _userRepository.UpdateAsync(user);
//删除用户角色
await _userRoleRepository.DeleteAsync(a => a.UserId == user.Id);
if (input.RoleIds != null && input.RoleIds.Any())
{
var roles = input.RoleIds.Select(a => new UserRoleEntity { UserId = user.Id, RoleId = a });
//批量插入用户角色
await _userRoleRepository.InsertAsync(roles);
}
}
抛出友好异常(推荐)
throw ResultOutput.Exception(msg);
throw new AppException(msg);
无异常抛出,将自动提交事务
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
Propagation | 事务传播方式 | enum | Required |
IsolationLevel | 事务隔离级别 | enum | - |
Propagation 事务传播方式可选值:
IsolationLevel 事务隔离级别可选值: