From dabbc7ea3b230cc2a5c5af20dbcba9fd691e0468 Mon Sep 17 00:00:00 2001 From: LemonNoCry <773596523@qq.com> Date: Fri, 14 Oct 2022 11:32:51 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20UnitOfWork=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E4=BA=8B=E5=8A=A1=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Blog.Core.Api/Blog.Core.xml | 4 +- .../Controllers/DbFirst/MigrateController.cs | 14 ++-- .../Controllers/TasksQzController.cs | 50 ++++++------- .../Controllers/TransactionController.cs | 14 ++-- Blog.Core.Api/Controllers/UserController.cs | 16 ++-- Blog.Core.Extensions/AOP/BlogTranAOP.cs | 20 ++--- .../AutofacModuleRegister.cs | 5 +- Blog.Core.Repository/BASE/BaseRepository.cs | 10 +-- .../Blog.Core.Repository.csproj | 4 - .../RoleModulePermissionRepository.cs | 4 +- .../IUnitOfWorkManage.cs} | 7 +- .../UnitOfWorks/UnitOfWork.cs | 50 +++++++++++++ .../UnitOfWorkManager.cs} | 23 ++++-- Blog.Core.Services/GuestbookServices.cs | 74 +++++++++---------- Blog.Core.Services/WeChatCompanyServices.cs | 8 +- Blog.Core.Services/WeChatConfigServices.cs | 14 ++-- Blog.Core.Services/WeChatPushLogServices.cs | 1 - Blog.Core.Services/WeChatSubServices.cs | 1 - .../DependencyInjection/DI_Test.cs | 2 - 19 files changed, 188 insertions(+), 133 deletions(-) rename Blog.Core.Repository/{UnitOfWork/IUnitOfWork.cs => UnitOfWorks/IUnitOfWorkManage.cs} (73%) create mode 100644 Blog.Core.Repository/UnitOfWorks/UnitOfWork.cs rename Blog.Core.Repository/{UnitOfWork/UnitOfWork.cs => UnitOfWorks/UnitOfWorkManager.cs} (85%) diff --git a/Blog.Core.Api/Blog.Core.xml b/Blog.Core.Api/Blog.Core.xml index 7ef55c7..b9c09b8 100644 --- a/Blog.Core.Api/Blog.Core.xml +++ b/Blog.Core.Api/Blog.Core.xml @@ -644,11 +644,11 @@ 用户管理 - + 构造函数 - + diff --git a/Blog.Core.Api/Controllers/DbFirst/MigrateController.cs b/Blog.Core.Api/Controllers/DbFirst/MigrateController.cs index b282c93..8006d1f 100644 --- a/Blog.Core.Api/Controllers/DbFirst/MigrateController.cs +++ b/Blog.Core.Api/Controllers/DbFirst/MigrateController.cs @@ -1,5 +1,4 @@ using Blog.Core.Common.Helper; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; @@ -15,6 +14,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using Blog.Core.Repository.UnitOfWorks; namespace Blog.Core.Controllers { @@ -23,7 +23,7 @@ namespace Blog.Core.Controllers //[Authorize(Permissions.Name)] public class MigrateController : ControllerBase { - private readonly IUnitOfWork _unitOfWork; + private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly IRoleModulePermissionServices _roleModulePermissionServices; private readonly IUserRoleServices _userRoleServices; private readonly IRoleServices _roleServices; @@ -33,7 +33,7 @@ namespace Blog.Core.Controllers private readonly ISysUserInfoServices _sysUserInfoServices; private readonly IWebHostEnvironment _env; - public MigrateController(IUnitOfWork unitOfWork, + public MigrateController(IUnitOfWorkManage unitOfWorkManage, IRoleModulePermissionServices roleModulePermissionServices, IUserRoleServices userRoleServices, IRoleServices roleServices, @@ -43,7 +43,7 @@ namespace Blog.Core.Controllers ISysUserInfoServices sysUserInfoServices, IWebHostEnvironment env) { - _unitOfWork = unitOfWork; + _unitOfWorkManage = unitOfWorkManage; _roleModulePermissionServices = roleModulePermissionServices; _userRoleServices = userRoleServices; _roleServices = roleServices; @@ -86,7 +86,7 @@ namespace Blog.Core.Controllers permissions = permissions.Where(d => filterPermissionIds.Contains(d.Id)).ToList(); // 开启事务,保证数据一致性 - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); // 注意信息的完整性,不要重复添加,确保主库没有要添加的数据 @@ -136,14 +136,14 @@ namespace Blog.Core.Controllers } - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); data.success = true; data.msg = "导入成功!"; } catch (Exception) { - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); } } diff --git a/Blog.Core.Api/Controllers/TasksQzController.cs b/Blog.Core.Api/Controllers/TasksQzController.cs index ba9b735..264fa19 100644 --- a/Blog.Core.Api/Controllers/TasksQzController.cs +++ b/Blog.Core.Api/Controllers/TasksQzController.cs @@ -4,11 +4,11 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Threading.Tasks; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; using Blog.Core.Model.ViewModels; +using Blog.Core.Repository.UnitOfWorks; using Blog.Core.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -23,11 +23,11 @@ namespace Blog.Core.Controllers { private readonly ITasksQzServices _tasksQzServices; private readonly ISchedulerCenter _schedulerCenter; - private readonly IUnitOfWork _unitOfWork; + private readonly IUnitOfWorkManage _unitOfWorkManage; - public TasksQzController(ITasksQzServices tasksQzServices, ISchedulerCenter schedulerCenter, IUnitOfWork unitOfWork) + public TasksQzController(ITasksQzServices tasksQzServices, ISchedulerCenter schedulerCenter, IUnitOfWorkManage unitOfWorkManage) { - _unitOfWork = unitOfWork; + _unitOfWorkManage = unitOfWorkManage; _tasksQzServices = tasksQzServices; _schedulerCenter = schedulerCenter; } @@ -70,7 +70,7 @@ namespace Blog.Core.Controllers public async Task> Post([FromBody] TasksQz tasksQz) { var data = new MessageModel(); - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); var id = (await _tasksQzServices.Add(tasksQz)); data.success = id > 0; try @@ -107,9 +107,9 @@ namespace Blog.Core.Controllers } finally { if(data.success) - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); else - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); } return data; } @@ -126,7 +126,7 @@ namespace Blog.Core.Controllers var data = new MessageModel(); if (tasksQz != null && tasksQz.Id > 0) { - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); data.success = await _tasksQzServices.Update(tasksQz); try { @@ -160,9 +160,9 @@ namespace Blog.Core.Controllers finally { if (data.success) - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); else - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); } } return data; @@ -180,7 +180,7 @@ namespace Blog.Core.Controllers var model = await _tasksQzServices.QueryById(jobId); if (model != null) { - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); data.success = await _tasksQzServices.Delete(model); try { @@ -204,9 +204,9 @@ namespace Blog.Core.Controllers finally { if (data.success) - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); else - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); } } else @@ -229,7 +229,7 @@ namespace Blog.Core.Controllers var model = await _tasksQzServices.QueryById(jobId); if (model != null) { - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); try { model.IsStart = true; @@ -262,9 +262,9 @@ namespace Blog.Core.Controllers finally { if (data.success) - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); else - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); } } else @@ -325,7 +325,7 @@ namespace Blog.Core.Controllers var model = await _tasksQzServices.QueryById(jobId); if (model != null) { - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); try { data.success = await _tasksQzServices.Update(model); @@ -356,9 +356,9 @@ namespace Blog.Core.Controllers finally { if (data.success) - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); else - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); } } else @@ -380,7 +380,7 @@ namespace Blog.Core.Controllers var model = await _tasksQzServices.QueryById(jobId); if (model != null) { - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); try { model.IsStart = true; @@ -412,9 +412,9 @@ namespace Blog.Core.Controllers finally { if (data.success) - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); else - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); } } else @@ -436,7 +436,7 @@ namespace Blog.Core.Controllers if (model != null) { - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); try { model.IsStart = true; @@ -472,9 +472,9 @@ namespace Blog.Core.Controllers finally { if (data.success) - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); else - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); } } else diff --git a/Blog.Core.Api/Controllers/TransactionController.cs b/Blog.Core.Api/Controllers/TransactionController.cs index dfd502c..dd6b038 100644 --- a/Blog.Core.Api/Controllers/TransactionController.cs +++ b/Blog.Core.Api/Controllers/TransactionController.cs @@ -1,10 +1,10 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; +using Blog.Core.Repository.UnitOfWorks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -17,12 +17,12 @@ namespace Blog.Core.Controllers { private readonly IPasswordLibServices _passwordLibServices; private readonly IGuestbookServices _guestbookServices; - private readonly IUnitOfWork _unitOfWork; + private readonly IUnitOfWorkManage _unitOfWorkManage; - public TransactionController(IUnitOfWork unitOfWork, IPasswordLibServices passwordLibServices, IGuestbookServices guestbookServices) + public TransactionController(IUnitOfWorkManage unitOfWorkManage, IPasswordLibServices passwordLibServices, IGuestbookServices guestbookServices) { - _unitOfWork = unitOfWork; + _unitOfWorkManage = unitOfWorkManage; _passwordLibServices = passwordLibServices; _guestbookServices = guestbookServices; } @@ -36,7 +36,7 @@ namespace Blog.Core.Controllers { returnMsg.Add($"Begin Transaction"); - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); var passwords = await _passwordLibServices.Query(d => d.IsDeleted == false); returnMsg.Add($"first time : the count of passwords is :{passwords.Count}"); @@ -76,11 +76,11 @@ namespace Blog.Core.Controllers returnMsg.Add($"first time : the count of guestbooks is :{guestbooks.Count}"); returnMsg.Add($" "); - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); } catch (Exception) { - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); var passwords = await _passwordLibServices.Query(); returnMsg.Add($"third time : the count of passwords is :{passwords.Count}"); diff --git a/Blog.Core.Api/Controllers/UserController.cs b/Blog.Core.Api/Controllers/UserController.cs index 21bc4e5..6a2a18f 100644 --- a/Blog.Core.Api/Controllers/UserController.cs +++ b/Blog.Core.Api/Controllers/UserController.cs @@ -6,11 +6,11 @@ using AutoMapper; using Blog.Core.AuthHelper.OverWrite; using Blog.Core.Common.Helper; using Blog.Core.Common.HttpContextUser; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; using Blog.Core.Model.ViewModels; +using Blog.Core.Repository.UnitOfWorks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -25,7 +25,7 @@ namespace Blog.Core.Controllers [Authorize(Permissions.Name)] public class UserController : BaseApiController { - private readonly IUnitOfWork _unitOfWork; + private readonly IUnitOfWorkManage _unitOfWorkManage; readonly ISysUserInfoServices _sysUserInfoServices; readonly IUserRoleServices _userRoleServices; readonly IRoleServices _roleServices; @@ -37,7 +37,7 @@ namespace Blog.Core.Controllers /// /// 构造函数 /// - /// + /// /// /// /// @@ -45,13 +45,13 @@ namespace Blog.Core.Controllers /// /// /// - public UserController(IUnitOfWork unitOfWork, ISysUserInfoServices sysUserInfoServices, + public UserController(IUnitOfWorkManage unitOfWorkManage, ISysUserInfoServices sysUserInfoServices, IUserRoleServices userRoleServices, IRoleServices roleServices, IDepartmentServices departmentServices, IUser user, IMapper mapper, ILogger logger) { - _unitOfWork = unitOfWork; + _unitOfWorkManage = unitOfWorkManage; _sysUserInfoServices = sysUserInfoServices; _userRoleServices = userRoleServices; _roleServices = roleServices; @@ -215,7 +215,7 @@ namespace Blog.Core.Controllers _mapper.Map(sysUserInfo, oldUser); - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); // 无论 Update Or Add , 先删除当前用户的全部 U_R 关系 var usreroles = (await _userRoleServices.Query(d => d.UserId == oldUser.Id)); if (usreroles.Any()) @@ -246,7 +246,7 @@ namespace Blog.Core.Controllers data.success = await _sysUserInfoServices.Update(oldUser); - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); if (data.success) { @@ -256,7 +256,7 @@ namespace Blog.Core.Controllers } catch (Exception e) { - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); _logger.LogError(e, e.Message); } diff --git a/Blog.Core.Extensions/AOP/BlogTranAOP.cs b/Blog.Core.Extensions/AOP/BlogTranAOP.cs index 837b018..2c252ef 100644 --- a/Blog.Core.Extensions/AOP/BlogTranAOP.cs +++ b/Blog.Core.Extensions/AOP/BlogTranAOP.cs @@ -1,11 +1,11 @@ using Blog.Core.Common; -using Blog.Core.IRepository.UnitOfWork; using Castle.DynamicProxy; using Microsoft.Extensions.Logging; using System; using System.Reflection; using System.Threading.Tasks; using Blog.Core.Common.DB; +using Blog.Core.Repository.UnitOfWorks; namespace Blog.Core.AOP { @@ -15,11 +15,11 @@ namespace Blog.Core.AOP public class BlogTranAOP : IInterceptor { private readonly ILogger _logger; - private readonly IUnitOfWork _unitOfWork; + private readonly IUnitOfWorkManage _unitOfWorkManage; - public BlogTranAOP(IUnitOfWork unitOfWork, ILogger logger) + public BlogTranAOP(IUnitOfWorkManage unitOfWorkManage, ILogger logger) { - _unitOfWork = unitOfWork; + _unitOfWorkManage = unitOfWorkManage; _logger = logger; } @@ -70,16 +70,16 @@ namespace Blog.Core.AOP switch (propagation) { case Propagation.Required: - if (_unitOfWork.TranCount <= 0) + if (_unitOfWorkManage.TranCount <= 0) { _logger.LogDebug($"Begin Transaction"); Console.WriteLine($"Begin Transaction"); - _unitOfWork.BeginTran(method); + _unitOfWorkManage.BeginTran(method); } break; case Propagation.Mandatory: - if (_unitOfWork.TranCount <= 0) + if (_unitOfWorkManage.TranCount <= 0) { throw new Exception("事务传播机制为:[Mandatory],当前不存在事务"); } @@ -88,7 +88,7 @@ namespace Blog.Core.AOP case Propagation.Nested: _logger.LogDebug($"Begin Transaction"); Console.WriteLine($"Begin Transaction"); - _unitOfWork.BeginTran(method); + _unitOfWorkManage.BeginTran(method); break; default: throw new ArgumentOutOfRangeException(nameof(propagation), propagation, null); @@ -97,12 +97,12 @@ namespace Blog.Core.AOP private void After(MethodInfo method) { - _unitOfWork.CommitTran(method); + _unitOfWorkManage.CommitTran(method); } private void AfterException(MethodInfo method) { - _unitOfWork.RollbackTran(method); + _unitOfWorkManage.RollbackTran(method); } /// diff --git a/Blog.Core.Extensions/ServiceExtensions/AutofacModuleRegister.cs b/Blog.Core.Extensions/ServiceExtensions/AutofacModuleRegister.cs index 1da6cb3..ef03832 100644 --- a/Blog.Core.Extensions/ServiceExtensions/AutofacModuleRegister.cs +++ b/Blog.Core.Extensions/ServiceExtensions/AutofacModuleRegister.cs @@ -12,8 +12,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Reflection; -using Blog.Core.IRepository.UnitOfWork; -using Blog.Core.Repository.UnitOfWork; +using Blog.Core.Repository.UnitOfWorks; namespace Blog.Core.Extensions { @@ -85,7 +84,7 @@ namespace Blog.Core.Extensions .PropertiesAutowired() .InstancePerDependency(); - builder.RegisterType().As() + builder.RegisterType().As() .AsImplementedInterfaces() .InstancePerLifetimeScope() .PropertiesAutowired(); diff --git a/Blog.Core.Repository/BASE/BaseRepository.cs b/Blog.Core.Repository/BASE/BaseRepository.cs index e9afb1c..76941cd 100644 --- a/Blog.Core.Repository/BASE/BaseRepository.cs +++ b/Blog.Core.Repository/BASE/BaseRepository.cs @@ -1,7 +1,6 @@ using Blog.Core.Common; using Blog.Core.Common.DB; using Blog.Core.IRepository.Base; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.Model; using SqlSugar; using System; @@ -10,12 +9,13 @@ using System.Data; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; +using Blog.Core.Repository.UnitOfWorks; namespace Blog.Core.Repository.Base { public class BaseRepository : IBaseRepository where TEntity : class, new() { - private readonly IUnitOfWork _unitOfWork; + private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly SqlSugarScope _dbBase; private ISqlSugarClient _db @@ -52,10 +52,10 @@ namespace Blog.Core.Repository.Base public ISqlSugarClient Db => _db; - public BaseRepository(IUnitOfWork unitOfWork) + public BaseRepository(IUnitOfWorkManage unitOfWorkManage) { - _unitOfWork = unitOfWork; - _dbBase = unitOfWork.GetDbClient(); + _unitOfWorkManage = unitOfWorkManage; + _dbBase = unitOfWorkManage.GetDbClient(); } diff --git a/Blog.Core.Repository/Blog.Core.Repository.csproj b/Blog.Core.Repository/Blog.Core.Repository.csproj index 7575c0c..5c9764b 100644 --- a/Blog.Core.Repository/Blog.Core.Repository.csproj +++ b/Blog.Core.Repository/Blog.Core.Repository.csproj @@ -29,8 +29,4 @@ - - - - diff --git a/Blog.Core.Repository/RoleModulePermissionRepository.cs b/Blog.Core.Repository/RoleModulePermissionRepository.cs index 8ac4465..1cb21eb 100644 --- a/Blog.Core.Repository/RoleModulePermissionRepository.cs +++ b/Blog.Core.Repository/RoleModulePermissionRepository.cs @@ -1,10 +1,10 @@ using Blog.Core.IRepository; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.Model.Models; using Blog.Core.Repository.Base; using SqlSugar; using System.Collections.Generic; using System.Threading.Tasks; +using Blog.Core.Repository.UnitOfWorks; namespace Blog.Core.Repository { @@ -13,7 +13,7 @@ namespace Blog.Core.Repository /// public class RoleModulePermissionRepository : BaseRepository, IRoleModulePermissionRepository { - public RoleModulePermissionRepository(IUnitOfWork unitOfWork) : base(unitOfWork) + public RoleModulePermissionRepository(IUnitOfWorkManage unitOfWorkManage) : base(unitOfWorkManage) { } diff --git a/Blog.Core.Repository/UnitOfWork/IUnitOfWork.cs b/Blog.Core.Repository/UnitOfWorks/IUnitOfWorkManage.cs similarity index 73% rename from Blog.Core.Repository/UnitOfWork/IUnitOfWork.cs rename to Blog.Core.Repository/UnitOfWorks/IUnitOfWorkManage.cs index 1aaa823..535bd14 100644 --- a/Blog.Core.Repository/UnitOfWork/IUnitOfWork.cs +++ b/Blog.Core.Repository/UnitOfWorks/IUnitOfWorkManage.cs @@ -1,12 +1,15 @@ using System.Reflection; using SqlSugar; -namespace Blog.Core.IRepository.UnitOfWork +namespace Blog.Core.Repository.UnitOfWorks { - public interface IUnitOfWork + public interface IUnitOfWorkManage { SqlSugarScope GetDbClient(); int TranCount { get; } + + UnitOfWork CreateUnitOfWork(); + void BeginTran(); void BeginTran(MethodInfo method); void CommitTran(); diff --git a/Blog.Core.Repository/UnitOfWorks/UnitOfWork.cs b/Blog.Core.Repository/UnitOfWorks/UnitOfWork.cs new file mode 100644 index 0000000..44124a7 --- /dev/null +++ b/Blog.Core.Repository/UnitOfWorks/UnitOfWork.cs @@ -0,0 +1,50 @@ +using System; +using Microsoft.Extensions.Logging; +using SqlSugar; + +namespace Blog.Core.Repository.UnitOfWorks; + +public class UnitOfWork : IDisposable +{ + public ILogger Logger { get; set; } + public ISqlSugarClient Db { get; internal set; } + + public ITenant Tenant { get; internal set; } + + public bool IsTran { get; internal set; } + + public bool IsCommit { get; internal set; } + + public bool IsClose { get; internal set; } + + public void Dispose() + { + if (this.IsTran && !this.IsCommit) + { + Logger.LogDebug("UnitOfWork RollbackTran"); + this.Tenant.RollbackTran(); + } + + if (this.Db.Ado.Transaction != null || this.IsClose) + return; + this.Db.Close(); + } + + public bool Commit() + { + if (this.IsTran && !this.IsCommit) + { + Logger.LogDebug("UnitOfWork CommitTran"); + this.Tenant.CommitTran(); + this.IsCommit = true; + } + + if (this.Db.Ado.Transaction == null && !this.IsClose) + { + this.Db.Close(); + this.IsClose = true; + } + + return this.IsCommit; + } +} \ No newline at end of file diff --git a/Blog.Core.Repository/UnitOfWork/UnitOfWork.cs b/Blog.Core.Repository/UnitOfWorks/UnitOfWorkManager.cs similarity index 85% rename from Blog.Core.Repository/UnitOfWork/UnitOfWork.cs rename to Blog.Core.Repository/UnitOfWorks/UnitOfWorkManager.cs index d487542..5804200 100644 --- a/Blog.Core.Repository/UnitOfWork/UnitOfWork.cs +++ b/Blog.Core.Repository/UnitOfWorks/UnitOfWorkManager.cs @@ -3,22 +3,21 @@ using System.Collections.Concurrent; using System.Reflection; using System.Threading; using Blog.Core.Common.Extensions; -using Blog.Core.IRepository.UnitOfWork; using Microsoft.Extensions.Logging; using SqlSugar; -namespace Blog.Core.Repository.UnitOfWork +namespace Blog.Core.Repository.UnitOfWorks { - public class UnitOfWork : IUnitOfWork + public class UnitOfWorkManageManager : IUnitOfWorkManage { - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly ISqlSugarClient _sqlSugarClient; private int _tranCount { get; set; } public int TranCount => _tranCount; public readonly ConcurrentStack TranStack = new(); - public UnitOfWork(ISqlSugarClient sqlSugarClient, ILogger logger) + public UnitOfWorkManageManager(ISqlSugarClient sqlSugarClient, ILogger logger) { _sqlSugarClient = sqlSugarClient; _logger = logger; @@ -36,6 +35,20 @@ namespace Blog.Core.Repository.UnitOfWork } + public UnitOfWork CreateUnitOfWork() + { + UnitOfWork uow = new UnitOfWork(); + uow.Logger = _logger; + uow.Db = _sqlSugarClient; + uow.Tenant = (ITenant) _sqlSugarClient; + uow.IsTran = true; + + uow.Db.Open(); + uow.Tenant.BeginTran(); + _logger.LogDebug("UnitOfWork Begin"); + return uow; + } + public void BeginTran() { lock (this) diff --git a/Blog.Core.Services/GuestbookServices.cs b/Blog.Core.Services/GuestbookServices.cs index e62a956..daff1ce 100644 --- a/Blog.Core.Services/GuestbookServices.cs +++ b/Blog.Core.Services/GuestbookServices.cs @@ -1,6 +1,5 @@ using Blog.Core.Common; using Blog.Core.IRepository.Base; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; @@ -8,19 +7,20 @@ using Blog.Core.Services.BASE; using System; using System.Threading.Tasks; using Blog.Core.Common.DB; +using Blog.Core.Repository.UnitOfWorks; namespace Blog.Core.Services { public class GuestbookServices : BaseServices, IGuestbookServices { - private readonly IUnitOfWork _unitOfWork; + private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly IBaseRepository _passwordLibRepository; private readonly IPasswordLibServices _passwordLibServices; - public GuestbookServices(IUnitOfWork unitOfWork, IBaseRepository dal, IBaseRepository passwordLibRepository, IPasswordLibServices passwordLibServices) + public GuestbookServices(IUnitOfWorkManage unitOfWorkManage, IBaseRepository dal, IBaseRepository passwordLibRepository, IPasswordLibServices passwordLibServices) { - _unitOfWork = unitOfWork; + _unitOfWorkManage = unitOfWorkManage; _passwordLibRepository = passwordLibRepository; _passwordLibServices = passwordLibServices; } @@ -31,45 +31,46 @@ namespace Blog.Core.Services { Console.WriteLine($""); Console.WriteLine($"事务操作开始"); - _unitOfWork.BeginTran(); - Console.WriteLine($""); - - Console.WriteLine($"insert a data into the table PasswordLib now."); - var insertPassword = await _passwordLibRepository.Add(new PasswordLib() + using (var uow = _unitOfWorkManage.CreateUnitOfWork()) { - IsDeleted = false, - plAccountName = "aaa", - plCreateTime = DateTime.Now - }); + Console.WriteLine($""); + + Console.WriteLine($"insert a data into the table PasswordLib now."); + var insertPassword = await _passwordLibRepository.Add(new PasswordLib() + { + IsDeleted = false, + plAccountName = "aaa", + plCreateTime = DateTime.Now + }); - var passwords = await _passwordLibRepository.Query(d => d.IsDeleted == false); - Console.WriteLine($"second time : the count of passwords is :{passwords.Count}"); + var passwords = await _passwordLibRepository.Query(d => d.IsDeleted == false); + Console.WriteLine($"second time : the count of passwords is :{passwords.Count}"); - //...... + //...... - Console.WriteLine($""); - var guestbooks = await BaseDal.Query(); - Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}"); + Console.WriteLine($""); + var guestbooks = await BaseDal.Query(); + Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}"); - int ex = 0; - Console.WriteLine($"\nThere's an exception!!"); - int throwEx = 1 / ex; + int ex = 0; + Console.WriteLine($"\nThere's an exception!!"); + int throwEx = 1 / ex; - Console.WriteLine($"insert a data into the table Guestbook now."); - var insertGuestbook = await BaseDal.Add(new Guestbook() - { - username = "bbb", - blogId = 1, - createdate = DateTime.Now, - isshow = true - }); + Console.WriteLine($"insert a data into the table Guestbook now."); + var insertGuestbook = await BaseDal.Add(new Guestbook() + { + username = "bbb", + blogId = 1, + createdate = DateTime.Now, + isshow = true + }); - guestbooks = await BaseDal.Query(); - Console.WriteLine($"second time : the count of guestbooks is :{guestbooks.Count}"); + guestbooks = await BaseDal.Query(); + Console.WriteLine($"second time : the count of guestbooks is :{guestbooks.Count}"); - - _unitOfWork.CommitTran(); + uow.Commit(); + } return new MessageModel() { @@ -79,7 +80,6 @@ namespace Blog.Core.Services } catch (Exception) { - _unitOfWork.RollbackTran(); var passwords = await _passwordLibRepository.Query(); Console.WriteLine($"third time : the count of passwords is :{passwords.Count}"); @@ -160,7 +160,7 @@ namespace Blog.Core.Services return true; } - + /// /// 测试无事务 Mandatory传播机制报错 @@ -207,7 +207,5 @@ namespace Blog.Core.Services return true; } - - } } \ No newline at end of file diff --git a/Blog.Core.Services/WeChatCompanyServices.cs b/Blog.Core.Services/WeChatCompanyServices.cs index 864c408..cd686ad 100644 --- a/Blog.Core.Services/WeChatCompanyServices.cs +++ b/Blog.Core.Services/WeChatCompanyServices.cs @@ -1,7 +1,6 @@ using Blog.Core.Common; using Blog.Core.Common.Helper; using Blog.Core.IRepository.Base; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; @@ -11,6 +10,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Threading.Tasks; +using Blog.Core.Repository.UnitOfWorks; namespace Blog.Core.Services { @@ -19,11 +19,11 @@ namespace Blog.Core.Services /// public class WeChatCompanyServices : BaseServices, IWeChatCompanyServices { - readonly IUnitOfWork _unitOfWork; + readonly IUnitOfWorkManage _unitOfWorkManage; readonly ILogger _logger; - public WeChatCompanyServices(IUnitOfWork unitOfWork, ILogger logger) + public WeChatCompanyServices(IUnitOfWorkManage unitOfWorkManage, ILogger logger) { - this._unitOfWork = unitOfWork; + this._unitOfWorkManage = unitOfWorkManage; this._logger = logger; } diff --git a/Blog.Core.Services/WeChatConfigServices.cs b/Blog.Core.Services/WeChatConfigServices.cs index 612a15d..0952ce1 100644 --- a/Blog.Core.Services/WeChatConfigServices.cs +++ b/Blog.Core.Services/WeChatConfigServices.cs @@ -1,7 +1,6 @@ using Blog.Core.Common; using Blog.Core.Common.Helper; using Blog.Core.IRepository.Base; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; @@ -11,6 +10,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Threading.Tasks; +using Blog.Core.Repository.UnitOfWorks; namespace Blog.Core.Services { @@ -19,11 +19,11 @@ namespace Blog.Core.Services /// public class WeChatConfigServices : BaseServices, IWeChatConfigServices { - readonly IUnitOfWork _unitOfWork; + readonly IUnitOfWorkManage _unitOfWorkManage; readonly ILogger _logger; - public WeChatConfigServices(IUnitOfWork unitOfWork, ILogger logger) + public WeChatConfigServices(IUnitOfWorkManage unitOfWorkManage, ILogger logger) { - this._unitOfWork = unitOfWork; + this._unitOfWorkManage = unitOfWorkManage; this._logger = logger; } public async Task> GetToken(string publicAccount) @@ -831,17 +831,17 @@ namespace Blog.Core.Services try { - _unitOfWork.BeginTran(); + _unitOfWorkManage.BeginTran(); await BaseDal.Db.Updateable(ticket).ExecuteCommandAsync(); if (isNewBind) await BaseDal.Db.Insertable(bindUser).ExecuteCommandAsync(); else await BaseDal.Db.Updateable(bindUser).ExecuteCommandAsync(); - _unitOfWork.CommitTran(); + _unitOfWorkManage.CommitTran(); } catch { - _unitOfWork.RollbackTran(); + _unitOfWorkManage.RollbackTran(); throw; } return @$" diff --git a/Blog.Core.Services/WeChatPushLogServices.cs b/Blog.Core.Services/WeChatPushLogServices.cs index 3e1ca52..e389147 100644 --- a/Blog.Core.Services/WeChatPushLogServices.cs +++ b/Blog.Core.Services/WeChatPushLogServices.cs @@ -1,7 +1,6 @@ using Blog.Core.Common; using Blog.Core.Common.Helper; using Blog.Core.IRepository.Base; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; diff --git a/Blog.Core.Services/WeChatSubServices.cs b/Blog.Core.Services/WeChatSubServices.cs index 2b0d7ed..7f66dd2 100644 --- a/Blog.Core.Services/WeChatSubServices.cs +++ b/Blog.Core.Services/WeChatSubServices.cs @@ -1,7 +1,6 @@ using Blog.Core.Common; using Blog.Core.Common.Helper; using Blog.Core.IRepository.Base; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.IServices; using Blog.Core.Model; using Blog.Core.Model.Models; diff --git a/Blog.Core.Tests/DependencyInjection/DI_Test.cs b/Blog.Core.Tests/DependencyInjection/DI_Test.cs index e921d80..6fad1f4 100644 --- a/Blog.Core.Tests/DependencyInjection/DI_Test.cs +++ b/Blog.Core.Tests/DependencyInjection/DI_Test.cs @@ -10,10 +10,8 @@ using Blog.Core.Common.LogHelper; using Blog.Core.Common.Seed; using Blog.Core.Extensions; using Blog.Core.IRepository.Base; -using Blog.Core.IRepository.UnitOfWork; using Blog.Core.Repository.Base; using Blog.Core.Repository.MongoRepository; -using Blog.Core.Repository.UnitOfWork; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.DependencyInjection;