diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml index 655c8090..02f9b1fc 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml @@ -560,7 +560,7 @@ 测试控制器 - + 依赖注入,优雅写法 @@ -572,6 +572,7 @@ + @@ -585,6 +586,12 @@ + + + 工作单元测试,使用sqlsugar内置 + + + 执行Sql返回 diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs index bd5937d4..878c3057 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs @@ -1,8 +1,10 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; +using SqlSugar; using System; using System.Collections.Generic; using System.Linq; @@ -19,7 +21,6 @@ using Yi.Framework.Job; using Yi.Framework.Language; using Yi.Framework.Model.RABC.Entitys; using Yi.Framework.Repository; -using Yi.Framework.Uow.Interceptors; using Yi.Framework.WebCore; using Yi.Framework.WebCore.AttributeExtend; using Yi.Framework.WebCore.AuthorizationPolicy; @@ -43,6 +44,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers private ThumbnailSharpInvoer _thumbnailSharpInvoer; private CacheInvoker _cacheDb; private ILogger _logger; + ISugarUnitOfWork _unitOfWork; [Autowired] public CacheInvoker CacheInvoker { get; set; } //你可以依赖注入服务层各各接口,也可以注入其他仓储层,怎么爽怎么来! @@ -57,6 +59,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// /// + /// public TestController(IHubContext hub, ILogger logger, IRoleService iRoleService, @@ -64,11 +67,13 @@ namespace Yi.Framework.ApiMicroservice.Controllers IStringLocalizer local, QuartzInvoker quartzInvoker, ThumbnailSharpInvoer thumbnailSharpInvoer, - CacheInvoker cacheInvoker) => + CacheInvoker cacheInvoker, + ISugarUnitOfWork unitOfWork + ) => - (_logger, _iUserService, _iRoleService, _quartzInvoker, _hub, _local, _thumbnailSharpInvoer, _cacheDb) = + (_logger, _iUserService, _iRoleService, _quartzInvoker, _hub, _local, _thumbnailSharpInvoer, _cacheDb, _unitOfWork) = - (logger, iUserService, iRoleService, quartzInvoker, hub, local, thumbnailSharpInvoer, cacheInvoker); + (logger, iUserService, iRoleService, quartzInvoker, hub, local, thumbnailSharpInvoer, cacheInvoker, unitOfWork); /// @@ -113,12 +118,20 @@ namespace Yi.Framework.ApiMicroservice.Controllers return Result.Success().SetData(await _iUserService.DbTest()); } + /// + /// 工作单元测试,使用sqlsugar内置 + /// + /// [HttpGet] - - public async Task TestUnitOfWork() + + public async Task UnitOfWorkTest() { - await _iRoleService.UowTest(); - return Result.Success(); + var data = await _iRoleService._repository._DbQueryable.ToListAsync(); + using (var uow = _unitOfWork.CreateContext()) + { + var res = await _iRoleService._repository.InsertReturnSnowflakeIdAsync(new RoleEntity { RoleName = "测试", RoleCode = "tt" }); + throw new ApplicationException("测试uow"); + } } /// diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs index c2919db0..52cea547 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs @@ -19,7 +19,6 @@ using Microsoft.AspNetCore.Mvc.Controllers; using Yi.Framework.WebCore.AutoFacExtend; using AspectCore.Extensions.DependencyInjection; using AspectCore.Extensions.Hosting; -using Yi.Framework.Uow.Interceptors; using Yi.Framework.WebCore.AspNetCoreExtensions; using Yi.Framework.WebCore.CommonExtend; @@ -36,8 +35,6 @@ builder.Host.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder configurationBuilder.AddApolloService("Yi"); }); -builder.Services.AddUnitOfWork(); - builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer(containerBuilder => { diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj index 947ac7b8..79db2747 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj @@ -10,7 +10,7 @@ ./Config/SwaggerDoc.xml - 1701;1702;CS1591 + 1701;1702;CS1591;CS8618 @@ -25,7 +25,6 @@ - diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/RABC/IRoleService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/RABC/IRoleService.cs index 954f0c38..01fb7986 100644 --- a/Yi.Framework.Net6/Yi.Framework.Interface/RABC/IRoleService.cs +++ b/Yi.Framework.Net6/Yi.Framework.Interface/RABC/IRoleService.cs @@ -16,11 +16,7 @@ namespace Yi.Framework.Interface.RABC /// Task> DbTest(); - /// - /// 工作单元测试 - /// - /// - Task UowTest(); + /// /// 通过角色id获取角色实体包含菜单 diff --git a/Yi.Framework.Net6/Yi.Framework.Model/RABC/Entitys/UserEntity.cs b/Yi.Framework.Net6/Yi.Framework.Model/RABC/Entitys/UserEntity.cs index c68f06a2..ce4da3e8 100644 --- a/Yi.Framework.Net6/Yi.Framework.Model/RABC/Entitys/UserEntity.cs +++ b/Yi.Framework.Net6/Yi.Framework.Model/RABC/Entitys/UserEntity.cs @@ -151,6 +151,10 @@ namespace Yi.Framework.Model.RABC.Entitys //如果不传值,那就把自己的password当作传进来的password if (password == null) { + if (Password == null) + { + throw new ArgumentNullException("Password不能为空"); + } password = Password; } Salt = MD5Helper.GenerateSalt(); diff --git a/Yi.Framework.Net6/Yi.Framework.Model/Yi.Framework.Model.csproj b/Yi.Framework.Net6/Yi.Framework.Model/Yi.Framework.Model.csproj index 4d5cad77..38505875 100644 --- a/Yi.Framework.Net6/Yi.Framework.Model/Yi.Framework.Model.csproj +++ b/Yi.Framework.Net6/Yi.Framework.Model/Yi.Framework.Model.csproj @@ -5,6 +5,14 @@ enable + + 1701;1702;CS8625;CS8618 + + + + 1701;1702;CS8625;CS8618 + + diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs b/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs deleted file mode 100644 index 9caae018..00000000 --- a/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs +++ /dev/null @@ -1,15 +0,0 @@ -//using SqlSugar; -//using Yi.Framework.Common.Models; -//using Yi.Framework.Model.Models; - -//namespace Yi.Framework.Repository -//{ -// public class DataContext : SimpleClient where T : class, IBaseModelEntity, new() -// { -// public DataContext(ISqlSugarClient context) : base(context) -// { -// } - -// } -//} -//简化已被弃用 diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/UnitOfWork.cs b/Yi.Framework.Net6/Yi.Framework.Repository/UnitOfWork.cs new file mode 100644 index 00000000..dda5a513 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Repository/UnitOfWork.cs @@ -0,0 +1,11 @@ +using SqlSugar; + +namespace Yi.Framework.Repository +{ + /// + /// Sqlsugar自带工作单元,何乐而不为呢? + /// + public class UnitOfWork: SugarUnitOfWork + { + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Service/RABC/RoleService.cs b/Yi.Framework.Net6/Yi.Framework.Service/RABC/RoleService.cs index de39724b..e20ed0ed 100644 --- a/Yi.Framework.Net6/Yi.Framework.Service/RABC/RoleService.cs +++ b/Yi.Framework.Net6/Yi.Framework.Service/RABC/RoleService.cs @@ -8,7 +8,6 @@ using Yi.Framework.Interface; using Yi.Framework.Model.RABC.Entitys; using Yi.Framework.Repository; using Yi.Framework.Service.Base; -using Yi.Framework.Uow.Interceptors; using Yi.Framework.Interface.RABC; using Yi.Framework.DTOModel.Base.Dto; @@ -21,17 +20,11 @@ namespace Yi.Framework.Service.RABC } public async Task> DbTest() { - return await _repository._Db.Queryable().ToListAsync(); - } - //添加工作单元特性 - [UnitOfWork] - public async Task UowTest() - { - var res = await _repository.InsertReturnSnowflakeIdAsync(new RoleEntity { RoleName = "测试", RoleCode = "tt" }); - throw new ApplicationException("测试uow"); - return res > 0; + return await _repository._DbQueryable.ToListAsync(); } + + public async Task GiveRoleSetMenu(List roleIds, List menuIds) { var _repositoryRoleMenu = _repository.ChangeRepository>(); diff --git a/Yi.Framework.Net6/Yi.Framework.Service/Yi.Framework.Service.csproj b/Yi.Framework.Net6/Yi.Framework.Service/Yi.Framework.Service.csproj index 8565dc13..3ff23470 100644 --- a/Yi.Framework.Net6/Yi.Framework.Service/Yi.Framework.Service.csproj +++ b/Yi.Framework.Net6/Yi.Framework.Service/Yi.Framework.Service.csproj @@ -10,7 +10,6 @@ - diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AspNetCoreExtensions/AutoIocExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AspNetCoreExtensions/AutoIocExtend.cs index bf4a2565..538a2442 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AspNetCoreExtensions/AutoIocExtend.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AspNetCoreExtensions/AutoIocExtend.cs @@ -10,7 +10,6 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using Yi.Framework.Common.Attribute; -using Yi.Framework.Uow.Interceptors; namespace Yi.Framework.WebCore.AspNetCoreExtensions { diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AspNetCoreExtensions/SqlsugarExtension.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AspNetCoreExtensions/SqlsugarExtension.cs index 241b14db..421b8397 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AspNetCoreExtensions/SqlsugarExtension.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AspNetCoreExtensions/SqlsugarExtension.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SqlSugar; using System; @@ -8,6 +9,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using Yi.Framework.Common.Models; +using Yi.Framework.Repository; using Yi.Framework.WebCore.CommonExtend; namespace Yi.Framework.WebCore.AspNetCoreExtensions @@ -124,6 +126,9 @@ namespace Yi.Framework.WebCore.AspNetCoreExtensions }); services.AddSingleton(sqlSugar);//这边是SqlSugarScope用AddSingleton + + //添加工作单元 + services.AddSingleton>(new SugarUnitOfWork(sqlSugar)); } } diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs index fd5024dc..5eb7028e 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs @@ -15,7 +15,6 @@ using Yi.Framework.Interface; using Yi.Framework.Job; using Yi.Framework.Repository; using Yi.Framework.Service; -using Yi.Framework.Uow.Interceptors; using Yi.Framework.WebCore.AutoFacExtend; using Module = Autofac.Module; @@ -49,9 +48,9 @@ namespace Yi.Framework.WebCore.AutoFacExtend containerBuilder.RegisterAssemblyTypes(assemblysServices) .AsImplementedInterfaces() .InstancePerLifetimeScope() - .EnableInterfaceInterceptors() + .EnableInterfaceInterceptors(); //开启工作单元拦截 - .InterceptedBy(typeof(UnitOfWorkInterceptor)); + //.InterceptedBy(typeof(UnitOfWorkInterceptor)); ///反射注册任务调度层 var assemblysJob = GetDll("Yi.Framework.Job.dll"); diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj b/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj index 7bf6ea1d..f92555ea 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj @@ -34,7 +34,6 @@ - diff --git a/Yi.Framework.Net6/Yi.Framework.sln b/Yi.Framework.Net6/Yi.Framework.sln index 7d1d0180..4e95fef1 100644 --- a/Yi.Framework.Net6/Yi.Framework.sln +++ b/Yi.Framework.Net6/Yi.Framework.sln @@ -49,8 +49,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Template", "Yi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.XUnitTest", "Yi.Framework.XUnitTest\Yi.Framework.XUnitTest.csproj", "{88E3298A-135D-4D9C-B98D-41A2C4268385}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.Uow", "Yi.Framework.Uow\Yi.Framework.Uow.csproj", "{657E4EA0-5A34-4D09-A39C-419C31E740FE}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -129,10 +127,6 @@ Global {88E3298A-135D-4D9C-B98D-41A2C4268385}.Debug|Any CPU.Build.0 = Debug|Any CPU {88E3298A-135D-4D9C-B98D-41A2C4268385}.Release|Any CPU.ActiveCfg = Release|Any CPU {88E3298A-135D-4D9C-B98D-41A2C4268385}.Release|Any CPU.Build.0 = Release|Any CPU - {657E4EA0-5A34-4D09-A39C-419C31E740FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {657E4EA0-5A34-4D09-A39C-419C31E740FE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {657E4EA0-5A34-4D09-A39C-419C31E740FE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {657E4EA0-5A34-4D09-A39C-419C31E740FE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -156,7 +150,6 @@ Global {C307189D-C42C-4C09-BB65-5A386C9F182B} = {9ABAF6B1-6C02-498A-90A2-ABC1140CF89A} {A51E9091-3745-461A-A3CB-32598BF0DC77} = {9ABAF6B1-6C02-498A-90A2-ABC1140CF89A} {88E3298A-135D-4D9C-B98D-41A2C4268385} = {C90E38FB-69EA-4997-8B3A-2C71EFA65B2B} - {657E4EA0-5A34-4D09-A39C-419C31E740FE} = {DB2506F5-05FD-4E76-940E-41D7AA148550} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {1ED77A6E-377F-4EEF-A3D0-D65C94657DAF}