mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
docs(core): 添加 CQRS 架构模式详细文档
- 完整介绍 CQRS 核心概念包括命令、查询、处理器和分发器 - 提供命令和查询的定义与实现示例代码 - 详细介绍处理器编写方法和注册流程 - 说明管道行为(Behaviors)的使用方式 - 展示通知(Notification)和流式处理功能 - 提供最佳实践和常见问题解决方案 - 包含完整的 API 参考和用法示例
This commit is contained in:
parent
f59e8f7a1f
commit
4db7923512
@ -105,7 +105,8 @@ major 版本中移除。
|
||||
- `PriorityGenerator` (`[Priority]`): 生成优先级比较相关实现。
|
||||
- `EnumExtensionsGenerator` (`[GenerateEnumExtensions]`): 生成枚举扩展能力。
|
||||
- `ContextAwareGenerator` (`[ContextAware]`): 自动实现 `IContextAware` 相关样板逻辑。
|
||||
- `CqrsHandlerRegistryGenerator`: 为消费端程序集生成 CQRS handler 注册器,运行时优先使用生成产物,无法覆盖时回退到反射扫描。
|
||||
- `CqrsHandlerRegistryGenerator`: 为消费端程序集生成 CQRS handler 注册器,运行时优先使用生成产物,无法覆盖时回退到反射扫描;非默认程序集可通过
|
||||
`RegisterCqrsHandlersFromAssembly(...)` / `RegisterCqrsHandlersFromAssemblies(...)` 显式接入同一路径。
|
||||
|
||||
这些生成器的目标是减少重复代码,同时保持框架层 API 的一致性与可维护性。
|
||||
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using GFramework.Core.Abstractions.Lifecycle;
|
||||
using GFramework.Core.Abstractions.Model;
|
||||
using GFramework.Core.Abstractions.Systems;
|
||||
using GFramework.Core.Abstractions.Utility;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace GFramework.Core.Abstractions.Architectures;
|
||||
|
||||
@ -96,6 +96,20 @@ public interface IArchitecture : IAsyncInitializable, IAsyncDestroyable, IInitia
|
||||
void RegisterMediatorBehavior<TBehavior>()
|
||||
where TBehavior : class;
|
||||
|
||||
/// <summary>
|
||||
/// 从指定程序集显式注册 CQRS 处理器。
|
||||
/// 当处理器位于默认架构程序集之外的模块或扩展程序集中时,可在初始化阶段调用该入口接入对应程序集。
|
||||
/// </summary>
|
||||
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
|
||||
void RegisterCqrsHandlersFromAssembly(Assembly assembly);
|
||||
|
||||
/// <summary>
|
||||
/// 从多个程序集显式注册 CQRS 处理器。
|
||||
/// 该入口会对程序集集合去重,适用于统一接入多个扩展包或模块程序集。
|
||||
/// </summary>
|
||||
/// <param name="assemblies">要接入的程序集集合。</param>
|
||||
void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies);
|
||||
|
||||
/// <summary>
|
||||
/// 安装架构模块
|
||||
/// </summary>
|
||||
|
||||
@ -16,4 +16,5 @@ global using System.Collections.Generic;
|
||||
global using System.Runtime;
|
||||
global using System.Linq;
|
||||
global using System.Threading;
|
||||
global using System.Threading.Tasks;
|
||||
global using System.Threading.Tasks;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using GFramework.Core.Abstractions.Rule;
|
||||
using GFramework.Core.Abstractions.Systems;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace GFramework.Core.Abstractions.Ioc;
|
||||
|
||||
@ -109,6 +109,21 @@ public interface IIocContainer : IContextAware
|
||||
void RegisterMediatorBehavior<TBehavior>()
|
||||
where TBehavior : class;
|
||||
|
||||
/// <summary>
|
||||
/// 从指定程序集显式注册 CQRS 处理器。
|
||||
/// 该入口适用于处理器不位于默认架构程序集中的场景,例如扩展包、模块程序集或拆分后的业务程序集。
|
||||
/// 运行时会优先使用程序集级源码生成注册器;若不存在可用注册器,则自动回退到反射扫描。
|
||||
/// </summary>
|
||||
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
|
||||
void RegisterCqrsHandlersFromAssembly(Assembly assembly);
|
||||
|
||||
/// <summary>
|
||||
/// 从多个程序集显式注册 CQRS 处理器。
|
||||
/// 容器会按稳定程序集键去重,避免默认启动路径与扩展模块重复接入同一程序集时产生重复 handler 映射。
|
||||
/// </summary>
|
||||
/// <param name="assemblies">要接入的程序集集合。</param>
|
||||
void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 配置服务
|
||||
|
||||
@ -0,0 +1,165 @@
|
||||
using System.Reflection;
|
||||
using GFramework.Core.Abstractions.Cqrs;
|
||||
using GFramework.Core.Abstractions.Logging;
|
||||
using GFramework.Core.Architectures;
|
||||
using GFramework.Core.Logging;
|
||||
|
||||
namespace GFramework.Core.Tests.Architectures;
|
||||
|
||||
/// <summary>
|
||||
/// 验证架构初始化阶段可以显式接入默认程序集之外的 CQRS handlers。
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public sealed class ArchitectureAdditionalCqrsHandlersTests
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化日志工厂和共享测试状态。
|
||||
/// </summary>
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
LoggerFactoryResolver.Provider = new ConsoleLoggerFactoryProvider();
|
||||
GameContext.Clear();
|
||||
AdditionalAssemblyNotificationHandler.Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理测试过程中写入的共享状态。
|
||||
/// </summary>
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
AdditionalAssemblyNotificationHandler.Reset();
|
||||
GameContext.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证显式声明的额外程序集会在初始化阶段接入当前架构容器。
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task RegisterCqrsHandlersFromAssembly_Should_Register_Handlers_From_Explicit_Assembly()
|
||||
{
|
||||
var generatedAssembly = CreateGeneratedHandlerAssembly();
|
||||
var architecture = new AdditionalHandlersTestArchitecture(target =>
|
||||
target.RegisterCqrsHandlersFromAssembly(generatedAssembly.Object));
|
||||
|
||||
await architecture.InitializeAsync();
|
||||
await architecture.Context.PublishAsync(new AdditionalAssemblyNotification());
|
||||
|
||||
Assert.That(AdditionalAssemblyNotificationHandler.InvocationCount, Is.EqualTo(1));
|
||||
|
||||
await architecture.DestroyAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证同一额外程序集被重复声明时,不会向容器重复写入相同 handler 映射。
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task RegisterCqrsHandlersFromAssembly_Should_Deduplicate_Repeated_Assembly_Registration()
|
||||
{
|
||||
var generatedAssembly = CreateGeneratedHandlerAssembly();
|
||||
var architecture = new AdditionalHandlersTestArchitecture(target =>
|
||||
{
|
||||
target.RegisterCqrsHandlersFromAssembly(generatedAssembly.Object);
|
||||
target.RegisterCqrsHandlersFromAssemblies([generatedAssembly.Object]);
|
||||
});
|
||||
|
||||
await architecture.InitializeAsync();
|
||||
await architecture.Context.PublishAsync(new AdditionalAssemblyNotification());
|
||||
|
||||
Assert.That(AdditionalAssemblyNotificationHandler.InvocationCount, Is.EqualTo(1));
|
||||
|
||||
await architecture.DestroyAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个仅暴露程序集级 CQRS registry 元数据的 mocked Assembly。
|
||||
/// 该测试替身模拟“扩展程序集已经挂接 source-generator,运行时只需显式接入该程序集”的真实路径。
|
||||
/// </summary>
|
||||
/// <returns>包含程序集级 handler registry 元数据的 mocked Assembly。</returns>
|
||||
private static Mock<Assembly> CreateGeneratedHandlerAssembly()
|
||||
{
|
||||
var generatedAssembly = new Mock<Assembly>();
|
||||
generatedAssembly
|
||||
.SetupGet(static assembly => assembly.FullName)
|
||||
.Returns("GFramework.Core.Tests.Architectures.ExplicitAdditionalHandlers, Version=1.0.0.0");
|
||||
generatedAssembly
|
||||
.Setup(static assembly => assembly.GetCustomAttributes(typeof(CqrsHandlerRegistryAttribute), false))
|
||||
.Returns([new CqrsHandlerRegistryAttribute(typeof(AdditionalAssemblyNotificationHandlerRegistry))]);
|
||||
return generatedAssembly;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于测试额外程序集注册入口的最小架构实现。
|
||||
/// </summary>
|
||||
private sealed class AdditionalHandlersTestArchitecture(Action<AdditionalHandlersTestArchitecture> configure) :
|
||||
Architecture
|
||||
{
|
||||
/// <summary>
|
||||
/// 在初始化阶段执行测试注入的额外 CQRS 程序集接入逻辑。
|
||||
/// </summary>
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
configure(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于验证额外程序集接入是否成功的测试通知。
|
||||
/// </summary>
|
||||
public sealed record AdditionalAssemblyNotification : INotification;
|
||||
|
||||
/// <summary>
|
||||
/// 由模拟扩展程序集的生成注册器挂入当前容器的通知处理器。
|
||||
/// </summary>
|
||||
public sealed class AdditionalAssemblyNotificationHandler : INotificationHandler<AdditionalAssemblyNotification>
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前测试进程中该处理器的执行次数。
|
||||
/// </summary>
|
||||
public static int InvocationCount { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 记录一次通知处理,供测试断言显式程序集接入后的运行时行为。
|
||||
/// </summary>
|
||||
/// <param name="notification">通知实例。</param>
|
||||
/// <param name="cancellationToken">取消令牌。</param>
|
||||
/// <returns>已完成任务。</returns>
|
||||
public ValueTask Handle(AdditionalAssemblyNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
InvocationCount++;
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理共享计数器,避免测试间相互污染。
|
||||
/// </summary>
|
||||
public static void Reset()
|
||||
{
|
||||
InvocationCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模拟由 source-generator 为扩展程序集生成的 CQRS handler registry。
|
||||
/// </summary>
|
||||
internal sealed class AdditionalAssemblyNotificationHandlerRegistry : ICqrsHandlerRegistry
|
||||
{
|
||||
/// <summary>
|
||||
/// 将扩展程序集中的通知处理器映射写入服务集合。
|
||||
/// </summary>
|
||||
/// <param name="services">目标服务集合。</param>
|
||||
/// <param name="logger">日志记录器。</param>
|
||||
public void Register(IServiceCollection services, ILogger logger)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(services);
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
services
|
||||
.AddTransient<INotificationHandler<AdditionalAssemblyNotification>,
|
||||
AdditionalAssemblyNotificationHandler>();
|
||||
logger.Debug(
|
||||
$"Registered CQRS handler {typeof(AdditionalAssemblyNotificationHandler).FullName} as {typeof(INotificationHandler<AdditionalAssemblyNotification>).FullName}.");
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
using System.Reflection;
|
||||
using GFramework.Core.Abstractions.Architectures;
|
||||
using GFramework.Core.Abstractions.Enums;
|
||||
using GFramework.Core.Abstractions.Lifecycle;
|
||||
@ -185,6 +186,16 @@ public class TestArchitectureWithRegistry : IArchitecture
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[Obsolete("Use RegisterCqrsPipelineBehavior<TBehavior>() instead.")]
|
||||
public void RegisterMediatorBehavior<TBehavior>() where TBehavior : class
|
||||
{
|
||||
@ -316,6 +327,16 @@ public class TestArchitectureWithoutRegistry : IArchitecture
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[Obsolete("Use RegisterCqrsPipelineBehavior<TBehavior>() instead.")]
|
||||
public void RegisterMediatorBehavior<TBehavior>() where TBehavior : class
|
||||
{
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using GFramework.Core.Abstractions.Architectures;
|
||||
using GFramework.Core.Abstractions.Enums;
|
||||
using GFramework.Core.Abstractions.Environment;
|
||||
@ -169,6 +170,26 @@ public abstract class Architecture : IArchitecture
|
||||
RegisterCqrsPipelineBehavior<TBehavior>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定程序集显式注册 CQRS 处理器。
|
||||
/// 该入口适用于把拆分到其他模块或扩展包程序集中的 handlers 接入当前架构。
|
||||
/// </summary>
|
||||
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
|
||||
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
|
||||
{
|
||||
_modules.RegisterCqrsHandlersFromAssembly(assembly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从多个程序集显式注册 CQRS 处理器。
|
||||
/// 适用于在初始化阶段批量接入多个扩展程序集,并沿用容器的去重策略避免重复注册。
|
||||
/// </summary>
|
||||
/// <param name="assemblies">要接入的程序集集合。</param>
|
||||
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
|
||||
{
|
||||
_modules.RegisterCqrsHandlersFromAssemblies(assemblies);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 安装架构模块
|
||||
/// </summary>
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
using GFramework.Core.Abstractions.Architectures;
|
||||
using GFramework.Core.Abstractions.Environment;
|
||||
using GFramework.Core.Abstractions.Logging;
|
||||
using GFramework.Core.Cqrs.Internal;
|
||||
|
||||
namespace GFramework.Core.Architectures;
|
||||
|
||||
@ -99,10 +98,11 @@ internal sealed class ArchitectureBootstrapper(
|
||||
private void ConfigureServices(IArchitectureContext context, Action<IServiceCollection>? configurator)
|
||||
{
|
||||
services.SetContext(context);
|
||||
CqrsHandlerRegistrar.RegisterHandlers(
|
||||
services.Container,
|
||||
[architectureType.Assembly, typeof(ArchitectureContext).Assembly],
|
||||
logger);
|
||||
services.Container.RegisterCqrsHandlersFromAssemblies(
|
||||
[
|
||||
architectureType.Assembly,
|
||||
typeof(ArchitectureContext).Assembly
|
||||
]);
|
||||
|
||||
if (configurator is null)
|
||||
logger.Debug("No external service configurator provided. Using built-in CQRS runtime registration only.");
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using GFramework.Core.Abstractions.Architectures;
|
||||
using GFramework.Core.Abstractions.Logging;
|
||||
|
||||
@ -38,6 +39,28 @@ internal sealed class ArchitectureModules(
|
||||
RegisterCqrsPipelineBehavior<TBehavior>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定程序集显式注册 CQRS 处理器。
|
||||
/// 该入口用于把默认架构程序集之外的扩展处理器接入当前架构容器。
|
||||
/// </summary>
|
||||
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
|
||||
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
|
||||
{
|
||||
logger.Debug($"Registering CQRS handlers from assembly: {assembly.FullName ?? assembly.GetName().Name}");
|
||||
services.Container.RegisterCqrsHandlersFromAssembly(assembly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从多个程序集显式注册 CQRS 处理器。
|
||||
/// 它会复用容器级去重逻辑,避免模块重复接入相同程序集时重复注册 handler。
|
||||
/// </summary>
|
||||
/// <param name="assemblies">要接入的程序集集合。</param>
|
||||
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
|
||||
{
|
||||
logger.Debug("Registering CQRS handlers from additional assemblies.");
|
||||
services.Container.RegisterCqrsHandlersFromAssemblies(assemblies);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 安装架构模块
|
||||
/// </summary>
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using GFramework.Core.Abstractions.Bases;
|
||||
using GFramework.Core.Abstractions.Cqrs;
|
||||
using GFramework.Core.Abstractions.Ioc;
|
||||
using GFramework.Core.Abstractions.Logging;
|
||||
using GFramework.Core.Abstractions.Systems;
|
||||
using GFramework.Core.Cqrs.Internal;
|
||||
using GFramework.Core.Logging;
|
||||
using GFramework.Core.Rule;
|
||||
|
||||
@ -56,6 +58,12 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
||||
/// </summary>
|
||||
private readonly HashSet<object> _registeredInstances = [];
|
||||
|
||||
/// <summary>
|
||||
/// 已接入 CQRS handler 注册流程的程序集键集合。
|
||||
/// 使用稳定字符串键而不是 Assembly 引用本身,以避免默认路径和显式扩展路径使用不同 Assembly 对象时重复注册。
|
||||
/// </summary>
|
||||
private readonly HashSet<string> _registeredCqrsHandlerAssemblyKeys = new(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// 日志记录器,用于记录容器操作日志
|
||||
/// </summary>
|
||||
@ -372,6 +380,56 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
||||
RegisterCqrsPipelineBehavior<TBehavior>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定程序集显式注册 CQRS 处理器。
|
||||
/// </summary>
|
||||
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
|
||||
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(assembly);
|
||||
RegisterCqrsHandlersFromAssemblies([assembly]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从多个程序集显式注册 CQRS 处理器。
|
||||
/// 同一程序集只会被接入一次,避免默认启动路径与扩展模块重复注册相同 handlers。
|
||||
/// </summary>
|
||||
/// <param name="assemblies">要接入的程序集集合。</param>
|
||||
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(assemblies);
|
||||
|
||||
_lock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
ThrowIfFrozen();
|
||||
|
||||
var processedAssemblyKeys = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var assembly in assemblies
|
||||
.Where(static assembly => assembly is not null)
|
||||
.OrderBy(GetCqrsAssemblyRegistrationKey, StringComparer.Ordinal))
|
||||
{
|
||||
var assemblyKey = GetCqrsAssemblyRegistrationKey(assembly);
|
||||
if (!processedAssemblyKeys.Add(assemblyKey))
|
||||
continue;
|
||||
|
||||
if (_registeredCqrsHandlerAssemblyKeys.Contains(assemblyKey))
|
||||
{
|
||||
_logger.Debug(
|
||||
$"Skipping CQRS handler registration for assembly {assemblyKey} because it was already registered.");
|
||||
continue;
|
||||
}
|
||||
|
||||
CqrsHandlerRegistrar.RegisterHandlers(this, [assembly], _logger);
|
||||
_registeredCqrsHandlerAssemblyKeys.Add(assemblyKey);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置服务
|
||||
/// </summary>
|
||||
@ -816,5 +874,16 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成 CQRS handler 注册用的稳定程序集键。
|
||||
/// 该键需要同时兼顾真实程序集与测试中使用的 mocked Assembly,避免仅靠引用比较导致重复接入。
|
||||
/// </summary>
|
||||
/// <param name="assembly">目标程序集。</param>
|
||||
/// <returns>稳定的程序集标识字符串。</returns>
|
||||
private static string GetCqrsAssemblyRegistrationKey(Assembly assembly)
|
||||
{
|
||||
return assembly.FullName ?? assembly.GetName().Name ?? assembly.ToString();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@ -224,7 +224,27 @@ public class GameArchitecture : Architecture
|
||||
如果该程序集没有生成注册器,或者包含生成代码无法合法引用的处理器类型,则会自动回退到运行时反射扫描。
|
||||
`GFramework.Core` 等未挂接该生成器的程序集仍会继续走反射扫描。
|
||||
|
||||
如果处理器位于其他模块或扩展程序集中,需要额外接入对应程序集的处理器注册,而不是只依赖默认接入范围。
|
||||
如果处理器位于其他模块或扩展程序集中,需要额外接入对应程序集的处理器注册,而不是只依赖默认接入范围:
|
||||
|
||||
```csharp
|
||||
public class GameArchitecture : Architecture
|
||||
{
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
RegisterCqrsPipelineBehavior<LoggingBehavior<,>>();
|
||||
|
||||
RegisterCqrsHandlersFromAssemblies(
|
||||
[
|
||||
typeof(InventoryCqrsMarker).Assembly,
|
||||
typeof(BattleCqrsMarker).Assembly
|
||||
]);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`RegisterCqrsHandlersFromAssembly(...)` / `RegisterCqrsHandlersFromAssemblies(...)` 会复用与默认启动路径相同的注册逻辑:
|
||||
优先使用程序集级生成注册器,失败时自动回退到反射扫描;如果同一程序集已经由默认路径或其他模块接入,框架会自动去重,避免重复注册
|
||||
handler。
|
||||
|
||||
`RegisterCqrsPipelineBehavior<TBehavior>()` 是推荐入口;旧的 `RegisterMediatorBehavior<TBehavior>()`
|
||||
仅作为兼容名称保留,当前已标记为 `Obsolete` 并从 IntelliSense 主路径隐藏,计划在未来 major 版本中移除。
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user