feat:完成内部核心模块替换改造成furion
This commit is contained in:
@@ -1,51 +1,71 @@
|
||||
using Furion;
|
||||
using Furion.DatabaseAccessor;
|
||||
using Furion.DependencyInjection;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SqlSugar;
|
||||
using Yi.Framework.Infrastructure.Ddd.Repositories;
|
||||
using Yi.Framework.Infrastructure.Uow;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Sqlsugar.Uow
|
||||
{
|
||||
public class SqlsugarUnitOfWork : IUnitOfWork, ISingleton
|
||||
public class SqlsugarUnitOfWork : IUnitOfWork
|
||||
{
|
||||
public ISqlSugarClient Db { get; set; }
|
||||
public ITenant Tenant { get; set; }
|
||||
public bool IsTran { get; set; }
|
||||
public bool IsCommit { get; set; }
|
||||
public bool IsClose { get; set; }
|
||||
// <summary>
|
||||
/// SqlSugar 对象
|
||||
/// </summary>
|
||||
private readonly ISqlSugarClient _sqlSugarClient;
|
||||
|
||||
public void Dispose()
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="sqlSugarClient"></param>
|
||||
public SqlsugarUnitOfWork(ISqlSugarClient sqlSugarClient)
|
||||
{
|
||||
|
||||
if (IsTran && IsCommit == false)
|
||||
{
|
||||
Tenant.RollbackTran();
|
||||
}
|
||||
if (Db.Ado.Transaction == null && IsClose == false)
|
||||
{
|
||||
Db.Close();
|
||||
}
|
||||
_sqlSugarClient = sqlSugarClient;
|
||||
}
|
||||
|
||||
public bool Commit()
|
||||
/// <summary>
|
||||
/// 开启工作单元处理
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="unitOfWork"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public void BeginTransaction(FilterContext context, UnitOfWorkAttribute unitOfWork)
|
||||
{
|
||||
if (IsTran && IsCommit == false)
|
||||
{
|
||||
Tenant.CommitTran();
|
||||
IsCommit = true;
|
||||
}
|
||||
if (Db.Ado.Transaction == null && IsClose == false)
|
||||
{
|
||||
Db.Close();
|
||||
IsClose = true;
|
||||
}
|
||||
return IsCommit;
|
||||
_sqlSugarClient.AsTenant().BeginTran();
|
||||
}
|
||||
|
||||
public IRepository<T> GetRepository<T>()
|
||||
/// <summary>
|
||||
/// 提交工作单元处理
|
||||
/// </summary>
|
||||
/// <param name="resultContext"></param>
|
||||
/// <param name="unitOfWork"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
||||
{
|
||||
return App.GetRequiredService<IRepository<T>>();
|
||||
_sqlSugarClient.AsTenant().CommitTran();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回滚工作单元处理
|
||||
/// </summary>
|
||||
/// <param name="resultContext"></param>
|
||||
/// <param name="unitOfWork"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
|
||||
{
|
||||
_sqlSugarClient.AsTenant().RollbackTran();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行完毕(无论成功失败)
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="resultContext"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public void OnCompleted(FilterContext context, FilterContext resultContext)
|
||||
{
|
||||
_sqlSugarClient.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
using Furion.DependencyInjection;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Infrastructure.Uow;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Sqlsugar.Uow
|
||||
{
|
||||
/// <summary>
|
||||
/// 此部分为sqlsugr的魔改版本
|
||||
/// </summary>
|
||||
internal class SqlsugarUnitOfWorkManager : IUnitOfWorkManager,ISingleton
|
||||
{
|
||||
public SqlsugarUnitOfWorkManager(ISqlSugarClient db)
|
||||
{
|
||||
Db = db;
|
||||
}
|
||||
public ISqlSugarClient Db { get; set; }
|
||||
public IUnitOfWork CreateContext(bool isTran = true)
|
||||
{
|
||||
SqlsugarUnitOfWork uow = new SqlsugarUnitOfWork();
|
||||
return CreateContext(isTran, uow);
|
||||
}
|
||||
private IUnitOfWork CreateContext(bool isTran, SqlsugarUnitOfWork sugarUnitOf)
|
||||
{
|
||||
sugarUnitOf.Db = Db;
|
||||
sugarUnitOf.Tenant = Db.AsTenant();
|
||||
sugarUnitOf.IsTran = isTran;
|
||||
Db.Open();
|
||||
if (isTran)
|
||||
Db.AsTenant().BeginTran();
|
||||
return sugarUnitOf;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Yi.Framework.Infrastructure.AspNetCore;
|
||||
using Yi.Framework.Infrastructure.Sqlsugar;
|
||||
using Yi.Framework.Infrastructure.Sqlsugar.Uow;
|
||||
|
||||
namespace Yi.Framework.Infrastructure;
|
||||
|
||||
@@ -17,6 +18,8 @@ public class Startup : AppStartup
|
||||
services.Configure<DbConnOptions>(App.Configuration.GetSection("DbConnOptions"));
|
||||
|
||||
services.AddDbSqlsugarContextServer();
|
||||
|
||||
services.AddUnitOfWork<SqlsugarUnitOfWork>();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using Yi.Framework.Infrastructure.Ddd.Repositories;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Uow
|
||||
{
|
||||
internal class DefaultUnitOfWork : IUnitOfWork
|
||||
{
|
||||
public DefaultUnitOfWork() { }
|
||||
public bool IsTran { get; set; }
|
||||
public bool IsCommit { get; set; }
|
||||
public bool IsClose { get; set; }
|
||||
|
||||
public bool Commit()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public IRepository<T> GetRepository<T>()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace Yi.Framework.Infrastructure.Uow
|
||||
{
|
||||
internal class DefaultUnitOfWorkManager : IUnitOfWorkManager
|
||||
{
|
||||
public IUnitOfWork CreateContext(bool isTran = true)
|
||||
{
|
||||
return new DefaultUnitOfWork();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using Yi.Framework.Infrastructure.Ddd.Repositories;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Uow
|
||||
{
|
||||
public interface IUnitOfWork : IDisposable
|
||||
{
|
||||
bool IsTran { get; set; }
|
||||
bool IsCommit { get; set; }
|
||||
bool IsClose { get; set; }
|
||||
|
||||
IRepository<T> GetRepository<T>();
|
||||
bool Commit();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace Yi.Framework.Infrastructure.Uow
|
||||
{
|
||||
public interface IUnitOfWorkManager
|
||||
{
|
||||
IUnitOfWork CreateContext(bool isTran = true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,6 @@ using Yi.Framework.Infrastructure.CurrentUsers;
|
||||
using Yi.Framework.Infrastructure.Ddd.Repositories;
|
||||
using Yi.Framework.Infrastructure.Ddd.Services;
|
||||
using Yi.Framework.Infrastructure.Exceptions;
|
||||
using Yi.Framework.Infrastructure.Uow;
|
||||
using Yi.Framework.Module.ImageSharp.HeiCaptcha;
|
||||
using Yi.Framework.Module.Sms.Aliyun;
|
||||
using Yi.Furion.Rbac.Application.System.Domain;
|
||||
@@ -53,7 +52,6 @@ namespace Yi.Furion.Rbac.Application.System.Services.Impl
|
||||
private UserManager _userManager { get; set; }
|
||||
|
||||
|
||||
private IUnitOfWorkManager _unitOfWorkManager { get; set; }
|
||||
|
||||
|
||||
private IRepository<RoleEntity> _roleRepository { get; set; }
|
||||
@@ -222,6 +220,7 @@ namespace Yi.Furion.Rbac.Application.System.Services.Impl
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[UnitOfWork]
|
||||
public async Task<object> PostRegisterAsync(RegisterDto input)
|
||||
{
|
||||
if (input.UserName == UserConst.Admin)
|
||||
@@ -251,8 +250,7 @@ namespace Yi.Furion.Rbac.Application.System.Services.Impl
|
||||
{
|
||||
throw new UserFriendlyException("用户已存在,注册失败");
|
||||
}
|
||||
using (var uow = _unitOfWorkManager.CreateContext())
|
||||
{
|
||||
|
||||
var newUser = new UserEntity(input.UserName, input.Password, input.Phone);
|
||||
|
||||
var entity = await _userRepository.InsertReturnEntityAsync(newUser);
|
||||
@@ -263,9 +261,6 @@ namespace Yi.Furion.Rbac.Application.System.Services.Impl
|
||||
{
|
||||
await _userManager.GiveUserSetRoleAsync(new List<long> { entity.Id }, new List<long> { role.Id });
|
||||
}
|
||||
uow.Commit();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using SqlSugar;
|
||||
using Yi.Framework.Infrastructure.Ddd.Dtos;
|
||||
using Yi.Framework.Infrastructure.Ddd.Services;
|
||||
using Yi.Framework.Infrastructure.Uow;
|
||||
using Yi.Furion.Rbac.Application.System.Domain;
|
||||
using Yi.Furion.Rbac.Application.System.Dtos.Role;
|
||||
using Yi.Furion.Rbac.Core.Entities;
|
||||
@@ -40,6 +39,7 @@ namespace Yi.Furion.Rbac.Application.System.Services.Impl
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[UnitOfWork]
|
||||
public override async Task<RoleGetOutputDto> CreateAsync(RoleCreateInputVo input)
|
||||
{
|
||||
RoleGetOutputDto outputDto;
|
||||
@@ -61,6 +61,7 @@ namespace Yi.Furion.Rbac.Application.System.Services.Impl
|
||||
/// <param name="id"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[UnitOfWork]
|
||||
public override async Task<RoleGetOutputDto> UpdateAsync(long id, RoleUpdateInputVo input)
|
||||
{
|
||||
var dto = new RoleGetOutputDto();
|
||||
|
||||
@@ -3,7 +3,6 @@ using Yi.Framework.Infrastructure.CurrentUsers;
|
||||
using Yi.Framework.Infrastructure.Ddd.Dtos;
|
||||
using Yi.Framework.Infrastructure.Ddd.Services;
|
||||
using Yi.Framework.Infrastructure.Exceptions;
|
||||
using Yi.Framework.Infrastructure.Uow;
|
||||
using Yi.Framework.Module.OperLogManager;
|
||||
using Yi.Furion.Rbac.Application.System.Domain;
|
||||
using Yi.Furion.Rbac.Application.System.Dtos.User;
|
||||
@@ -73,6 +72,7 @@ namespace Yi.Furion.Rbac.Application.System.Services.Impl
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
[OperLog("添加用户", OperEnum.Insert)]
|
||||
[UnitOfWork]
|
||||
public async override Task<UserGetOutputDto> CreateAsync(UserCreateInputVo input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input.Password))
|
||||
@@ -118,6 +118,7 @@ namespace Yi.Furion.Rbac.Application.System.Services.Impl
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[OperLog("更新用户", OperEnum.Update)]
|
||||
[UnitOfWork]
|
||||
public async override Task<UserGetOutputDto> UpdateAsync(long id, UserUpdateInputVo input)
|
||||
{
|
||||
if (await _repository.IsAnyAsync(u => input.UserName!.Equals(u.UserName) && !id.Equals(u.Id)))
|
||||
|
||||
Reference in New Issue
Block a user