mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
- 新增 CQRS 核心概念介绍,包括命令、查询、处理器和分发器 - 添加基本用法示例,展示命令和查询的定义与发送流程 - 实现高级功能文档,涵盖请求、通知、管道行为和流式处理 - 提供最佳实践指南,明确命令查询分离和验证行为使用方式 - 增加常见问题解答,解释 Command/Query 区别和错误处理方案 - 新增 CQRS 处理器自动注册实现,支持源码生成和反射扫描 - 添加单元测试验证处理器注册顺序和容错行为 - 更新项目 AI 代理说明文档,完善模块依赖关系图
196 lines
11 KiB
C#
196 lines
11 KiB
C#
using GFramework.SourceGenerators.Cqrs;
|
|
using GFramework.SourceGenerators.Tests.Core;
|
|
|
|
namespace GFramework.SourceGenerators.Tests.Cqrs;
|
|
|
|
/// <summary>
|
|
/// 验证 CQRS 处理器注册生成器的输出与回退边界。
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class CqrsHandlerRegistryGeneratorTests
|
|
{
|
|
/// <summary>
|
|
/// 验证生成器会为当前程序集中的 request、notification 和 stream 处理器生成稳定顺序的注册器。
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Generates_Assembly_Level_Cqrs_Handler_Registry()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection
|
|
{
|
|
public interface IServiceCollection { }
|
|
|
|
public static class ServiceCollectionServiceExtensions
|
|
{
|
|
public static void AddTransient(IServiceCollection services, Type serviceType, Type implementationType) { }
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.Logging
|
|
{
|
|
public interface ILogger
|
|
{
|
|
void Debug(string msg);
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.Cqrs
|
|
{
|
|
public interface IRequest<TResponse> { }
|
|
public interface INotification { }
|
|
public interface IStreamRequest<TResponse> { }
|
|
|
|
public interface IRequestHandler<in TRequest, TResponse> where TRequest : IRequest<TResponse> { }
|
|
public interface INotificationHandler<in TNotification> where TNotification : INotification { }
|
|
public interface IStreamRequestHandler<in TRequest, out TResponse> where TRequest : IStreamRequest<TResponse> { }
|
|
|
|
public interface ICqrsHandlerRegistry
|
|
{
|
|
void Register(Microsoft.Extensions.DependencyInjection.IServiceCollection services, GFramework.Core.Abstractions.Logging.ILogger logger);
|
|
}
|
|
|
|
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
|
|
public sealed class CqrsHandlerRegistryAttribute : Attribute
|
|
{
|
|
public CqrsHandlerRegistryAttribute(Type registryType) { }
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.Core.Abstractions.Cqrs;
|
|
|
|
public sealed record PingQuery() : IRequest<string>;
|
|
public sealed record DomainEvent() : INotification;
|
|
public sealed record NumberStream() : IStreamRequest<int>;
|
|
|
|
public sealed class ZetaNotificationHandler : INotificationHandler<DomainEvent> { }
|
|
public sealed class AlphaQueryHandler : IRequestHandler<PingQuery, string> { }
|
|
public sealed class StreamHandler : IStreamRequestHandler<NumberStream, int> { }
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated />
|
|
#nullable enable
|
|
|
|
[assembly: global::GFramework.Core.Abstractions.Cqrs.CqrsHandlerRegistryAttribute(typeof(global::GFramework.Generated.Cqrs.__GFrameworkGeneratedCqrsHandlerRegistry))]
|
|
|
|
namespace GFramework.Generated.Cqrs;
|
|
|
|
internal sealed class __GFrameworkGeneratedCqrsHandlerRegistry : global::GFramework.Core.Abstractions.Cqrs.ICqrsHandlerRegistry
|
|
{
|
|
public void Register(global::Microsoft.Extensions.DependencyInjection.IServiceCollection services, global::GFramework.Core.Abstractions.Logging.ILogger logger)
|
|
{
|
|
if (services is null)
|
|
throw new global::System.ArgumentNullException(nameof(services));
|
|
if (logger is null)
|
|
throw new global::System.ArgumentNullException(nameof(logger));
|
|
|
|
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient(
|
|
services,
|
|
typeof(global::GFramework.Core.Abstractions.Cqrs.IRequestHandler<global::TestApp.PingQuery, string>),
|
|
typeof(global::TestApp.AlphaQueryHandler));
|
|
logger.Debug("Registered CQRS handler TestApp.AlphaQueryHandler as GFramework.Core.Abstractions.Cqrs.IRequestHandler<TestApp.PingQuery, string>.");
|
|
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient(
|
|
services,
|
|
typeof(global::GFramework.Core.Abstractions.Cqrs.IStreamRequestHandler<global::TestApp.NumberStream, int>),
|
|
typeof(global::TestApp.StreamHandler));
|
|
logger.Debug("Registered CQRS handler TestApp.StreamHandler as GFramework.Core.Abstractions.Cqrs.IStreamRequestHandler<TestApp.NumberStream, int>.");
|
|
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient(
|
|
services,
|
|
typeof(global::GFramework.Core.Abstractions.Cqrs.INotificationHandler<global::TestApp.DomainEvent>),
|
|
typeof(global::TestApp.ZetaNotificationHandler));
|
|
logger.Debug("Registered CQRS handler TestApp.ZetaNotificationHandler as GFramework.Core.Abstractions.Cqrs.INotificationHandler<TestApp.DomainEvent>.");
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<CqrsHandlerRegistryGenerator>.RunAsync(
|
|
source,
|
|
("CqrsHandlerRegistry.g.cs", expected));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证当程序集包含生成代码无法合法引用的私有嵌套处理器时,生成器会放弃产出并让运行时回退到反射扫描。
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Skips_Generation_When_Assembly_Contains_Private_Nested_Handler()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection
|
|
{
|
|
public interface IServiceCollection { }
|
|
|
|
public static class ServiceCollectionServiceExtensions
|
|
{
|
|
public static void AddTransient(IServiceCollection services, Type serviceType, Type implementationType) { }
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.Logging
|
|
{
|
|
public interface ILogger
|
|
{
|
|
void Debug(string msg);
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.Cqrs
|
|
{
|
|
public interface IRequest<TResponse> { }
|
|
public interface INotification { }
|
|
public interface IStreamRequest<TResponse> { }
|
|
|
|
public interface IRequestHandler<in TRequest, TResponse> where TRequest : IRequest<TResponse> { }
|
|
public interface INotificationHandler<in TNotification> where TNotification : INotification { }
|
|
public interface IStreamRequestHandler<in TRequest, out TResponse> where TRequest : IStreamRequest<TResponse> { }
|
|
|
|
public interface ICqrsHandlerRegistry
|
|
{
|
|
void Register(Microsoft.Extensions.DependencyInjection.IServiceCollection services, GFramework.Core.Abstractions.Logging.ILogger logger);
|
|
}
|
|
|
|
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
|
|
public sealed class CqrsHandlerRegistryAttribute : Attribute
|
|
{
|
|
public CqrsHandlerRegistryAttribute(Type registryType) { }
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.Core.Abstractions.Cqrs;
|
|
|
|
public sealed record VisibleRequest() : IRequest<string>;
|
|
|
|
public sealed class Container
|
|
{
|
|
private sealed record HiddenRequest() : IRequest<string>;
|
|
|
|
private sealed class HiddenHandler : IRequestHandler<HiddenRequest, string> { }
|
|
}
|
|
|
|
public sealed class VisibleHandler : IRequestHandler<VisibleRequest, string> { }
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<CqrsHandlerRegistryGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" }
|
|
};
|
|
|
|
await test.RunAsync();
|
|
}
|
|
}
|