515fceeb
“wangming”
框架初始化
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.MultiTenancy;
namespace Yi.Framework.Ddd.Application
{
/// <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)
{
}
}
public abstract class YiCacheCrudAppService<TEntity, TEntityDto, TKey, TGetListInput>
: YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected YiCacheCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class YiCacheCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput>
: YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected YiCacheCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class YiCacheCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: YiCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected YiCacheCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
/// <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>
{
/// <summary>
/// 分布式缓存访问器
/// </summary>
private IDistributedCache<TEntity> EntityCache =>
LazyServiceProvider.LazyGetRequiredService<IDistributedCache<TEntity>>();
/// <summary>
/// 获取缓存键
/// </summary>
protected virtual string GenerateCacheKey(TKey id) =>
$"{typeof(TEntity).Name}:{CurrentTenant.Id ?? Guid.Empty}:{id}";
protected YiCacheCrudAppService(IRepository<TEntity, TKey> repository)
: base(repository)
{
}
/// <summary>
/// 更新实体并清除缓存
/// </summary>
public override async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
{
var result = await base.UpdateAsync(id, input);
await EntityCache.RemoveAsync(GenerateCacheKey(id));
return result;
}
/// <summary>
/// 获取实体列表(需要继承实现具体的缓存策略)
/// </summary>
public override Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
{
// 建议实现两种缓存策略:
// 1. 全表缓存: 适用于数据量小且变动不频繁的场景
// 2. 按需缓存: 仅缓存常用数据,适用于大数据量场景
throw new NotImplementedException("请实现具体的缓存查询策略");
}
/// <summary>
/// 从数据库获取实体列表
/// </summary>
protected virtual Task<PagedResultDto<TGetListOutputDto>> GetListFromDatabaseAsync(
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)
{
return (await EntityCache.GetOrAddAsync(
GenerateCacheKey(id),
async () => await base.GetEntityByIdAsync(id)))!;
}
/// <summary>
/// 批量删除实体并清除缓存
/// </summary>
public override async Task DeleteAsync(IEnumerable<TKey> ids)
{
await base.DeleteAsync(ids);
// 批量清除缓存
var tasks = ids.Select(id =>
EntityCache.RemoveAsync(GenerateCacheKey(id)));
await Task.WhenAll(tasks);
}
}
}
|