使用sqlsugar内置工作单元

This commit is contained in:
chenchun
2023-01-02 13:59:05 +08:00
parent 0e78857645
commit ea35871aba
16 changed files with 65 additions and 58 deletions

View File

@@ -560,7 +560,7 @@
测试控制器
</summary>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.#ctor(Microsoft.AspNetCore.SignalR.IHubContext{Yi.Framework.WebCore.SignalRHub.MainHub},Microsoft.Extensions.Logging.ILogger{Yi.Framework.ApiMicroservice.Controllers.TestController},Yi.Framework.Interface.RABC.IRoleService,Yi.Framework.Interface.RABC.IUserService,Microsoft.Extensions.Localization.IStringLocalizer{Yi.Framework.Language.LocalLanguage},Yi.Framework.Core.Quartz.QuartzInvoker,Yi.Framework.Core.ThumbnailSharpInvoer,Yi.Framework.Core.Cache.CacheInvoker)">
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.#ctor(Microsoft.AspNetCore.SignalR.IHubContext{Yi.Framework.WebCore.SignalRHub.MainHub},Microsoft.Extensions.Logging.ILogger{Yi.Framework.ApiMicroservice.Controllers.TestController},Yi.Framework.Interface.RABC.IRoleService,Yi.Framework.Interface.RABC.IUserService,Microsoft.Extensions.Localization.IStringLocalizer{Yi.Framework.Language.LocalLanguage},Yi.Framework.Core.Quartz.QuartzInvoker,Yi.Framework.Core.ThumbnailSharpInvoer,Yi.Framework.Core.Cache.CacheInvoker,SqlSugar.ISugarUnitOfWork{Yi.Framework.Repository.UnitOfWork})">
<summary>
依赖注入,优雅写法
</summary>
@@ -572,6 +572,7 @@
<param name="quartzInvoker"></param>
<param name="thumbnailSharpInvoer"></param>
<param name="cacheInvoker"></param>
<param name="unitOfWork"></param>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.Swagger">
<summary>
@@ -585,6 +586,12 @@
</summary>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.UnitOfWorkTest">
<summary>
工作单元测试使用sqlsugar内置
</summary>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.SqlTest">
<summary>
执行Sql返回

View File

@@ -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<TestController> _logger;
ISugarUnitOfWork<UnitOfWork> _unitOfWork;
[Autowired]
public CacheInvoker CacheInvoker { get; set; }
//你可以依赖注入服务层各各接口,也可以注入其他仓储层,怎么爽怎么来!
@@ -57,6 +59,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// <param name="quartzInvoker"></param>
/// <param name="thumbnailSharpInvoer"></param>
/// <param name="cacheInvoker"></param>
/// <param name="unitOfWork"></param>
public TestController(IHubContext<MainHub> hub,
ILogger<TestController> logger,
IRoleService iRoleService,
@@ -64,11 +67,13 @@ namespace Yi.Framework.ApiMicroservice.Controllers
IStringLocalizer<LocalLanguage> local,
QuartzInvoker quartzInvoker,
ThumbnailSharpInvoer thumbnailSharpInvoer,
CacheInvoker cacheInvoker) =>
CacheInvoker cacheInvoker,
ISugarUnitOfWork<UnitOfWork> 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);
/// <summary>
@@ -113,12 +118,20 @@ namespace Yi.Framework.ApiMicroservice.Controllers
return Result.Success().SetData(await _iUserService.DbTest());
}
/// <summary>
/// 工作单元测试使用sqlsugar内置
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<Result> TestUnitOfWork()
public async Task<Result> 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");
}
}
/// <summary>

View File

@@ -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>(containerBuilder =>
{

View File

@@ -10,7 +10,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>./Config/SwaggerDoc.xml</DocumentationFile>
<NoWarn>1701;1702;CS1591</NoWarn>
<NoWarn>1701;1702;CS1591;CS8618</NoWarn>
</PropertyGroup>
<ItemGroup>
@@ -25,7 +25,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Uow\Yi.Framework.Uow.csproj" />
<ProjectReference Include="..\Yi.Framework.WebCore\Yi.Framework.WebCore.csproj" />
</ItemGroup>

View File

@@ -16,11 +16,7 @@ namespace Yi.Framework.Interface.RABC
/// <returns></returns>
Task<List<RoleEntity>> DbTest();
/// <summary>
/// 工作单元测试
/// </summary>
/// <returns></returns>
Task<bool> UowTest();
/// <summary>
/// 通过角色id获取角色实体包含菜单

View File

@@ -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();

View File

@@ -5,6 +5,14 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;CS8625;CS8618</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;CS8625;CS8618</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NEST" Version="7.16.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.3.30" />

View File

@@ -1,15 +0,0 @@
//using SqlSugar;
//using Yi.Framework.Common.Models;
//using Yi.Framework.Model.Models;
//namespace Yi.Framework.Repository
//{
// public class DataContext<T> : SimpleClient<T> where T : class, IBaseModelEntity, new()
// {
// public DataContext(ISqlSugarClient context) : base(context)
// {
// }
// }
//}
//简化已被弃用

View File

@@ -0,0 +1,11 @@
using SqlSugar;
namespace Yi.Framework.Repository
{
/// <summary>
/// Sqlsugar自带工作单元何乐而不为呢
/// </summary>
public class UnitOfWork: SugarUnitOfWork
{
}
}

View File

@@ -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<List<RoleEntity>> DbTest()
{
return await _repository._Db.Queryable<RoleEntity>().ToListAsync();
}
//添加工作单元特性
[UnitOfWork]
public async Task<bool> 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<bool> GiveRoleSetMenu(List<long> roleIds, List<long> menuIds)
{
var _repositoryRoleMenu = _repository.ChangeRepository<Repository<RoleMenuEntity>>();

View File

@@ -10,7 +10,6 @@
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Interface\Yi.Framework.Interface.csproj" />
<ProjectReference Include="..\Yi.Framework.Uow\Yi.Framework.Uow.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -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
{

View File

@@ -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<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
//添加工作单元
services.AddSingleton<ISugarUnitOfWork<UnitOfWork>>(new SugarUnitOfWork<UnitOfWork>(sqlSugar));
}
}

View File

@@ -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");

View File

@@ -34,7 +34,6 @@
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
<ProjectReference Include="..\Yi.Framework.Service\Yi.Framework.Service.csproj" />
<ProjectReference Include="..\Yi.Framework.Uow\Yi.Framework.Uow.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -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}