mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
feat(architecture): 添加Mediator模式支持并扩展架构上下文功能
- 在ArchitectureContext中新增Sender属性和SendCommand/SendQuery方法 - 实现异步命令和查询的发送功能,支持取消令牌 - 提供同步版本的命令和查询发送方法以保持向后兼容性 - 更新IArchitectureContext接口定义新增相关方法签名 - 添加完整的Mediator集成测试验证新功能 - 配置项目依赖移除旧的Mediator包并添加必要引用 - 修复测试中的类型引用和方法重载问题
This commit is contained in:
parent
a082b770a9
commit
d3e1e04e89
@ -127,6 +127,44 @@ public interface IArchitectureContext
|
||||
/// </summary>
|
||||
TResponse SendRequest<TResponse>(IRequest<TResponse> request);
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 异步发送命令并返回结果
|
||||
/// 通过Mediator模式发送命令请求,支持取消操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
||||
/// <param name="command">要发送的命令对象</param>
|
||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||
/// <returns>包含命令执行结果的ValueTask</returns>
|
||||
ValueTask<TResponse> SendCommandAsync<TResponse>(Mediator.ICommand<TResponse> command,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性)
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
||||
/// <param name="command">要发送的命令对象</param>
|
||||
/// <returns>命令执行结果</returns>
|
||||
TResponse SendCommand<TResponse>(Mediator.ICommand<TResponse> command);
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 异步发送查询并返回结果
|
||||
/// 通过Mediator模式发送查询请求,支持取消操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||
/// <param name="command">要发送的查询对象</param>
|
||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||
/// <returns>包含查询结果的ValueTask</returns>
|
||||
ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性)
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||
/// <param name="command">要发送的查询对象</param>
|
||||
/// <returns>查询结果</returns>
|
||||
TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command);
|
||||
|
||||
/// <summary>
|
||||
/// 发布通知(一对多事件)
|
||||
/// </summary>
|
||||
|
||||
@ -6,6 +6,11 @@
|
||||
<TargetFrameworks>net10.0;net8.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Mediator.Abstractions" Version="3.0.1"/>
|
||||
<PackageReference Include="Mediator.SourceGenerator" Version="3.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1"/>
|
||||
<PackageReference Include="Moq" Version="4.20.72"/>
|
||||
<PackageReference Include="NUnit" Version="4.4.0"/>
|
||||
|
||||
@ -128,7 +128,8 @@ public class ArchitectureContextTests
|
||||
[Test]
|
||||
public void SendQuery_Should_ThrowArgumentNullException_When_Query_IsNull()
|
||||
{
|
||||
Assert.That(() => _context!.SendQuery<int>(null!),
|
||||
// 明确指定调用旧的 IQuery<int> 重载
|
||||
Assert.That(() => _context!.SendQuery((Mediator.IQuery<int>)null!),
|
||||
Throws.ArgumentNullException.With.Property("ParamName").EqualTo("query"));
|
||||
}
|
||||
|
||||
|
||||
@ -13,7 +13,9 @@ using GFramework.Core.environment;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.ioc;
|
||||
using GFramework.Core.query;
|
||||
using Mediator;
|
||||
using NUnit.Framework;
|
||||
using ICommand = GFramework.Core.Abstractions.command.ICommand;
|
||||
|
||||
namespace GFramework.Core.Tests.architecture;
|
||||
|
||||
@ -264,11 +266,80 @@ public class TestArchitectureContextV3 : IArchitectureContext
|
||||
{
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> SendRequestAsync<TResponse>(IRequest<TResponse> request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResponse SendRequest<TResponse>(IRequest<TResponse> request)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> SendCommandAsync<TResponse>(Mediator.ICommand<TResponse> command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResponse SendCommand<TResponse>(Mediator.ICommand<TResponse> command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask PublishAsync<TNotification>(TNotification notification,
|
||||
CancellationToken cancellationToken = default) where TNotification : INotification
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TResponse> request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask SendAsync<TCommand>(TCommand command, CancellationToken cancellationToken = default)
|
||||
where TCommand : IRequest<Unit>
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> SendAsync<TResponse>(IRequest<TResponse> command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> QueryAsync<TResponse>(IRequest<TResponse> query,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask PublishEventAsync<TNotification>(TNotification notification,
|
||||
CancellationToken cancellationToken = default) where TNotification : INotification
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SendCommand(ICommand command)
|
||||
{
|
||||
}
|
||||
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
||||
public TResult SendCommand<TResult>(Abstractions.command.ICommand<TResult> command)
|
||||
{
|
||||
return default!;
|
||||
}
|
||||
@ -283,7 +354,7 @@ public class TestArchitectureContextV3 : IArchitectureContext
|
||||
return (Task<TResult>)Task.CompletedTask;
|
||||
}
|
||||
|
||||
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
||||
public TResult SendQuery<TResult>(Abstractions.query.IQuery<TResult> query)
|
||||
{
|
||||
return default!;
|
||||
}
|
||||
|
||||
@ -13,7 +13,9 @@ using GFramework.Core.environment;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.ioc;
|
||||
using GFramework.Core.query;
|
||||
using Mediator;
|
||||
using NUnit.Framework;
|
||||
using ICommand = GFramework.Core.Abstractions.command.ICommand;
|
||||
|
||||
namespace GFramework.Core.Tests.architecture;
|
||||
|
||||
@ -333,6 +335,75 @@ public class TestArchitectureContext : IArchitectureContext
|
||||
{
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> SendRequestAsync<TResponse>(IRequest<TResponse> request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResponse SendRequest<TResponse>(IRequest<TResponse> request)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> SendCommandAsync<TResponse>(Mediator.ICommand<TResponse> command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResponse SendCommand<TResponse>(Mediator.ICommand<TResponse> command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask PublishAsync<TNotification>(TNotification notification,
|
||||
CancellationToken cancellationToken = default) where TNotification : INotification
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TResponse> request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask SendAsync<TCommand>(TCommand command, CancellationToken cancellationToken = default)
|
||||
where TCommand : IRequest<Unit>
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> SendAsync<TResponse>(IRequest<TResponse> command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> QueryAsync<TResponse>(IRequest<TResponse> query,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ValueTask PublishEventAsync<TNotification>(TNotification notification,
|
||||
CancellationToken cancellationToken = default) where TNotification : INotification
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送命令
|
||||
/// </summary>
|
||||
@ -347,7 +418,7 @@ public class TestArchitectureContext : IArchitectureContext
|
||||
/// <typeparam name="TResult">返回值类型</typeparam>
|
||||
/// <param name="command">命令对象</param>
|
||||
/// <returns>命令执行结果</returns>
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
||||
public TResult SendCommand<TResult>(Abstractions.command.ICommand<TResult> command)
|
||||
{
|
||||
return default!;
|
||||
}
|
||||
@ -368,7 +439,7 @@ public class TestArchitectureContext : IArchitectureContext
|
||||
/// <typeparam name="TResult">查询结果类型</typeparam>
|
||||
/// <param name="query">查询对象</param>
|
||||
/// <returns>查询结果</returns>
|
||||
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
||||
public TResult SendQuery<TResult>(Abstractions.query.IQuery<TResult> query)
|
||||
{
|
||||
return default!;
|
||||
}
|
||||
|
||||
443
GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs
Normal file
443
GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs
Normal file
@ -0,0 +1,443 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using GFramework.Core.Abstractions.architecture;
|
||||
using GFramework.Core.Abstractions.enums;
|
||||
using GFramework.Core.Abstractions.events;
|
||||
using GFramework.Core.Abstractions.model;
|
||||
using GFramework.Core.Abstractions.system;
|
||||
using GFramework.Core.Abstractions.utility;
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.command;
|
||||
using GFramework.Core.environment;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.ioc;
|
||||
using GFramework.Core.logging;
|
||||
using GFramework.Core.query;
|
||||
using Mediator;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NUnit.Framework;
|
||||
// ✅ Mediator 库的命名空间
|
||||
|
||||
// ✅ 使用 global using 或别名来区分
|
||||
|
||||
namespace GFramework.Core.Tests.mediator;
|
||||
|
||||
[TestFixture]
|
||||
public class MediatorComprehensiveTests
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
LoggerFactoryResolver.Provider = new ConsoleLoggerFactoryProvider();
|
||||
_container = new MicrosoftDiContainer();
|
||||
|
||||
var loggerField = typeof(MicrosoftDiContainer).GetField("_logger",
|
||||
BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
loggerField?.SetValue(_container,
|
||||
LoggerFactoryResolver.Provider.CreateLogger(nameof(MediatorComprehensiveTests)));
|
||||
|
||||
// 注册基础服务(Legacy CQRS)
|
||||
_eventBus = new EventBus();
|
||||
_commandBus = new CommandExecutor();
|
||||
_queryBus = new QueryExecutor();
|
||||
_asyncQueryBus = new AsyncQueryExecutor();
|
||||
_environment = new DefaultEnvironment();
|
||||
|
||||
_container.RegisterPlurality(_eventBus);
|
||||
_container.RegisterPlurality(_commandBus);
|
||||
_container.RegisterPlurality(_queryBus);
|
||||
_container.RegisterPlurality(_asyncQueryBus);
|
||||
_container.RegisterPlurality(_environment);
|
||||
|
||||
// ✅ 注册 Mediator
|
||||
_container.RegisterMediator(options => { options.ServiceLifetime = ServiceLifetime.Singleton; });
|
||||
|
||||
// ✅ 手动注册 Mediator Handlers
|
||||
_container.Services.AddSingleton<IRequestHandler<TestRequest, int>, TestRequestHandler>();
|
||||
_container.Services.AddSingleton<IRequestHandler<TestCommand, Unit>, TestCommandHandler>();
|
||||
_container.Services.AddSingleton<IRequestHandler<TestCommandWithResult, int>, TestCommandWithResultHandler>();
|
||||
_container.Services.AddSingleton<IRequestHandler<TestQuery, string>, TestQueryHandler>();
|
||||
_container.Services.AddSingleton<INotificationHandler<TestNotification>, TestNotificationHandler>();
|
||||
_container.Services.AddSingleton<IStreamRequestHandler<TestStreamRequest, int>, TestStreamRequestHandler>();
|
||||
|
||||
// 注册测试组件(Legacy)
|
||||
_testSystem = new TestSystem();
|
||||
_testModel = new TestModel();
|
||||
_testUtility = new TestUtility();
|
||||
_testCommand = new TestTraditionalCommand();
|
||||
_testQuery = new TestTraditionalQuery { Result = 999 };
|
||||
|
||||
_container.RegisterPlurality(_testSystem);
|
||||
_container.RegisterPlurality(_testModel);
|
||||
_container.RegisterPlurality(_testUtility);
|
||||
_container.RegisterPlurality(_testCommand);
|
||||
_container.RegisterPlurality(_testQuery);
|
||||
|
||||
// ✅ Freeze 容器
|
||||
_container.Freeze();
|
||||
|
||||
_context = new ArchitectureContext(_container);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_context = null;
|
||||
_container = null;
|
||||
_eventBus = null;
|
||||
_commandBus = null;
|
||||
_queryBus = null;
|
||||
_asyncQueryBus = null;
|
||||
_environment = null;
|
||||
_testSystem = null;
|
||||
_testModel = null;
|
||||
_testUtility = null;
|
||||
_testCommand = null;
|
||||
_testQuery = null;
|
||||
}
|
||||
|
||||
private ArchitectureContext? _context;
|
||||
private MicrosoftDiContainer? _container;
|
||||
private EventBus? _eventBus;
|
||||
private CommandExecutor? _commandBus;
|
||||
private QueryExecutor? _queryBus;
|
||||
private AsyncQueryExecutor? _asyncQueryBus;
|
||||
private DefaultEnvironment? _environment;
|
||||
private TestSystem? _testSystem;
|
||||
private TestModel? _testModel;
|
||||
private TestUtility? _testUtility;
|
||||
private TestTraditionalCommand? _testCommand;
|
||||
private TestTraditionalQuery? _testQuery;
|
||||
|
||||
[Test]
|
||||
public async Task SendRequestAsync_Should_ReturnResult_When_Request_IsValid()
|
||||
{
|
||||
var testRequest = new TestRequest { Value = 42 };
|
||||
var result = await _context!.SendRequestAsync(testRequest);
|
||||
|
||||
Assert.That(result, Is.EqualTo(42));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SendRequestAsync_Should_ThrowArgumentNullException_When_Request_IsNull()
|
||||
{
|
||||
Assert.ThrowsAsync<ArgumentNullException>(async () =>
|
||||
await _context!.SendRequestAsync<int>(null!));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SendRequest_Should_ReturnResult_When_Request_IsValid()
|
||||
{
|
||||
var testRequest = new TestRequest { Value = 123 };
|
||||
var result = _context!.SendRequest(testRequest);
|
||||
|
||||
Assert.That(result, Is.EqualTo(123));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PublishAsync_Should_PublishNotification_When_Notification_IsValid()
|
||||
{
|
||||
TestNotificationHandler.LastReceivedMessage = null;
|
||||
var notification = new TestNotification { Message = "test" };
|
||||
|
||||
await _context!.PublishAsync(notification);
|
||||
await Task.Delay(100);
|
||||
|
||||
Assert.That(TestNotificationHandler.LastReceivedMessage, Is.EqualTo("test"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CreateStream_Should_ReturnStream_When_StreamRequest_IsValid()
|
||||
{
|
||||
var testStreamRequest = new TestStreamRequest { Values = [1, 2, 3, 4, 5] };
|
||||
var stream = _context!.CreateStream(testStreamRequest);
|
||||
|
||||
var results = new List<int>();
|
||||
await foreach (var item in stream)
|
||||
{
|
||||
results.Add(item);
|
||||
}
|
||||
|
||||
Assert.That(results, Is.EqualTo(new[] { 1, 2, 3, 4, 5 }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SendAsync_CommandWithoutResult_Should_Execute_When_Command_IsValid()
|
||||
{
|
||||
var testCommand = new TestCommand { ShouldExecute = true };
|
||||
await _context!.SendAsync(testCommand);
|
||||
|
||||
Assert.That(testCommand.Executed, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SendAsync_CommandWithResult_Should_ReturnResult_When_Command_IsValid()
|
||||
{
|
||||
var testCommand = new TestCommandWithResult { ResultValue = 42 };
|
||||
var result = await _context!.SendAsync(testCommand);
|
||||
|
||||
Assert.That(result, Is.EqualTo(42));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task QueryAsync_Should_ReturnResult_When_Query_IsValid()
|
||||
{
|
||||
var testQuery = new TestQuery { QueryResult = "test result" };
|
||||
var result = await _context!.QueryAsync(testQuery);
|
||||
|
||||
Assert.That(result, Is.EqualTo("test result"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PublishEventAsync_Should_PublishNotification_When_Notification_IsValid()
|
||||
{
|
||||
TestNotificationHandler.LastReceivedMessage = null;
|
||||
var testNotification = new TestNotification { Message = "test event" };
|
||||
|
||||
await _context!.PublishEventAsync(testNotification);
|
||||
await Task.Delay(100);
|
||||
|
||||
Assert.That(TestNotificationHandler.LastReceivedMessage, Is.EqualTo("test event"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Mediator_And_CommandExecutor_Should_Coexist()
|
||||
{
|
||||
// 使用传统方式(Legacy)
|
||||
_context!.SendCommand(_testCommand!);
|
||||
Assert.That(_testCommand!.Executed, Is.True);
|
||||
|
||||
// 使用 Mediator 方式
|
||||
var mediatorCommand = new TestCommandWithResult { ResultValue = 123 };
|
||||
var result = await _context.SendAsync(mediatorCommand);
|
||||
Assert.That(result, Is.EqualTo(123));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Mediator_And_QueryExecutor_Should_Coexist()
|
||||
{
|
||||
// 使用传统方式(Legacy)
|
||||
var traditionalResult = _context!.SendQuery(_testQuery!);
|
||||
Assert.That(traditionalResult, Is.EqualTo(999));
|
||||
|
||||
// 使用 Mediator 方式
|
||||
var mediatorQuery = new TestQuery { QueryResult = "mediator result" };
|
||||
var mediatorResult = await _context.QueryAsync(mediatorQuery);
|
||||
Assert.That(mediatorResult, Is.EqualTo("mediator result"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetService_Should_Use_Cache()
|
||||
{
|
||||
var firstResult = _context!.GetService<IEventBus>();
|
||||
Assert.That(firstResult, Is.Not.Null);
|
||||
Assert.That(firstResult, Is.SameAs(_eventBus));
|
||||
|
||||
var secondResult = _context.GetService<IEventBus>();
|
||||
Assert.That(secondResult, Is.SameAs(firstResult));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Architecture_Component_Getters_Should_Work()
|
||||
{
|
||||
var system = _context!.GetSystem<TestSystem>();
|
||||
var model = _context.GetModel<TestModel>();
|
||||
var utility = _context.GetUtility<TestUtility>();
|
||||
var environment = _context.GetEnvironment();
|
||||
|
||||
Assert.That(system, Is.SameAs(_testSystem));
|
||||
Assert.That(model, Is.SameAs(_testModel));
|
||||
Assert.That(utility, Is.SameAs(_testUtility));
|
||||
Assert.That(environment, Is.SameAs(_environment));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Unregistered_Mediator_Should_Throw_InvalidOperationException()
|
||||
{
|
||||
var containerWithoutMediator = new MicrosoftDiContainer();
|
||||
containerWithoutMediator.Freeze();
|
||||
|
||||
var contextWithoutMediator = new ArchitectureContext(containerWithoutMediator);
|
||||
var testRequest = new TestRequest { Value = 42 };
|
||||
|
||||
Assert.ThrowsAsync<InvalidOperationException>(async () =>
|
||||
await contextWithoutMediator.SendRequestAsync(testRequest));
|
||||
}
|
||||
}
|
||||
|
||||
#region Test Classes - Mediator (新实现)
|
||||
|
||||
// ✅ 这些类使用 Mediator.IRequest
|
||||
public sealed record TestRequest : IRequest<int>
|
||||
{
|
||||
public int Value { get; init; }
|
||||
}
|
||||
|
||||
public sealed record TestCommand : IRequest<Unit>
|
||||
{
|
||||
public bool ShouldExecute { get; init; }
|
||||
public bool Executed { get; set; }
|
||||
}
|
||||
|
||||
public sealed record TestCommandWithResult : IRequest<int>
|
||||
{
|
||||
public int ResultValue { get; init; }
|
||||
}
|
||||
|
||||
public sealed record TestQuery : IRequest<string>
|
||||
{
|
||||
public string QueryResult { get; init; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed record TestNotification : INotification
|
||||
{
|
||||
public string Message { get; init; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed record TestStreamRequest : IStreamRequest<int>
|
||||
{
|
||||
public int[] Values { get; init; } = [];
|
||||
}
|
||||
|
||||
// ✅ 这些 Handler 使用 Mediator.IRequestHandler
|
||||
public sealed class TestRequestHandler : IRequestHandler<TestRequest, int>
|
||||
{
|
||||
public ValueTask<int> Handle(TestRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
return new ValueTask<int>(request.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TestCommandHandler : IRequestHandler<TestCommand, Unit>
|
||||
{
|
||||
public ValueTask<Unit> Handle(TestCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.ShouldExecute)
|
||||
{
|
||||
request.Executed = true;
|
||||
}
|
||||
|
||||
return ValueTask.FromResult(Unit.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TestCommandWithResultHandler : IRequestHandler<TestCommandWithResult, int>
|
||||
{
|
||||
public ValueTask<int> Handle(TestCommandWithResult request, CancellationToken cancellationToken)
|
||||
{
|
||||
return new ValueTask<int>(request.ResultValue);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TestQueryHandler : IRequestHandler<TestQuery, string>
|
||||
{
|
||||
public ValueTask<string> Handle(TestQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return new ValueTask<string>(request.QueryResult);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TestNotificationHandler : INotificationHandler<TestNotification>
|
||||
{
|
||||
public static string? LastReceivedMessage { get; set; }
|
||||
|
||||
public ValueTask Handle(TestNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
LastReceivedMessage = notification.Message;
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TestStreamRequestHandler : IStreamRequestHandler<TestStreamRequest, int>
|
||||
{
|
||||
public async IAsyncEnumerable<int> Handle(
|
||||
TestStreamRequest request,
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken)
|
||||
{
|
||||
foreach (var value in request.Values)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
yield return value;
|
||||
await Task.Yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Test Classes - Legacy CQRS (旧实现)
|
||||
|
||||
public class TestSystem : ISystem
|
||||
{
|
||||
private IArchitectureContext _context = null!;
|
||||
public int Id { get; init; }
|
||||
|
||||
public void SetContext(IArchitectureContext context) => _context = context;
|
||||
public IArchitectureContext GetContext() => _context;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnArchitecturePhase(ArchitecturePhase phase)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class TestModel : IModel
|
||||
{
|
||||
private IArchitectureContext _context = null!;
|
||||
public int Id { get; init; }
|
||||
|
||||
public void SetContext(IArchitectureContext context) => _context = context;
|
||||
public IArchitectureContext GetContext() => _context;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnArchitecturePhase(ArchitecturePhase phase)
|
||||
{
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class TestUtility : IUtility
|
||||
{
|
||||
private IArchitectureContext _context = null!;
|
||||
public int Id { get; init; }
|
||||
|
||||
public void SetContext(IArchitectureContext context) => _context = context;
|
||||
public IArchitectureContext GetContext() => _context;
|
||||
}
|
||||
|
||||
// ✅ 使用你框架的 ICommand
|
||||
public class TestTraditionalCommand : ICommand
|
||||
{
|
||||
private IArchitectureContext _context = null!;
|
||||
public bool Executed { get; private set; }
|
||||
|
||||
public void Execute() => Executed = true;
|
||||
public void SetContext(IArchitectureContext context) => _context = context;
|
||||
public IArchitectureContext GetContext() => _context;
|
||||
}
|
||||
|
||||
// ✅ 使用你框架的 IQuery
|
||||
public class TestTraditionalQuery : IQuery<int>
|
||||
{
|
||||
private IArchitectureContext _context = null!;
|
||||
public int Result { get; init; }
|
||||
|
||||
public int Do() => Result;
|
||||
public void SetContext(IArchitectureContext context) => _context = context;
|
||||
public IArchitectureContext GetContext() => _context;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -12,7 +12,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LanguageExt.Core" Version="4.4.9"/>
|
||||
<PackageReference Include="Mediator.Abstractions" Version="3.0.1"/>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.3"/>
|
||||
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -27,6 +27,11 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||
/// </summary>
|
||||
private IMediator? Mediator => GetOrCache<IMediator>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取 ISender 实例(更轻量的发送器)
|
||||
/// </summary>
|
||||
private ISender? Sender => GetOrCache<ISender>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取 IPublisher 实例(用于发布通知)
|
||||
/// </summary>
|
||||
@ -97,6 +102,68 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||
return SendRequestAsync(request).AsTask().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 异步发送命令并返回结果
|
||||
/// 通过Mediator模式发送命令请求,支持取消操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
||||
/// <param name="command">要发送的命令对象</param>
|
||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||
/// <returns>包含命令执行结果的ValueTask</returns>
|
||||
public async ValueTask<TResponse> SendCommandAsync<TResponse>(Mediator.ICommand<TResponse> command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
var sender = Sender;
|
||||
if (sender == null)
|
||||
throw new InvalidOperationException("Sender not registered.");
|
||||
|
||||
return await sender.Send(command, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性)
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
||||
/// <param name="command">要发送的命令对象</param>
|
||||
/// <returns>命令执行结果</returns>
|
||||
public TResponse SendCommand<TResponse>(Mediator.ICommand<TResponse> command)
|
||||
{
|
||||
return SendCommandAsync(command).AsTask().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 异步发送查询并返回结果
|
||||
/// 通过Mediator模式发送查询请求,支持取消操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||
/// <param name="command">要发送的查询对象</param>
|
||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||
/// <returns>包含查询结果的ValueTask</returns>
|
||||
public async ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
var sender = Sender;
|
||||
if (sender == null)
|
||||
throw new InvalidOperationException("Sender not registered.");
|
||||
|
||||
return await sender.Send(command, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性)
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||
/// <param name="command">要发送的查询对象</param>
|
||||
/// <returns>查询结果</returns>
|
||||
public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command)
|
||||
{
|
||||
return SendQueryAsync(command).AsTask().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [Mediator] 发布通知(一对多)
|
||||
/// 用于事件驱动场景,多个处理器可以同时处理同一个通知
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user