using Dpz.Core.MessageQueue.Abstractions;
using Dpz.Core.Public.ViewModel.Request;
using Dpz.Core.Public.ViewModel.Response;
namespace Dpz.Core.Service.RepositoryService;
/// <summary>
/// 消息 Outbox 的 MongoDB 存储服务接口。
/// 继承 <see cref="IMessageOutboxStore"/>,由配置驱动 DI 扫描自动注册为 Scoped 生命周期,
/// 供 <see cref="Dpz.Core.Service.MessageOutboxExtensions.AddMessageOutbox"/> 的
/// Singleton 适配器通过 <c>IServiceScopeFactory</c> 按调用创建 Scope 后解析。
/// </summary>
public interface IMongoMessageOutboxStore : IMessageOutboxStore
{
/// <summary>
/// 获取 Outbox 记录。
/// </summary>
/// <param name="id">记录 ID。</param>
/// <param name="cancellationToken">取消令牌。</param>
Task<MessageOutboxResponse?> GetAsync(string id, CancellationToken cancellationToken = default);
/// <summary>
/// 分页搜索 Outbox 记录。
/// </summary>
/// <param name="request">查询参数。</param>
/// <param name="cancellationToken">取消令牌。</param>
Task<IPagedList<MessageOutboxResponse>> GetPagedListAsync(
MessageOutboxRequest request,
CancellationToken cancellationToken = default
);
/// <summary>
/// 获取 Outbox 导出数据。
/// </summary>
/// <param name="request">查询参数。</param>
/// <param name="take">导出记录数。</param>
/// <param name="cancellationToken">取消令牌。</param>
IAsyncEnumerable<MessageOutboxResponse> GetExportDataAsync(
MessageOutboxRequest request,
int take,
CancellationToken cancellationToken = default
);
/// <summary>
/// 获取 Outbox 搜索筛选项。
/// </summary>
/// <param name="cancellationToken">取消令牌。</param>
Task<MessageOutboxFilterOptionsResponse> GetFilterOptionsAsync(
CancellationToken cancellationToken = default
);
/// <summary>
/// 删除指定时间前已经成功消费的 Outbox 记录。
/// </summary>
/// <param name="cutoffTime">消费完成时间截止点。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>删除的记录数。</returns>
Task<long> DeleteConsumedBeforeAsync(
DateTime cutoffTime,
CancellationToken cancellationToken = default
);
/// <summary>
/// 删除 Outbox 记录。
/// </summary>
/// <param name="id">记录 ID。</param>
/// <param name="cancellationToken">取消令牌。</param>
Task DeleteAsync(string id, CancellationToken cancellationToken = default);
}
⚠⚠ 以下内容为AI分析的结果,请根据实际情况进行判断。
代码解释
这是一个定义 消息发件箱(Message Outbox)MongoDB 存储服务 的接口文件,采用了 Outbox 模式来保证分布式系统中的消息可靠性。
核心要点
1. 接口继承关系
public interface IMongoMessageOutboxStore : IMessageOutboxStore
- 继承自
IMessageOutboxStore(来自消息队列抽象层) - 扩展了 MongoDB 特定的查询和管理功能
2. 依赖注入生命周期设计
文档注释说明了复杂的 DI 架构:
- 接口本身注册为 Scoped 生命周期
- 被 Singleton 适配器包装
- 适配器通过
IServiceScopeFactory按需创建作用域来解析实例
这种设计模式常见于需要数据库连接的后台服务,确保每次操作都有独立的数据库上下文。
接口方法详解
查询操作
GetAsync - 单条记录查询
Task<MessageOutboxResponse?> GetAsync(string id, ...)- 通过 ID 获取指定的 Outbox 记录
- 返回可空类型,记录不存在时返回 null
GetPagedListAsync - 分页查询
Task<IPagedList<MessageOutboxResponse>> GetPagedListAsync(MessageOutboxRequest request, ...)- 支持复杂查询条件
- 返回分页结果(包含总数、页码等分页信息)
GetExportDataAsync - 数据导出
IAsyncEnumerable<MessageOutboxResponse> GetExportDataAsync(..., int take, ...)- 使用
IAsyncEnumerable实现流式处理 - 适用于大数据量导出场景,避免内存溢出
- 使用
GetFilterOptionsAsync - 获取筛选项
Task<MessageOutboxFilterOptionsResponse> GetFilterOptionsAsync(...)- 返回可用的筛选条件(如消息类型、状态等)
- 典型用于前端动态构建查询表单
维护操作
DeleteConsumedBeforeAsync - 清理历史数据
Task<long> DeleteConsumedBeforeAsync(DateTime cutoffTime, ...)- 删除指定时间前已成功消费的记录
- 返回删除数量
- 用于定期清理归档数据
DeleteAsync - 单条删除
Task DeleteAsync(string id, ...)- 手动删除指定记录
设计模式与最佳实践
- Outbox 模式:确保消息发送与业务操作的原子性
- 取消令牌:所有异步方法都支持取消操作
- 响应式设计:使用
IAsyncEnumerable处理大数据流 - 关注点分离:接口专注于 MongoDB 存储,不关心业务逻辑
这个接口是微服务架构中实现 最终一致性 的关键组件,通过持久化消息保证系统在故障后能够恢复和重试。
AI 正在分析代码…
评论加载中...