命名空间:ZhonTai.Admin.Services / ZhonTai.Admin.Core.Repositories / ZhonTai.Admin.Core.RegisterModules
中台 Admin 采用 Autofac 作为 DI 容器,通过约定实现 Service 和 Repository 的自动注册,并提供基类以简化开发流程。
自动注册规则
RegisterModule 扫描 appconfig.json 中 AssemblyNames 指定的程序集,自动注册满足以下条件的类:
注册条件
- 类名以
Service 或 Repository 结尾
- 或 实现了
IRegisterIOC 接口
- 非抽象、非接口、public 访问级别
- 未标记
[NonRegisterIOC] 特性
默认生命周期
所有自动注册的类默认生命周期为 InstancePerLifetimeScope(Scoped)。
注册示例
1 2 3 4 5 6
| public class UserService : IBaseService { } public class UserRepository { }
public class MyHelper : IRegisterIOC { }
|
生命周期管理
通过特性可覆盖默认的 Scoped 生命周期。
| 特性 |
生命周期 |
适用场景 |
[InjectTransient] |
瞬时 |
每次请求创建新实例,适用于轻量级、无状态的工具类 |
[InjectScoped] |
作用域 |
每个 HTTP 请求创建一个实例(Service/Repository 默认值) |
[InjectSingleton] |
单例 |
全局唯一实例,适用于缓存、配置等共享资源 |
示例
1 2 3 4 5 6 7 8
| [InjectTransient] public class EmailHelper { }
[InjectScoped] public class OrderService { }
[InjectSingleton] public class CacheService { }
|
排除自动注册
标记 [NonRegisterIOC] 的类不会被自动注册,需手动处理:
1 2
| [NonRegisterIOC] public class MyCustomService { }
|
BaseService
服务基类,继承后可直接使用以下属性:
| 属性 |
类型 |
说明 |
Cache |
ICacheTool |
缓存工具 |
Mapper |
IMapper |
对象映射(Mapster) |
User |
IUser |
当前用户信息 |
Logger |
ILogger |
日志记录 |
ServiceProvider |
IServiceProvider |
服务提供者 |
使用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| using ZhonTai.Admin.Services;
public class ArticleService : BaseService { private readonly AppRepositoryBase<ArticleEntity> _repository;
public ArticleService(AppRepositoryBase<ArticleEntity> repository) { _repository = repository; }
public async Task<IResultOutput> CreateAsync(ArticleCreateInput input) { var entity = Mapper.Map<ArticleEntity>(input); await _repository.InsertAsync(entity); return ResultOutput.Ok(); }
public async Task<IResultOutput<ArticleDto>> GetAsync(long id) { var dto = await _repository.GetAsync<ArticleDto>(id); return ResultOutput.Ok(dto); } }
|
RepositoryBase
仓储基类继承 FreeSQL 的 BaseRepository,提供软删除、递归删除等增强方法。
注入方式
1 2 3 4 5 6 7 8 9 10
| public class ArticleService : BaseService { private readonly AppRepositoryBase<ArticleEntity> _repository;
public ArticleService(AppRepositoryBase<ArticleEntity> repository) { _repository = repository; } }
|
多数据库自定义仓储
当项目使用多数据库时,需继承 RepositoryBase 创建自定义仓储基类来指定数据库:
1 2 3 4 5 6 7 8 9 10 11 12
| using ZhonTai.Admin.Core.Consts; using ZhonTai.Admin.Core.Db.Transaction; using ZhonTai.Admin.Core.Repositories;
namespace MyApp.Api.Core.Repositories;
public class AppRepositoryBase<TEntity> : RepositoryBase<TEntity> where TEntity : class { public AppRepositoryBase(UnitOfWorkManagerCloud uowm) : base(DbKeys.AppDb, uowm) { } }
|
常用方法
| 方法 |
说明 |
GetAsync<TDto>(id) |
根据 Id 获取 DTO |
GetAsync<TDto>(Expression) |
根据条件获取 DTO |
InsertAsync(entity) |
插入数据 |
UpdateAsync(entity) |
更新数据 |
DeleteAsync(id) |
物理删除 |
SoftDeleteAsync(id) |
软删除(设置IsDeleted=true) |
SoftDeleteAsync(ids) |
批量软删除 |
SoftDeleteAsync(Expression) |
条件软删除 |
DeleteRecursiveAsync(Expression) |
递归物理删除 |
SoftDeleteRecursiveAsync(Expression) |
递归软删除 |
Select |
FreeSQL 查询构造器(支持Where/OrderBy/ToPage 等) |
软删除示例
1 2 3 4 5 6 7 8 9 10 11
| await _repository.SoftDeleteAsync(id);
await _repository.SoftDeleteAsync(new long[] { 1, 2, 3 });
await _repository.SoftDeleteAsync(a => a.Status == -1);
await _repository.SoftDeleteRecursiveAsync(a => a.Id == parentId);
|
禁用全局过滤器
软删除、租户等特性通过 FreeSQL 全局过滤器实现。需要时可临时禁用:
1 2 3 4 5
| await _repository.SoftDeleteAsync( a => a.Id == id, FilterNames.Delete, FilterNames.Tenant );
|
内置全局过滤器
| 过滤器名 |
说明 |
FilterNames.Delete |
过滤IsDeleted=false 的数据 |
FilterNames.Tenant |
过滤当前租户数据 |
FilterNames.Data |
数据权限过滤 |
FilterNames.Self |
仅本人数据 |
FilterNames.Member |
会员数据过滤 |
懒加载服务
BaseService 提供 LazyGetRequiredService<T>() 方法,用于在需要时获取服务,避免构造函数注入过多依赖:
1 2 3 4 5 6 7 8 9
| public class MyService : BaseService { public async Task DoSomething() { var emailService = LazyGetRequiredService<IEmailService>(); await emailService.SendAsync(...); } }
|
#中台 #中台/DI生命周期 #中台/特性注解 #Code/C#