diff --git a/src/CellularManagement.Domain/Repositories/IBaseRepository.cs b/src/CellularManagement.Domain/Repositories/IBaseRepository.cs new file mode 100644 index 0000000..4c8d318 --- /dev/null +++ b/src/CellularManagement.Domain/Repositories/IBaseRepository.cs @@ -0,0 +1,229 @@ +using CellularManagement.Domain.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; +/// +/// 基础仓储接口 +/// 组合命令和查询仓储的功能 +/// +/// 实体类型 +namespace CellularManagement.Domain.Repositories +{ + public interface IBaseRepository where T : class + { + // + /// 添加单个实体到数据库 + /// + /// 要添加的实体 + /// 取消令牌,用于取消异步操作 + /// 添加后的实体 + /// + /// 这是一个异步操作,因为需要等待数据库的插入操作完成 + /// 实体添加后,其ID和其他数据库生成的字段会被填充 + /// + Task AddAsync(T entity, CancellationToken cancellationToken = default); + + /// + /// 批量添加多个实体到数据库 + /// + /// 要添加的实体集合 + /// 取消令牌,用于取消异步操作 + /// 添加后的实体集合 + /// + /// 这是一个异步操作,因为需要等待数据库的批量插入操作完成 + /// 批量添加比逐个添加更高效,特别是在大量数据时 + /// + Task> AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default); + + /// + /// 更新单个实体的状态 + /// + /// 要更新的实体 + /// + /// 这是一个同步操作,因为它只是标记实体的状态为已修改 + /// 实际的数据库更新操作由 UnitOfWork 在事务提交时执行 + /// + void Update(T entity); + + /// + /// 批量更新多个实体的状态 + /// + /// 要更新的实体集合 + /// + /// 这是一个同步操作,因为它只是标记实体的状态为已修改 + /// 批量更新比逐个更新更高效,特别是在大量数据时 + /// + void UpdateRange(IEnumerable entities); + + /// + /// 删除单个实体 + /// + /// 要删除的实体 + /// + /// 这是一个同步操作,因为它只是标记实体的状态为已删除 + /// 实际的数据库删除操作由 UnitOfWork 在事务提交时执行 + /// 如果实体实现了 ISoftDelete 接口,将执行软删除 + /// + void Delete(T entity); + + /// + /// 批量删除多个实体 + /// + /// 要删除的实体集合 + /// + /// 这是一个同步操作,因为它只是标记实体的状态为已删除 + /// 批量删除比逐个删除更高效,特别是在大量数据时 + /// + void DeleteRange(IEnumerable entities); + + /// + /// 根据ID删除实体 + /// + /// 要删除的实体的ID + /// 取消令牌,用于取消异步操作 + /// 是否成功找到并删除实体 + /// + /// 这是一个异步操作,因为需要先查询数据库找到实体 + /// 如果实体不存在,返回 false + /// 如果实体存在,将其标记为已删除 + /// + Task DeleteByIdAsync(string id, CancellationToken cancellationToken = default); + + /// + /// 根据条件删除实体 + /// + /// 删除条件表达式 + /// 取消令牌,用于取消异步操作 + /// 删除的实体数量 + /// + /// 这是一个异步操作,因为需要先查询数据库找到符合条件的实体 + /// 使用 LINQ 表达式树构建查询条件 + /// 返回实际删除的实体数量 + /// + Task DeleteWhereAsync(Expression> predicate, CancellationToken cancellationToken = default); + + + /// + /// 根据ID查询单个实体 + /// + /// 要查询的实体ID + /// 取消令牌,用于取消异步操作 + /// 查询到的实体,如果不存在则返回 null + /// + /// 这是一个异步操作,因为需要等待数据库的查询操作完成 + /// 使用主键进行查询,性能最优 + /// + Task GetByIdAsync(string id, CancellationToken cancellationToken = default); + + /// + /// 查询所有实体 + /// + /// 取消令牌,用于取消异步操作 + /// 所有实体的集合 + /// + /// 这是一个异步操作,因为需要等待数据库的查询操作完成 + /// 注意:对于大数据量的表,建议使用分页查询 + /// + Task> GetAllAsync(CancellationToken cancellationToken = default); + + /// + /// 根据条件查询实体 + /// + /// 查询条件表达式 + /// 取消令牌,用于取消异步操作 + /// 符合条件的实体集合 + /// + /// 这是一个异步操作,因为需要等待数据库的查询操作完成 + /// 使用 LINQ 表达式树构建查询条件 + /// 条件查询会被转换为 SQL 语句在数据库端执行 + /// + Task> FindAsync(Expression> predicate, CancellationToken cancellationToken = default); + + /// + /// 分页查询实体 + /// + /// 页码,从1开始 + /// 每页记录数 + /// 取消令牌,用于取消异步操作 + /// 分页查询结果,包含总记录数和当前页数据 + /// + /// 这是一个异步操作,因为需要等待数据库的查询操作完成 + /// 分页查询可以有效减少数据传输量,提高性能 + /// 建议在查询大数据量时使用 + /// + Task<(int TotalCount, IEnumerable Items)> GetPagedAsync(int pageNumber, int pageSize, CancellationToken cancellationToken = default); + + /// + /// 根据条件分页查询实体 + /// + /// 查询条件表达式 + /// 页码,从1开始 + /// 每页记录数 + /// 取消令牌,用于取消异步操作 + /// 分页查询结果,包含总记录数和当前页数据 + /// + /// 这是一个异步操作,因为需要等待数据库的查询操作完成 + /// 结合条件查询和分页查询,适用于大数据量的条件筛选 + /// + Task<(int TotalCount, IEnumerable Items)> GetPagedAsync( + Expression> predicate, + int pageNumber, + int pageSize, + CancellationToken cancellationToken = default); + + /// + /// 查询单个实体 + /// + /// 查询条件表达式 + /// 取消令牌,用于取消异步操作 + /// 查询到的实体,如果不存在则返回 null + /// + /// 这是一个异步操作,因为需要等待数据库的查询操作完成 + /// 如果查询结果包含多个实体,将返回第一个 + /// 建议在确定只会返回一个结果时使用 + /// + Task FirstOrDefaultAsync(Expression> predicate, CancellationToken cancellationToken = default); + + /// + /// 查询实体是否存在 + /// + /// 查询条件表达式 + /// 取消令牌,用于取消异步操作 + /// 是否存在符合条件的实体 + /// + /// 这是一个异步操作,因为需要等待数据库的查询操作完成 + /// 使用 EXISTS 语句在数据库端执行,性能优于获取完整实体 + /// + Task AnyAsync(Expression> predicate, CancellationToken cancellationToken = default); + + /// + /// 统计符合条件的实体数量 + /// + /// 查询条件表达式 + /// 取消令牌,用于取消异步操作 + /// 符合条件的实体数量 + /// + /// 这是一个异步操作,因为需要等待数据库的查询操作完成 + /// 使用 COUNT 语句在数据库端执行,性能优于获取完整实体 + /// + Task CountAsync(Expression> predicate, CancellationToken cancellationToken = default); + + /// + /// 执行SQL复制查询 + /// + /// SQL查询语句 + /// SQL参数 + /// 取消令牌,用于取消异步操作 + /// 查询结果集合 + /// + /// 这是一个异步操作,用于执行自定义SQL查询 + /// 支持参数化查询,防止SQL注入 + /// 返回的结果会被映射到实体类型T + /// + Task> ExecuteSqlQueryAsync(string sql, object[] parameters, CancellationToken cancellationToken = default); + } +} + diff --git a/src/CellularManagement.Domain/Repositories/ICellularDeviceRepository.cs b/src/CellularManagement.Domain/Repositories/ICellularDeviceRepository.cs index 06f8ab5..42411f1 100644 --- a/src/CellularManagement.Domain/Repositories/ICellularDeviceRepository.cs +++ b/src/CellularManagement.Domain/Repositories/ICellularDeviceRepository.cs @@ -4,11 +4,12 @@ using CellularManagement.Domain.Entities.Device; namespace CellularManagement.Domain.Repositories; /// -/// 蜂窝设备命令仓储接口 -/// 负责蜂窝设备的写入操作 +/// 蜂窝设备仓储接口 +/// 组合命令和查询仓储接口 /// -public interface ICellularDeviceCommandRepository : ICommandRepository +public interface ICellularDeviceRepository : IBaseRepository { + /// /// 添加蜂窝设备 /// @@ -38,14 +39,7 @@ public interface ICellularDeviceCommandRepository : ICommandRepository Task DeleteDevicesAsync(IEnumerable ids, CancellationToken cancellationToken = default); -} -/// -/// 蜂窝设备查询仓储接口 -/// 负责蜂窝设备的读取操作 -/// -public interface ICellularDeviceQueryRepository : IQueryRepository -{ /// /// 获取所有蜂窝设备 /// @@ -89,12 +83,4 @@ public interface ICellularDeviceQueryRepository : IQueryRepository Task SerialNumberExistsAsync(string serialNumber, CancellationToken cancellationToken = default); -} - -/// -/// 蜂窝设备仓储接口 -/// 组合命令和查询仓储接口 -/// -public interface ICellularDeviceRepository : ICellularDeviceCommandRepository, ICellularDeviceQueryRepository -{ } \ No newline at end of file diff --git a/src/CellularManagement.Domain/Repositories/ILoginLogRepository.cs b/src/CellularManagement.Domain/Repositories/ILoginLogRepository.cs index 57a6126..ebc2a8c 100644 --- a/src/CellularManagement.Domain/Repositories/ILoginLogRepository.cs +++ b/src/CellularManagement.Domain/Repositories/ILoginLogRepository.cs @@ -8,7 +8,7 @@ namespace CellularManagement.Domain.Repositories; /// /// 登录日志仓储接口 /// -public interface ILoginLogRepository : ICommandRepository, IQueryRepository +public interface ILoginLogRepository : IBaseRepository { /// /// 添加登录日志 diff --git a/src/CellularManagement.Domain/Repositories/IPermissionRepository.cs b/src/CellularManagement.Domain/Repositories/IPermissionRepository.cs index b01900f..f3daac2 100644 --- a/src/CellularManagement.Domain/Repositories/IPermissionRepository.cs +++ b/src/CellularManagement.Domain/Repositories/IPermissionRepository.cs @@ -6,11 +6,16 @@ using CellularManagement.Domain.Entities; namespace CellularManagement.Domain.Repositories; /// -/// 权限命令仓储接口 -/// 负责权限的写入操作 +/// 权限仓储接口 +/// 组合命令和查询仓储接口 /// -public interface IPermissionCommandRepository : ICommandRepository +public interface IPermissionRepository : IBaseRepository { + /// + /// 根据名称获取权限 + /// + Task GetByNameAsync(string name, CancellationToken cancellationToken = default); + /// /// 更新权限 /// @@ -20,25 +25,4 @@ public interface IPermissionCommandRepository : ICommandRepository /// 删除权限 /// Task DeleteAsync(Permission permission, CancellationToken cancellationToken = default); -} - -/// -/// 权限查询仓储接口 -/// 负责权限的读取操作 -/// -public interface IPermissionQueryRepository : IQueryRepository -{ - /// - /// 根据名称获取权限 - /// - Task GetByNameAsync(string name, CancellationToken cancellationToken = default); -} - -/// -/// 权限仓储接口 -/// 组合命令和查询仓储接口 -/// -public interface IPermissionRepository : IPermissionCommandRepository, IPermissionQueryRepository -{ - } \ No newline at end of file diff --git a/src/CellularManagement.Domain/Repositories/IRolePermissionRepository.cs b/src/CellularManagement.Domain/Repositories/IRolePermissionRepository.cs index 2f49ecd..afab4e2 100644 --- a/src/CellularManagement.Domain/Repositories/IRolePermissionRepository.cs +++ b/src/CellularManagement.Domain/Repositories/IRolePermissionRepository.cs @@ -6,7 +6,7 @@ namespace CellularManagement.Domain.Repositories; /// 角色权限仓储接口 /// 遵循 DDD 设计模式,提供角色权限相关的领域操作 /// -public interface IRolePermissionRepository : ICommandRepository, IQueryRepository +public interface IRolePermissionRepository : IBaseRepository { /// /// 根据角色ID获取所有权限 diff --git a/src/CellularManagement.Domain/Repositories/IUserRoleRepository.cs b/src/CellularManagement.Domain/Repositories/IUserRoleRepository.cs index 9b59213..fee941c 100644 --- a/src/CellularManagement.Domain/Repositories/IUserRoleRepository.cs +++ b/src/CellularManagement.Domain/Repositories/IUserRoleRepository.cs @@ -5,11 +5,12 @@ using CellularManagement.Domain.Entities; namespace CellularManagement.Domain.Repositories; + /// -/// 用户角色命令仓储接口 -/// 负责用户角色关系的写入操作 +/// 用户角色仓储接口 +/// 组合命令和查询仓储接口 /// -public interface IUserRoleCommandRepository : ICommandRepository +public interface IUserRoleRepository : IBaseRepository { /// /// 添加用户角色关系 @@ -30,14 +31,8 @@ public interface IUserRoleCommandRepository : ICommandRepository /// 批量删除用户角色关系 /// Task DeleteUserRolesAsync(string userId, IEnumerable roleIds, CancellationToken cancellationToken = default); -} -/// -/// 用户角色查询仓储接口 -/// 负责用户角色关系的读取操作 -/// -public interface IUserRoleQueryRepository : IQueryRepository -{ + /// /// 获取用户的所有角色 /// @@ -57,13 +52,4 @@ public interface IUserRoleQueryRepository : IQueryRepository /// 获取用户角色关系 /// Task GetUserRoleAsync(string userId, string roleId, CancellationToken cancellationToken = default); -} - -/// -/// 用户角色仓储接口 -/// 组合命令和查询仓储接口 -/// -public interface IUserRoleRepository : IUserRoleCommandRepository, IUserRoleQueryRepository -{ - -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/CellularManagement.Infrastructure/Repositories/Base/IBaseRepository.cs b/src/CellularManagement.Infrastructure/Repositories/Base/IBaseRepository.cs deleted file mode 100644 index 9029a15..0000000 --- a/src/CellularManagement.Infrastructure/Repositories/Base/IBaseRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using CellularManagement.Domain.Repositories; - -namespace CellularManagement.Infrastructure.Repositories.Base; - -/// -/// 基础仓储接口 -/// 组合命令和查询仓储的功能 -/// -/// 实体类型 -public interface IBaseRepository : ICommandRepository, IQueryRepository where T : class -{ - -} \ No newline at end of file