From 2fa3570f8535ebf2d884fb7329db8e6fbb7a2775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Tue, 17 Jan 2023 23:33:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=B7=A5=E4=BD=9C=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Uow/UnitOfWork.cs | 48 +++++++++ .../Uow/UnitOfWorkManager.cs | 37 +++++++ .../Yi.Framework.Core.Sqlsugar.csproj | 2 +- .../YiFrameworkCoreSqlsugarModule.cs | 5 + .../Yi.Framework.Ddd/Yi.Framework.Ddd.csproj | 1 + .../framework/Yi.Framework.Uow/IUnitOfWork.cs | 17 +++ .../Yi.Framework.Uow/IUnitOfWorkManager.cs | 100 ++---------------- .../Student/StudentService.cs | 22 ++-- .../test/Yi.Framework.Web/Program.cs | 2 +- .../test/Yi.Framework.Web/TimeTest.txt | 2 + .../test/Yi.Framework.Web/yi-sqlsugar-dev.db | Bin 12288 -> 12288 bytes 11 files changed, 134 insertions(+), 102 deletions(-) create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWork.cs create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWorkManager.cs create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Uow/IUnitOfWork.cs diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWork.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWork.cs new file mode 100644 index 00000000..3a5545c5 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWork.cs @@ -0,0 +1,48 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Uow; + +namespace Yi.Framework.Core.Sqlsugar.Uow +{ + public class UnitOfWork : 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; } + + public void Dispose() + { + + if (this.IsTran && IsCommit == false) + { + this.Tenant.RollbackTran(); + } + if (this.Db.Ado.Transaction == null && IsClose == false) + { + this.Db.Close(); + } + } + + public bool Commit() + { + if (this.IsTran && this.IsCommit == false) + { + this.Tenant.CommitTran(); + IsCommit = true; + } + if (this.Db.Ado.Transaction == null && this.IsClose == false) + { + this.Db.Close(); + IsClose = true; + } + return IsCommit; + } + } +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWorkManager.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWorkManager.cs new file mode 100644 index 00000000..b235050a --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Uow/UnitOfWorkManager.cs @@ -0,0 +1,37 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Uow; + +namespace Yi.Framework.Core.Sqlsugar.Uow +{ + /// + /// 此部分为sqlsugr的魔改版本 + /// + internal class UnitOfWorkManager : IUnitOfWorkManager + { + public UnitOfWorkManager(ISqlSugarClient db) + { + this.Db = db; + } + public ISqlSugarClient Db { get; set; } + public IUnitOfWork CreateContext(bool isTran = true) + { + UnitOfWork uow = new UnitOfWork(); + return CreateContext(isTran, uow); + } + private IUnitOfWork CreateContext(bool isTran, UnitOfWork sugarUnitOf) + { + sugarUnitOf.Db = Db; + sugarUnitOf.Tenant = Db.AsTenant(); + sugarUnitOf.IsTran = isTran; + Db.Open(); + if (isTran) + Db.AsTenant().BeginTran(); + return sugarUnitOf; + } + } +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Yi.Framework.Core.Sqlsugar.csproj b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Yi.Framework.Core.Sqlsugar.csproj index f994d88c..6cac0d12 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Yi.Framework.Core.Sqlsugar.csproj +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Yi.Framework.Core.Sqlsugar.csproj @@ -13,10 +13,10 @@ + - diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs index 0f9fde52..c73bacb8 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/YiFrameworkCoreSqlsugarModule.cs @@ -5,8 +5,10 @@ using Yi.Framework.Core.Configuration; using Yi.Framework.Core.Sqlsugar.Extensions; using Yi.Framework.Core.Sqlsugar.Options; using Yi.Framework.Core.Sqlsugar.Repositories; +using Yi.Framework.Core.Sqlsugar.Uow; using Yi.Framework.Ddd; using Yi.Framework.Ddd.Repositories; +using Yi.Framework.Uow; namespace Yi.Framework.Core.Sqlsugar { @@ -20,6 +22,9 @@ namespace Yi.Framework.Core.Sqlsugar public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) { services.AddTransient(typeof(IRepository<>), typeof(SqlsugarRepository<>)); + + services.AddSingleton(); + services.Configure(Appsettings.appConfiguration("DbConnOptions")); services.AddSqlsugarServer(); diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Yi.Framework.Ddd.csproj b/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Yi.Framework.Ddd.csproj index 4d82bdc4..af17369b 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Yi.Framework.Ddd.csproj +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Yi.Framework.Ddd.csproj @@ -12,6 +12,7 @@ + diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/IUnitOfWork.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/IUnitOfWork.cs new file mode 100644 index 00000000..172b9732 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/IUnitOfWork.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Uow +{ + public interface IUnitOfWork : IDisposable + { + bool IsTran { get; set; } + bool IsCommit { get; set; } + bool IsClose { get; set; } + + bool Commit(); + } +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/IUnitOfWorkManager.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/IUnitOfWorkManager.cs index ef1d3e51..00b0b408 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/IUnitOfWorkManager.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Uow/IUnitOfWorkManager.cs @@ -1,94 +1,8 @@ -//namespace Yi.Framework.Uow -//{ -// public interface IUnitOfWorkManager -// { -// SugarUnitOfWork CreateContext(bool isTran = true); -// } +namespace Yi.Framework.Uow +{ + public interface IUnitOfWorkManager + { + IUnitOfWork CreateContext(bool isTran = true); + } - -// /// -// /// 这个放到实现类中 -// /// -// public class SugarUnitOfWorkManager : IUnitOfWorkManager -// { -// public SugarUnitOfWorkManager(ISqlSugarClient db) -// { -// this.Db = db; -// } -// public SugarUnitOfWork Db { get; set; } -// public SugarUnitOfWork CreateContext(bool isTran = true) -// { -// return Db.CreateContext(isTran); -// } -// } - - -// /// -// /// 下面这个定死了 -// /// -// public class SugarUnitOfWork : IDisposable -// { -// 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 && IsCommit == false) -// { -// this.Tenant.RollbackTran(); -// } -// if (this.Db.Ado.Transaction == null && IsClose == false) -// { -// this.Db.Close(); -// } -// } - -// public SimpleClient GetRepository() where T : class, new() -// { -// TenantAttribute tenantAttribute = typeof(T).GetCustomAttribute(); -// if (tenantAttribute == null) -// { -// return new SimpleClient(Db); -// } -// else -// { -// return new SimpleClient(Db.AsTenant().GetConnection(tenantAttribute.configId)); -// } -// } - -// public RepositoryType GetMyRepository() where RepositoryType : ISugarRepository, new() -// { -// var result = new RepositoryType(); -// var type = typeof(RepositoryType).GetGenericArguments()[0]; -// TenantAttribute tenantAttribute = type.GetCustomAttribute(); -// if (tenantAttribute == null) -// { -// result.Context = this.Db; -// } -// else -// { -// result.Context = this.Db.AsTenant().GetConnection(tenantAttribute.configId); -// } -// return result; -// } - -// public bool Commit() -// { -// if (this.IsTran && this.IsCommit == false) -// { -// this.Tenant.CommitTran(); -// IsCommit = true; -// } -// if (this.Db.Ado.Transaction == null && this.IsClose == false) -// { -// this.Db.Close(); -// IsClose = true; -// } -// return IsCommit; -// } -// } -//} \ No newline at end of file +} \ No newline at end of file diff --git a/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs b/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs index 98f2aa96..9279a54d 100644 --- a/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs +++ b/Yi.Framework.Net6/test/Yi.Framework.Application/Student/StudentService.cs @@ -14,6 +14,7 @@ using Yi.Framework.Application.Contracts.Student.Dtos; using Yi.Framework.Domain.Student.Entities; using Yi.Framework.Ddd.Services; using Yi.Framework.Core.Attributes; +using Yi.Framework.Uow; namespace Yi.Framework.Application.Student { @@ -23,24 +24,31 @@ namespace Yi.Framework.Application.Student [AppService] public class StudentService : CrudAppService, - IStudentService,IAutoApiService + IStudentService, IAutoApiService { private readonly IStudentRepository _studentRepository; private readonly StudentManager _studentManager; - public StudentService(IStudentRepository studentRepository, StudentManager studentManager) + private readonly IUnitOfWorkManager _unitOfWorkManager; + public StudentService(IStudentRepository studentRepository, StudentManager studentManager, IUnitOfWorkManager unitOfWorkManager) { _studentRepository = studentRepository; _studentManager = studentManager; + _unitOfWorkManager = unitOfWorkManager; } /// - /// 你好世界 + /// Uow /// /// - public async Task> PostShijie() + public async Task PostUow() { - throw new NotImplementedException(); - var entities = await _studentRepository.GetMyListAsync(); - return await MapToGetListOutputDtosAsync(entities); + StudentGetOutputDto res = new(); + using (var uow = _unitOfWorkManager.CreateContext()) + { + res = await base.CreateAsync(new StudentCreateInputVo { Name = $"老杰哥{DateTime.Now.ToString("ffff")}", Number = 2023 }); + if (new Random().Next(0, 2) == 0) throw new NotImplementedException(); + uow.Commit(); + } + return res; } } } diff --git a/Yi.Framework.Net6/test/Yi.Framework.Web/Program.cs b/Yi.Framework.Net6/test/Yi.Framework.Web/Program.cs index 9331e00b..4ff8c565 100644 --- a/Yi.Framework.Net6/test/Yi.Framework.Web/Program.cs +++ b/Yi.Framework.Net6/test/Yi.Framework.Web/Program.cs @@ -8,7 +8,7 @@ TimeTest.Start(); var builder = WebApplication.CreateBuilder(args); //url -builder.WebHost.UseStartUrlsServer(builder.Configuration, "StartUrl"); +builder.WebHost.UseStartUrlsServer(builder.Configuration); //ģ builder.UseYiModules(typeof(YiFrameworkWebModule)); diff --git a/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt b/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt index e51f52d3..2180c9a2 100644 --- a/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt +++ b/Yi.Framework.Net6/test/Yi.Framework.Web/TimeTest.txt @@ -30,3 +30,5 @@ 2023:01:17-22:43:56本次运行启动时间为:2023毫秒 2023:01:17-22:45:25本次运行启动时间为:1803毫秒 2023:01:17-22:45:52本次运行启动时间为:1877毫秒 +2023:01:17-23:24:07本次运行启动时间为:1836毫秒 +2023:01:17-23:29:20本次运行启动时间为:1958毫秒 diff --git a/Yi.Framework.Net6/test/Yi.Framework.Web/yi-sqlsugar-dev.db b/Yi.Framework.Net6/test/Yi.Framework.Web/yi-sqlsugar-dev.db index ceeda9cc3b6aac28acc02dda1dd5d56f1c4d4e5c..13bbae92710adc19c9925af7ffe6bd91555ecbae 100644 GIT binary patch delta 667 zcmY+Bze@sP7{^c3^Y{DQNkm{^gc+34-MzkdIn-cdRM1jO;82T8WPiX}OG`uH)ZnH; zLy$N}WwT=r4Q-{+SW_*%_wpRPjUPTdzrIhsQm(dy;K4Qw)uuoN3C^v6#gxRg}|PWoan2 zZExK$%Iv5o$r7`9<#;HhHg2;!c^z^Je8Ui4p%0JHg=@ItZ*!pr2e1q4unH#Ry1Mp5 z+~k-V@V})ai0^0!;v3+Dmp(x#4EXjbAG~mr2qMgc4_