refactor: ai+人工重构优化 framework
This commit is contained in:
@@ -6,11 +6,19 @@ using Volo.Abp.MultiTenancy;
|
||||
|
||||
namespace Yi.Framework.Ddd.Application
|
||||
{
|
||||
public abstract class YiCacheCrudAppService<TEntity, TEntityDto, TKey> : YiCrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TEntityDto : IEntityDto<TKey>
|
||||
/// <summary>
|
||||
/// 带缓存的CRUD应用服务基类
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">实体类型</typeparam>
|
||||
/// <typeparam name="TEntityDto">实体DTO类型</typeparam>
|
||||
/// <typeparam name="TKey">主键类型</typeparam>
|
||||
public abstract class YiCacheCrudAppService<TEntity, TEntityDto, TKey>
|
||||
: YiCrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TEntityDto : IEntityDto<TKey>
|
||||
{
|
||||
protected YiCacheCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
|
||||
protected YiCacheCrudAppService(IRepository<TEntity, TKey> repository)
|
||||
: base(repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -47,73 +55,92 @@ namespace Yi.Framework.Ddd.Application
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 完整的带缓存CRUD应用服务实现
|
||||
/// </summary>
|
||||
public abstract class YiCacheCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
|
||||
: YiCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TGetOutputDto : IEntityDto<TKey>
|
||||
where TGetListOutputDto : IEntityDto<TKey>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TGetOutputDto : IEntityDto<TKey>
|
||||
where TGetListOutputDto : IEntityDto<TKey>
|
||||
{
|
||||
protected IDistributedCache<TEntity> Cache => LazyServiceProvider.LazyGetRequiredService<IDistributedCache<TEntity>>();
|
||||
/// <summary>
|
||||
/// 分布式缓存访问器
|
||||
/// </summary>
|
||||
private IDistributedCache<TEntity> EntityCache =>
|
||||
LazyServiceProvider.LazyGetRequiredService<IDistributedCache<TEntity>>();
|
||||
|
||||
protected string GetCacheKey(TKey id) => typeof(TEntity).Name + ":" + CurrentTenant.Id ?? Guid.Empty + ":" + id.ToString();
|
||||
protected YiCacheCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
|
||||
/// <summary>
|
||||
/// 获取缓存键
|
||||
/// </summary>
|
||||
protected virtual string GenerateCacheKey(TKey id) =>
|
||||
$"{typeof(TEntity).Name}:{CurrentTenant.Id ?? Guid.Empty}:{id}";
|
||||
|
||||
protected YiCacheCrudAppService(IRepository<TEntity, TKey> repository)
|
||||
: base(repository)
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
|
||||
/// <summary>
|
||||
/// 更新实体并清除缓存
|
||||
/// </summary>
|
||||
public override async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
|
||||
{
|
||||
var output = await base.UpdateAsync(id, input);
|
||||
await Cache.RemoveAsync(GetCacheKey(id));
|
||||
return output;
|
||||
var result = await base.UpdateAsync(id, input);
|
||||
await EntityCache.RemoveAsync(GenerateCacheKey(id));
|
||||
return result;
|
||||
}
|
||||
|
||||
public override async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
|
||||
/// <summary>
|
||||
/// 获取实体列表(需要继承实现具体的缓存策略)
|
||||
/// </summary>
|
||||
public override Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
|
||||
{
|
||||
//两种方式:
|
||||
//1:全表缓存,使用缓存直接查询
|
||||
//2:非全部缓存,查询到的数据直接添加到缓存
|
||||
|
||||
//判断是否该实体为全表缓存
|
||||
throw new NotImplementedException();
|
||||
|
||||
//IDistributedCache 有局限性,条件查询无法进行缓存了
|
||||
//if (true)
|
||||
//{
|
||||
// return await GetListByCacheAsync(input);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// return await GetListByDbAsync(input);
|
||||
//}
|
||||
|
||||
// 建议实现两种缓存策略:
|
||||
// 1. 全表缓存: 适用于数据量小且变动不频繁的场景
|
||||
// 2. 按需缓存: 仅缓存常用数据,适用于大数据量场景
|
||||
throw new NotImplementedException("请实现具体的缓存查询策略");
|
||||
}
|
||||
|
||||
protected virtual async Task<PagedResultDto<TGetListOutputDto>> GetListByDbAsync(TGetListInput input)
|
||||
/// <summary>
|
||||
/// 从数据库获取实体列表
|
||||
/// </summary>
|
||||
protected virtual Task<PagedResultDto<TGetListOutputDto>> GetListFromDatabaseAsync(
|
||||
TGetListInput input)
|
||||
{
|
||||
//如果不是全表缓存,可以走这个啦
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
protected virtual async Task<PagedResultDto<TGetListOutputDto>> GetListByCacheAsync(TGetListInput input)
|
||||
{
|
||||
//如果是全表缓存,可以走这个啦
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从缓存获取实体列表
|
||||
/// </summary>
|
||||
protected virtual Task<PagedResultDto<TGetListOutputDto>> GetListFromCacheAsync(
|
||||
TGetListInput input)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取单个实体(优先从缓存获取)
|
||||
/// </summary>
|
||||
protected override async Task<TEntity> GetEntityByIdAsync(TKey id)
|
||||
{
|
||||
var output = await Cache.GetOrAddAsync(GetCacheKey(id), async () => await base.GetEntityByIdAsync(id));
|
||||
return output!;
|
||||
return (await EntityCache.GetOrAddAsync(
|
||||
GenerateCacheKey(id),
|
||||
async () => await base.GetEntityByIdAsync(id)))!;
|
||||
}
|
||||
|
||||
public override async Task DeleteAsync(IEnumerable<TKey> id)
|
||||
/// <summary>
|
||||
/// 批量删除实体并清除缓存
|
||||
/// </summary>
|
||||
public override async Task DeleteAsync(IEnumerable<TKey> ids)
|
||||
{
|
||||
await base.DeleteAsync(id);
|
||||
foreach (var itemId in id)
|
||||
{
|
||||
await Cache.RemoveAsync(GetCacheKey(itemId));
|
||||
}
|
||||
|
||||
await base.DeleteAsync(ids);
|
||||
|
||||
// 批量清除缓存
|
||||
var tasks = ids.Select(id =>
|
||||
EntityCache.RemoveAsync(GenerateCacheKey(id)));
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,183 +8,244 @@ using Volo.Abp.Domain.Repositories;
|
||||
|
||||
namespace Yi.Framework.Ddd.Application
|
||||
{
|
||||
public abstract class
|
||||
YiCrudAppService<TEntity, TEntityDto, TKey> : YiCrudAppService<TEntity, TEntityDto, TKey,
|
||||
PagedAndSortedResultRequestDto>
|
||||
/// <summary>
|
||||
/// CRUD应用服务基类 - 基础版本
|
||||
/// </summary>
|
||||
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey>
|
||||
: YiCrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TEntityDto : IEntityDto<TKey>
|
||||
{
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository)
|
||||
: base(repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// CRUD应用服务基类 - 支持自定义查询输入
|
||||
/// </summary>
|
||||
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput>
|
||||
: YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TEntityDto : IEntityDto<TKey>
|
||||
{
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository)
|
||||
: base(repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// CRUD应用服务基类 - 支持自定义创建输入
|
||||
/// </summary>
|
||||
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput>
|
||||
: YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TEntityDto : IEntityDto<TKey>
|
||||
{
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository)
|
||||
: base(repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// CRUD应用服务基类 - 支持自定义更新输入
|
||||
/// </summary>
|
||||
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
|
||||
: YiCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TEntityDto : IEntityDto<TKey>
|
||||
{
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository)
|
||||
: base(repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public abstract class YiCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput,
|
||||
TUpdateInput>
|
||||
/// <summary>
|
||||
/// CRUD应用服务基类 - 完整实现
|
||||
/// </summary>
|
||||
public abstract class YiCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
|
||||
: CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
|
||||
where TEntity : class, IEntity<TKey>
|
||||
where TGetOutputDto : IEntityDto<TKey>
|
||||
where TGetListOutputDto : IEntityDto<TKey>
|
||||
{
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
|
||||
/// <summary>
|
||||
/// 临时文件存储路径
|
||||
/// </summary>
|
||||
private const string TempFilePath = "/wwwroot/temp";
|
||||
|
||||
protected YiCrudAppService(IRepository<TEntity, TKey> repository)
|
||||
: base(repository)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新实体
|
||||
/// </summary>
|
||||
/// <param name="id">实体ID</param>
|
||||
/// <param name="input">更新输入</param>
|
||||
/// <returns>更新后的实体DTO</returns>
|
||||
public override async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
|
||||
{
|
||||
// 检查更新权限
|
||||
await CheckUpdatePolicyAsync();
|
||||
|
||||
// 获取并验证实体
|
||||
var entity = await GetEntityByIdAsync(id);
|
||||
await CheckUpdateInputDtoAsync(entity,input);
|
||||
|
||||
// 检查更新输入
|
||||
await CheckUpdateInputDtoAsync(entity, input);
|
||||
|
||||
// 映射并更新实体
|
||||
await MapToEntityAsync(input, entity);
|
||||
await Repository.UpdateAsync(entity, autoSave: true);
|
||||
|
||||
return await MapToGetOutputDtoAsync(entity);
|
||||
}
|
||||
|
||||
protected virtual Task CheckUpdateInputDtoAsync(TEntity entity,TUpdateInput input)
|
||||
/// <summary>
|
||||
/// 检查更新输入数据的有效性
|
||||
/// </summary>
|
||||
protected virtual Task CheckUpdateInputDtoAsync(TEntity entity, TUpdateInput input)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建实体
|
||||
/// </summary>
|
||||
/// <param name="input">创建输入</param>
|
||||
/// <returns>创建后的实体DTO</returns>
|
||||
public override async Task<TGetOutputDto> CreateAsync(TCreateInput input)
|
||||
{
|
||||
// 检查创建权限
|
||||
await CheckCreatePolicyAsync();
|
||||
|
||||
// 检查创建输入
|
||||
await CheckCreateInputDtoAsync(input);
|
||||
|
||||
// 映射到实体
|
||||
var entity = await MapToEntityAsync(input);
|
||||
|
||||
// 设置租户ID
|
||||
TryToSetTenantId(entity);
|
||||
|
||||
// 插入实体
|
||||
await Repository.InsertAsync(entity, autoSave: true);
|
||||
|
||||
return await MapToGetOutputDtoAsync(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查创建输入数据的有效性
|
||||
/// </summary>
|
||||
protected virtual Task CheckCreateInputDtoAsync(TCreateInput input)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 多查
|
||||
/// 获取实体列表
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
/// <param name="input">查询输入</param>
|
||||
/// <returns>分页结果</returns>
|
||||
public override async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
|
||||
{
|
||||
List<TEntity>? entites = null;
|
||||
//区分多查还是批量查
|
||||
List<TEntity> entities;
|
||||
|
||||
// 根据输入类型决定查询方式
|
||||
if (input is IPagedResultRequest pagedInput)
|
||||
{
|
||||
entites = await Repository.GetPagedListAsync(pagedInput.SkipCount, pagedInput.MaxResultCount,
|
||||
string.Empty);
|
||||
// 分页查询
|
||||
entities = await Repository.GetPagedListAsync(
|
||||
pagedInput.SkipCount,
|
||||
pagedInput.MaxResultCount,
|
||||
string.Empty
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
entites = await Repository.GetListAsync();
|
||||
// 查询全部
|
||||
entities = await Repository.GetListAsync();
|
||||
}
|
||||
|
||||
var total = await Repository.GetCountAsync();
|
||||
var output = await MapToGetListOutputDtosAsync(entites);
|
||||
return new PagedResultDto<TGetListOutputDto>(total, output);
|
||||
//throw new NotImplementedException($"【{typeof(TEntity)}】实体的CrudAppService,查询为具体业务,通用查询几乎无实际场景,请重写实现!");
|
||||
// 获取总数并映射结果
|
||||
var totalCount = await Repository.GetCountAsync();
|
||||
var dtos = await MapToGetListOutputDtosAsync(entities);
|
||||
|
||||
return new PagedResultDto<TGetListOutputDto>(totalCount, dtos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 多删
|
||||
/// 批量删除实体
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
/// <param name="ids">实体ID集合</param>
|
||||
[RemoteService(isEnabled: true)]
|
||||
public virtual async Task DeleteAsync(IEnumerable<TKey> id)
|
||||
public virtual async Task DeleteAsync(IEnumerable<TKey> ids)
|
||||
{
|
||||
await Repository.DeleteManyAsync(id);
|
||||
await Repository.DeleteManyAsync(ids);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 偷梁换柱
|
||||
/// 单个删除实体(禁用远程访问)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[RemoteService(isEnabled: false)]
|
||||
public override Task DeleteAsync(TKey id)
|
||||
{
|
||||
return base.DeleteAsync(id);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 导出excel
|
||||
/// 导出Excel
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
/// <param name="input">查询条件</param>
|
||||
/// <returns>Excel文件</returns>
|
||||
public virtual async Task<IActionResult> GetExportExcelAsync(TGetListInput input)
|
||||
{
|
||||
// 重置分页参数以获取全部数据
|
||||
if (input is IPagedResultRequest paged)
|
||||
{
|
||||
paged.SkipCount = 0;
|
||||
paged.MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount;
|
||||
}
|
||||
|
||||
var output = await this.GetListAsync(input);
|
||||
var dirPath = $"/wwwroot/temp";
|
||||
// 获取数据
|
||||
var output = await GetListAsync(input);
|
||||
|
||||
var fileName = $"{typeof(TEntity).Name}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}_{Guid.NewGuid()}";
|
||||
var filePath = $"{dirPath}/{fileName}.xlsx";
|
||||
if (!Directory.Exists(dirPath))
|
||||
// 确保临时目录存在
|
||||
if (!Directory.Exists(TempFilePath))
|
||||
{
|
||||
Directory.CreateDirectory(dirPath);
|
||||
Directory.CreateDirectory(TempFilePath);
|
||||
}
|
||||
|
||||
MiniExcel.SaveAs(filePath, output.Items);
|
||||
// 生成文件名和路径
|
||||
var fileName = GenerateExcelFileName();
|
||||
var filePath = Path.Combine(TempFilePath, fileName);
|
||||
|
||||
// 保存Excel文件
|
||||
await MiniExcel.SaveAsAsync(filePath, output.Items);
|
||||
|
||||
return new PhysicalFileResult(filePath, "application/vnd.ms-excel");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导入excle
|
||||
/// 生成Excel文件名
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task PostImportExcelAsync(List<TCreateInput> input)
|
||||
private string GenerateExcelFileName()
|
||||
{
|
||||
var entities = input.Select(x => MapToEntity(x)).ToList();
|
||||
//安全起见,该接口需要自己实现
|
||||
throw new NotImplementedException();
|
||||
//await Repository.DeleteManyAsync(entities.Select(x => x.Id));
|
||||
//await Repository.InsertManyAsync(entities);
|
||||
return $"{typeof(TEntity).Name}_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{Guid.NewGuid()}.xlsx";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导入Excel(需要实现类重写此方法)
|
||||
/// </summary>
|
||||
public virtual Task PostImportExcelAsync(List<TCreateInput> input)
|
||||
{
|
||||
throw new NotImplementedException("请在实现类中重写此方法以支持Excel导入");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,14 +6,34 @@ using Yi.Framework.Ddd.Application.Contracts;
|
||||
|
||||
namespace Yi.Framework.Ddd.Application
|
||||
{
|
||||
[DependsOn(typeof(AbpDddApplicationModule),
|
||||
typeof(YiFrameworkDddApplicationContractsModule))]
|
||||
/// <summary>
|
||||
/// Yi框架DDD应用层模块
|
||||
/// </summary>
|
||||
[DependsOn(
|
||||
typeof(AbpDddApplicationModule),
|
||||
typeof(YiFrameworkDddApplicationContractsModule)
|
||||
)]
|
||||
public class YiFrameworkDddApplicationModule : AbpModule
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用程序初始化配置
|
||||
/// </summary>
|
||||
/// <param name="context">应用程序初始化上下文</param>
|
||||
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
||||
{
|
||||
//分页限制
|
||||
// 配置分页查询的默认值和最大值限制
|
||||
ConfigureDefaultPagingSettings();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置默认分页设置
|
||||
/// </summary>
|
||||
private void ConfigureDefaultPagingSettings()
|
||||
{
|
||||
// 设置默认每页显示记录数
|
||||
LimitedResultRequestDto.DefaultMaxResultCount = 10;
|
||||
|
||||
// 设置最大允许的每页记录数
|
||||
LimitedResultRequestDto.MaxMaxResultCount = 10000;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user