Skip to content

Refactor/Add CQRS dispatcher caching and refactor handler registration#228

Merged
GeWuYou merged 4 commits into
refactor/cqrs-architecture-decouplingfrom
refactor/cqrs-architecture-decoupling-todo-8
Apr 16, 2026
Merged

Refactor/Add CQRS dispatcher caching and refactor handler registration#228
GeWuYou merged 4 commits into
refactor/cqrs-architecture-decouplingfrom
refactor/cqrs-architecture-decoupling-todo-8

Conversation

@GeWuYou

@GeWuYou GeWuYou commented Apr 16, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

发布说明

  • 新功能

    • 增强了处理程序注册的反射回退机制,提供更灵活的配置选项
    • CQRS调度器添加了服务类型缓存以提升性能
  • 测试

    • 新增缓存机制的验证测试

GeWuYou added 4 commits April 16, 2026 11:11
- 实现CqrsHandlerRegistrar类,支持扫描并注册CQRS请求/通知/流式处理器
- 添加源码生成注册器优先策略,减少冷启动时的反射开销
- 实现运行时反射扫描回退机制,确保处理器注册的完整性
- 添加CqrsReflectionFallbackAttribute特性,标记需要运行时补充扫描的程序集
- 创建完整的单元测试套件,验证处理器注册顺序与容错行为
- 实现CqrsHandlerRegistryGenerator源码生成器,自动生成处理器注册代码
- 添加详细的日志记录与诊断功能,便于调试注册过程
- 实现类型安全的处理器映射验证与重复注册检测机制
- 实现GFramework自有CQRS运行时分发器,支持请求/通知/流式请求处理
- 添加进程级缓存机制优化反射调用性能,包括请求、通知、流水线调用委托缓存
- 重构CqrsHandlerRegistrar使用ReflectionFallbackMetadata替代字符串类型名
- 引入CqrsReflectionFallbackAttribute支持运行时补充反射扫描的处理器类型
- 添加完整的CQRS处理器注册单元测试,验证有序执行和容错行为
- 修复MicrosoftDiContainer中异常消息的格式化空白问题
- 实现上下文感知处理器的CQRS分发上下文注入功能
- 在 CqrsDispatcher 中添加 Concurrent 包引用以支持线程安全操作
- 在全局引用文件中增加 Concurrent 包引用,统一并发编程支持
- 为后续的并发处理逻辑改进奠定基础架构支持
- 实现 CqrsDispatcher 类,支持请求/通知/流式请求的分发处理
- 添加进程级缓存机制,优化热路径中的反射和类型构造性能
- 实现上下文感知处理器的 CQRS 分发上下文注入功能
- 开发 CqrsHandlerRegistryGenerator 源代码生成器,减少运行时反射扫描
- 添加完整的单元测试验证缓存机制和服务类型注册功能
- 支持管道行为链处理和异步流式请求响应模式
@coderabbitai

coderabbitai Bot commented Apr 16, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

总体概览

此PR为GFramework CQRS框架引入了多项性能优化和功能增强:添加了进程级别的缓存机制以避免重复的泛型类型构造,增强了CqrsReflectionFallbackAttribute支持多种构造器和属性,改进了源代码生成器以支持反射式处理程序注册,并添加了验证缓存行为的新测试。

变更清单

Cohort / File(s) 摘要
CQRS运行时缓存优化
GFramework.Cqrs/Internal/CqrsDispatcher.cs
引入三个进程级别的缓存(通知处理程序服务类型、请求服务类型、流处理程序服务类型)以减少泛型类型构造开销;添加RequestServiceTypeSet记录结构;缓存MethodInfo字段以优化反射调用。
反射回退属性增强
GFramework.Cqrs/CqrsReflectionFallbackAttribute.cs
添加三个新的公开构造器(无参、params string[]params Type[])和两个公开只读属性(FallbackHandlerTypeNamesFallbackHandlerTypes),支持显式指定回退处理程序。
处理程序注册逻辑重构
GFramework.Cqrs/Internal/CqrsHandlerRegistrar.cs
重构RegisterHandlers分支逻辑;引入GetReflectionFallbackMetadata方法;添加GeneratedRegistrationResultReflectionFallbackMetadata记录结构;支持基于元数据的候选处理程序类型选择。
源代码生成器更新
GFramework.SourceGenerators/Cqrs/CqrsHandlerRegistryGenerator.cs
移除CqrsReflectionFallbackAttribute标记生成;重构处理程序收集为ImplementationRegistrationSpec;添加RegisterReflectedHandler辅助方法;支持反射式处理程序注册而非直接AddTransient调用。
全局导入
GFramework.Cqrs/GlobalUsings.cs
添加System.Collections.Concurrent全局using声明。
XML文档调整
GFramework.Core/Ioc/MicrosoftDiContainer.cs
调整RegisterCqrsHandlersFromAssemblies方法的XML文档异常说明中的缩进格式。
CQRS缓存验证测试
GFramework.Cqrs.Tests/Cqrs/CqrsDispatcherCacheTests.cs
新增完整的测试套件,通过反射访问非公开的静态缓存字段验证分发器缓存行为,包含多个测试消息类型和对应的处理程序。
处理程序注册器测试更新
GFramework.Cqrs.Tests/Cqrs/CqrsHandlerRegistrarTests.cs
重写现有测试以验证反射回退的精确驱动机制;添加新测试验证直接回退元数据路径。
源生成器测试更新
GFramework.SourceGenerators.Tests/Cqrs/CqrsHandlerRegistryGeneratorTests.cs
更新测试预期结果从程序集级别反射回退标记改为生成的注册表内的有目标反射;调整测试用例和内联属性构造器签名。

预计代码审查工作量

🎯 4 (复杂) | ⏱️ ~60 分钟

可能相关的PR

  • PR #222:同时修改了CQRS处理程序注册表/生成和运行时注册路径,代码级别的源生成注册表与注册器回退行为逻辑重叠。
  • PR #220:同样修改了CQRS运行时和处理程序注册代码,涉及CqrsDispatcherCqrsHandlerRegistrarCqrsReflectionFallbackAttribute等相同文件。
  • PR #225:两个PR都修改了CQRS运行时表面和内部实现(包括分发器、注册器、反射回退属性),代码路径直接相关。
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 64.81% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately summarizes the main changes: CQRS dispatcher caching and handler registration refactoring are the primary objectives across multiple files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/cqrs-architecture-decoupling-todo-8

Comment @coderabbitai help to get the list of available commands and usage tips.

@GeWuYou GeWuYou changed the title Refactor/cqrs architecture decoupling todo 8 Refactor/Add CQRS dispatcher caching and refactor handler registration Apr 16, 2026
@GeWuYou GeWuYou merged commit ba6a358 into refactor/cqrs-architecture-decoupling Apr 16, 2026
7 checks passed
@GeWuYou GeWuYou deleted the refactor/cqrs-architecture-decoupling-todo-8 branch April 16, 2026 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant