非常优雅的完成了数据模块

This commit is contained in:
橙子
2023-01-20 20:03:25 +08:00
parent 98375f8629
commit fceefac0ee
26 changed files with 185 additions and 32 deletions

View File

@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Core.Sqlsugar.Extensions
{
public static class SqlsugarDataFilterExtensions
{
public static IApplicationBuilder UseSqlsugarDataFiterServer(this IApplicationBuilder builder)
{
return builder.UseMiddleware<SqlsugarDataFilterMiddleware>();
}
}
public class SqlsugarDataFilterMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<SqlsugarDataFilterMiddleware> _logger;
public SqlsugarDataFilterMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
this._next = next;
this._logger = loggerFactory.CreateLogger<SqlsugarDataFilterMiddleware>();
}
public async Task InvokeAsync(HttpContext context)
{
await _next(context);
}
}
}

View File

@@ -81,7 +81,6 @@ namespace Yi.Framework.Core.Sqlsugar.Extensions
{ {
action(db); action(db);
} }
db.Aop.DataExecuting = (oldValue, entityInfo) => db.Aop.DataExecuting = (oldValue, entityInfo) =>
{ {

View File

@@ -9,10 +9,10 @@ using Yi.Framework.Data.Filters;
namespace Yi.Framework.Core.Sqlsugar.Filters namespace Yi.Framework.Core.Sqlsugar.Filters
{ {
public class SqlsugarDataFiter : IDataFilter public class SqlsugarDataFilter : IDataFilter
{ {
private ISqlSugarClient _Db { get; set; } private ISqlSugarClient _Db { get; set; }
public SqlsugarDataFiter(ISqlSugarClient sqlSugarClient) public SqlsugarDataFilter(ISqlSugarClient sqlSugarClient)
{ {
_Db = sqlSugarClient; _Db = sqlSugarClient;
} }
@@ -24,24 +24,27 @@ namespace Yi.Framework.Core.Sqlsugar.Filters
public IDisposable Disable<TFilter>() where TFilter : class public IDisposable Disable<TFilter>() where TFilter : class
{ {
_Db.QueryFilter.ClearAndBackup<TFilter>(); _Db.QueryFilter.ClearAndBackup<TFilter>();
throw new NotImplementedException(); return this;
} }
public IDisposable Enable<TFilter>() where TFilter : class public IDisposable Enable<TFilter>() where TFilter : class
{ {
_Db.QueryFilter.Restore(); throw new NotImplementedException("暂时没有单独还原过滤器的方式");
throw new NotImplementedException();
} }
public bool IsEnabled<TFilter>() where TFilter : class public bool IsEnabled<TFilter>() where TFilter : class
{ {
throw new NotImplementedException(); throw new NotImplementedException("暂时没有判断过滤器的方式");
} }
public void RemoveFilter<TFilter>() where TFilter : class public void RemoveFilter<TFilter>() where TFilter : class
{ {
_Db.QueryFilter.Clear<TFilter>(); _Db.QueryFilter.Clear<TFilter>();
throw new NotImplementedException(); }
public void Dispose()
{
_Db.QueryFilter.Restore();
} }
} }
} }

View File

@@ -7,7 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="SqlSugarCore" Version="5.1.3.46-preview10" /> <PackageReference Include="SqlSugarCore" Version="5.1.3.46-preview11" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -3,9 +3,11 @@ using Microsoft.Extensions.DependencyInjection;
using StartupModules; using StartupModules;
using Yi.Framework.Core.Configuration; using Yi.Framework.Core.Configuration;
using Yi.Framework.Core.Sqlsugar.Extensions; using Yi.Framework.Core.Sqlsugar.Extensions;
using Yi.Framework.Core.Sqlsugar.Filters;
using Yi.Framework.Core.Sqlsugar.Options; using Yi.Framework.Core.Sqlsugar.Options;
using Yi.Framework.Core.Sqlsugar.Repositories; using Yi.Framework.Core.Sqlsugar.Repositories;
using Yi.Framework.Core.Sqlsugar.Uow; using Yi.Framework.Core.Sqlsugar.Uow;
using Yi.Framework.Data.Filters;
using Yi.Framework.Ddd; using Yi.Framework.Ddd;
using Yi.Framework.Ddd.Repositories; using Yi.Framework.Ddd.Repositories;
using Yi.Framework.Uow; using Yi.Framework.Uow;
@@ -26,7 +28,7 @@ namespace Yi.Framework.Core.Sqlsugar
services.AddSingleton<IUnitOfWorkManager, UnitOfWorkManager>(); services.AddSingleton<IUnitOfWorkManager, UnitOfWorkManager>();
//这里替换过滤器实现 //这里替换过滤器实现
services.AddScoped<IDataFilter, SqlsugarDataFilter>();
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions")); services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));
services.AddSqlsugarServer(); services.AddSqlsugarServer();

View File

@@ -12,7 +12,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.3.46-preview10" /> <PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.3.46-preview11" />
<PackageReference Include="StartupModules" Version="4.0.0" /> <PackageReference Include="StartupModules" Version="4.0.0" />
</ItemGroup> </ItemGroup>

View File

@@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Data.Entities;
using Yi.Framework.Data.Filters;
namespace Yi.Framework.Data.Extensions
{
public static class DataFilterExtensions
{
public static IApplicationBuilder UseDataFiterServer(this IApplicationBuilder builder)
{
return builder.UseMiddleware<DataFilterMiddleware>();
}
}
public class DataFilterMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<DataFilterMiddleware> _logger;
public DataFilterMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
this._next = next;
this._logger = loggerFactory.CreateLogger<DataFilterMiddleware>();
}
public async Task InvokeAsync(HttpContext context, IDataFilter dataFilter)
{
//添加默认的过滤器
dataFilter.AddFilter<ISoftDelete>(u => u.IsDeleted == false);
//dataFilter.AddFilter<IMultiTenant>(u => u.TenantId == Guid.Empty);
await _next(context);
}
}
}

View File

@@ -21,12 +21,12 @@ namespace Yi.Framework.Data.Filters
public IDisposable Disable<TFilter>() where TFilter : class public IDisposable Disable<TFilter>() where TFilter : class
{ {
return null; return this;
} }
public IDisposable Enable<TFilter>() where TFilter : class public IDisposable Enable<TFilter>() where TFilter : class
{ {
return null; return this;
} }
public bool IsEnabled<TFilter>() where TFilter : class public bool IsEnabled<TFilter>() where TFilter : class
@@ -44,5 +44,10 @@ namespace Yi.Framework.Data.Filters
public void AddFilter<TFilter>(Expression<Func<TFilter, bool>> expression) where TFilter : class public void AddFilter<TFilter>(Expression<Func<TFilter, bool>> expression) where TFilter : class
{ {
} }
public void Dispose()
{
}
} }
} }

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Yi.Framework.Data.Filters namespace Yi.Framework.Data.Filters
{ {
public interface IDataFilter public interface IDataFilter:IDisposable
{ {
IDisposable Enable<TFilter>() where TFilter :class; IDisposable Enable<TFilter>() where TFilter :class;

View File

@@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Core.Attributes; using Yi.Framework.Core.Attributes;
using Yi.Framework.Data.Entities; using Yi.Framework.Data.Entities;
using Yi.Framework.Data.Extensions;
using Yi.Framework.Data.Filters; using Yi.Framework.Data.Filters;
using Yi.Framework.Ddd; using Yi.Framework.Ddd;
@@ -19,12 +20,8 @@ namespace Yi.Framework.Data
{ {
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context) public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
{ {
var dataFilter = app.ApplicationServices.GetRequiredService<IDataFilter>(); //使用了过滤器
//内置多租户与软删除过滤 app.UseDataFiterServer();
dataFilter.AddFilter<ISoftDelete>(u => u.IsDeleted == false);
//租户id从租户管理中获取
dataFilter.AddFilter<IMultiTenant>(u => u.TenantId == Guid.Empty);
} }
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)

View File

@@ -9,6 +9,11 @@
Student输入创建对象 Student输入创建对象
</summary> </summary>
</member> </member>
<member name="P:Yi.Framework.Application.Contracts.Student.Dtos.StudentGetListOutputDto.IsDeleted">
<summary>
想看一下结果
</summary>
</member>
<member name="T:Yi.Framework.Application.Contracts.Student.IStudentService"> <member name="T:Yi.Framework.Application.Contracts.Student.IStudentService">
<summary> <summary>
服务抽象 服务抽象

View File

@@ -12,5 +12,10 @@ namespace Yi.Framework.Application.Contracts.Student.Dtos
public long Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public long Number { get; set; } public long Number { get; set; }
/// <summary>
/// 想看一下结果
/// </summary>
public bool IsDeleted { get; set; }
} }
} }

View File

@@ -8,9 +8,10 @@ namespace Yi.Framework.Application.Contracts.Student.Dtos
{ {
public class StudentUpdateInputVo public class StudentUpdateInputVo
{ {
public long Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public long? Number { get; set; } public long? Number { get; set; }
public bool IsDeleted { get; set; }
} }
} }

View File

@@ -9,6 +9,13 @@
服务实现 服务实现
</summary> </summary>
</member> </member>
<member name="M:Yi.Framework.Application.Student.StudentService.GetDataFiterTestAsync(Yi.Framework.Application.Contracts.Student.Dtos.StudentGetListInputVo)">
<summary>
数据过滤测试
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.Application.Student.StudentService.GetToken"> <member name="M:Yi.Framework.Application.Student.StudentService.GetToken">
<summary> <summary>
测试token 测试token

View File

@@ -5,7 +5,6 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Application.Contracts.Student; using Yi.Framework.Application.Contracts.Student;
using Yi.Framework.Domain.Student; using Yi.Framework.Domain.Student;
using Yi.Framework.Domain.Student.IRepository;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NET.AutoWebApi.Setting; using NET.AutoWebApi.Setting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@@ -21,6 +20,10 @@ using Yi.Framework.Core.Const;
using Yi.Framework.Core.CurrentUsers; using Yi.Framework.Core.CurrentUsers;
using Yi.Framework.Auth.JwtBearer.Authorization; using Yi.Framework.Auth.JwtBearer.Authorization;
using Yi.Framework.Domain.Shared.Student.ConstClasses; using Yi.Framework.Domain.Shared.Student.ConstClasses;
using Yi.Framework.Domain.Student.Repositories;
using Yi.Framework.Data.Filters;
using Yi.Framework.Data.Entities;
using Yi.Framework.Ddd.Dtos;
namespace Yi.Framework.Application.Student namespace Yi.Framework.Application.Student
{ {
@@ -37,13 +40,36 @@ namespace Yi.Framework.Application.Student
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly JwtTokenManager _jwtTokenManager; private readonly JwtTokenManager _jwtTokenManager;
private readonly ICurrentUser _currentUser; private readonly ICurrentUser _currentUser;
public StudentService(IStudentRepository studentRepository, StudentManager studentManager, IUnitOfWorkManager unitOfWorkManager, JwtTokenManager jwtTokenManager, ICurrentUser currentUser) private readonly IDataFilter _dataFilter;
public StudentService(IStudentRepository studentRepository, StudentManager studentManager, IUnitOfWorkManager unitOfWorkManager, JwtTokenManager jwtTokenManager, ICurrentUser currentUser, IDataFilter dataFilter)
{ {
_studentRepository = studentRepository; _studentRepository = studentRepository;
_studentManager = studentManager; _studentManager = studentManager;
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
_jwtTokenManager = jwtTokenManager; _jwtTokenManager = jwtTokenManager;
_currentUser = currentUser; _currentUser = currentUser;
_dataFilter = dataFilter;
}
/// <summary>
/// 数据过滤测试
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PagedResultDto<StudentGetListOutputDto>> GetDataFiterTestAsync(StudentGetListInputVo input)
{
PagedResultDto<StudentGetListOutputDto> res = new();
using (_dataFilter.Disable<ISoftDelete>())
{
res = await base.GetListAsync(input);
}
var p = await base.GetListAsync(input);
return res;
} }
/// <summary> /// <summary>

View File

@@ -11,6 +11,8 @@ using Yi.Framework.Application.Contracts.Student;
using Yi.Framework.Application.Student; using Yi.Framework.Application.Student;
using Yi.Framework.Auth.JwtBearer; using Yi.Framework.Auth.JwtBearer;
using Yi.Framework.Core.Attributes; using Yi.Framework.Core.Attributes;
using Yi.Framework.Data;
using Yi.Framework.Ddd;
using Yi.Framework.Domain; using Yi.Framework.Domain;
namespace Yi.Framework.Application namespace Yi.Framework.Application
@@ -18,7 +20,7 @@ namespace Yi.Framework.Application
[DependsOn( [DependsOn(
typeof(YiFrameworkApplicationContractsModule), typeof(YiFrameworkApplicationContractsModule),
typeof(YiFrameworkDomainModule), typeof(YiFrameworkDomainModule),
typeof(YiFrameworkAuthJwtBearerModule) typeof(YiFrameworkAuthJwtBearerModule)
)] )]
public class YiFrameworkApplicationModule : IStartupModule public class YiFrameworkApplicationModule : IStartupModule
{ {

View File

@@ -19,7 +19,12 @@
学号 学号
</summary> </summary>
</member> </member>
<member name="T:Yi.Framework.Domain.Student.IRepository.IStudentRepository"> <member name="P:Yi.Framework.Domain.Student.Entities.StudentEntity.IsDeleted">
<summary>
软删除
</summary>
</member>
<member name="T:Yi.Framework.Domain.Student.Repositories.IStudentRepository">
<summary> <summary>
仓储抽象 仓储抽象
</summary> </summary>

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Data.Entities;
using Yi.Framework.Ddd.Entities; using Yi.Framework.Ddd.Entities;
namespace Yi.Framework.Domain.Student.Entities namespace Yi.Framework.Domain.Student.Entities
@@ -12,7 +13,7 @@ namespace Yi.Framework.Domain.Student.Entities
/// 学生实体 /// 学生实体
/// </summary> /// </summary>
[SugarTable("Student")] [SugarTable("Student")]
public class StudentEntity : IEntity<long> public class StudentEntity : IEntity<long>, ISoftDelete
{ {
[SugarColumn(IsPrimaryKey = true)] [SugarColumn(IsPrimaryKey = true)]
public long Id { get; set; } public long Id { get; set; }
@@ -26,5 +27,10 @@ namespace Yi.Framework.Domain.Student.Entities
/// 学号 /// 学号
/// </summary> /// </summary>
public long Number { get;set ; } public long Number { get;set ; }
/// <summary>
/// 软删除
/// </summary>
public bool IsDeleted { get; set; }
} }
} }

View File

@@ -6,12 +6,12 @@ using System.Threading.Tasks;
using Yi.Framework.Ddd.Repositories; using Yi.Framework.Ddd.Repositories;
using Yi.Framework.Domain.Student.Entities; using Yi.Framework.Domain.Student.Entities;
namespace Yi.Framework.Domain.Student.IRepository namespace Yi.Framework.Domain.Student.Repositories
{ {
/// <summary> /// <summary>
/// 仓储抽象 /// 仓储抽象
/// </summary> /// </summary>
public interface IStudentRepository:IRepository<StudentEntity> public interface IStudentRepository : IRepository<StudentEntity>
{ {
Task<List<StudentEntity>> GetMyListAsync(); Task<List<StudentEntity>> GetMyListAsync();
} }

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Domain.Student.IRepository; using Yi.Framework.Domain.Student.Repositories;
namespace Yi.Framework.Domain.Student namespace Yi.Framework.Domain.Student
{ {

View File

@@ -11,6 +11,7 @@
<Compile Include="..\..\GlobalUsings.cs" Link="Properties\GlobalUsings.cs" /> <Compile Include="..\..\GlobalUsings.cs" Link="Properties\GlobalUsings.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\framework\Yi.Framework.Data\Yi.Framework.Data.csproj" />
<ProjectReference Include="..\Yi.Framework.Domain.Shared\Yi.Framework.Domain.Shared.csproj" /> <ProjectReference Include="..\Yi.Framework.Domain.Shared\Yi.Framework.Domain.Shared.csproj" />
</ItemGroup> </ItemGroup>

View File

@@ -7,13 +7,15 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Core.Attributes; using Yi.Framework.Core.Attributes;
using Yi.Framework.Data;
using Yi.Framework.Domain.Shared; using Yi.Framework.Domain.Shared;
using Yi.Framework.Domain.Student; using Yi.Framework.Domain.Student;
namespace Yi.Framework.Domain namespace Yi.Framework.Domain
{ {
[DependsOn( [DependsOn(
typeof(YiFrameworkDomainSharedModule) typeof(YiFrameworkDomainSharedModule),
typeof(YiFrameworkDataModule)
)] )]
public class YiFrameworkDomainModule : IStartupModule public class YiFrameworkDomainModule : IStartupModule
{ {

View File

@@ -6,7 +6,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Core.Sqlsugar.Repositories; using Yi.Framework.Core.Sqlsugar.Repositories;
using Yi.Framework.Domain.Student.Entities; using Yi.Framework.Domain.Student.Entities;
using Yi.Framework.Domain.Student.IRepository; using Yi.Framework.Domain.Student.Repositories;
namespace Yi.Framework.Sqlsugar.Student namespace Yi.Framework.Sqlsugar.Student
{ {

View File

@@ -9,7 +9,7 @@ using System.Threading.Tasks;
using Yi.Framework.Core.Attributes; using Yi.Framework.Core.Attributes;
using Yi.Framework.Core.Sqlsugar; using Yi.Framework.Core.Sqlsugar;
using Yi.Framework.Domain; using Yi.Framework.Domain;
using Yi.Framework.Domain.Student.IRepository; using Yi.Framework.Domain.Student.Repositories;
using Yi.Framework.Sqlsugar.Student; using Yi.Framework.Sqlsugar.Student;
namespace Yi.Framework.Sqlsugar namespace Yi.Framework.Sqlsugar

View File

@@ -65,3 +65,12 @@
2023:01:19-17:52:49本次运行启动时间为1940毫秒 2023:01:19-17:52:49本次运行启动时间为1940毫秒
2023:01:19-17:54:41本次运行启动时间为1861毫秒 2023:01:19-17:54:41本次运行启动时间为1861毫秒
2023:01:19-17:57:37本次运行启动时间为1945毫秒 2023:01:19-17:57:37本次运行启动时间为1945毫秒
2023:01:20-18:25:59本次运行启动时间为43382毫秒
2023:01:20-18:29:09本次运行启动时间为2173毫秒
2023:01:20-18:32:14本次运行启动时间为2397毫秒
2023:01:20-18:34:14本次运行启动时间为2126毫秒
2023:01:20-18:38:36本次运行启动时间为2152毫秒
2023:01:20-18:45:15本次运行启动时间为7203毫秒
2023:01:20-18:50:46本次运行启动时间为6513毫秒
2023:01:20-18:53:16本次运行启动时间为5186毫秒
2023:01:20-19:01:36本次运行启动时间为5194毫秒