From d0e7a9fb9bd92da7b311a56dc3268af79611e5d2 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 14 Feb 2026 22:16:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(architecture):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=9E=B6=E6=9E=84=E4=B8=8A=E4=B8=8B=E6=96=87=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=92=8C=E6=9F=A5=E8=AF=A2=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除旧的 Mediator 扩展方法区域标记 - 重新组织命令发送方法,将异步和同步版本分离 - 更新 SendCommand 和 SendQuery 方法的实现逻辑 - 为查询操作添加新的同步和异步发送方法 - 调整命令执行器方法的参数类型和返回值 - 优化方法注释文档以提高可读性 --- .../architecture/ArchitectureContext.cs | 164 +++++++++--------- 1 file changed, 80 insertions(+), 84 deletions(-) diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs index e69f4de..d0c51a9 100644 --- a/GFramework.Core/architecture/ArchitectureContext.cs +++ b/GFramework.Core/architecture/ArchitectureContext.cs @@ -102,68 +102,6 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext return SendRequestAsync(request).AsTask().GetAwaiter().GetResult(); } - /// - /// [Mediator] 异步发送命令并返回结果 - /// 通过Mediator模式发送命令请求,支持取消操作 - /// - /// 命令响应类型 - /// 要发送的命令对象 - /// 取消令牌,用于取消操作 - /// 包含命令执行结果的ValueTask - public async ValueTask SendCommandAsync(Mediator.ICommand 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); - } - - /// - /// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性) - /// - /// 命令响应类型 - /// 要发送的命令对象 - /// 命令执行结果 - public TResponse SendCommand(Mediator.ICommand command) - { - return SendCommandAsync(command).AsTask().GetAwaiter().GetResult(); - } - - /// - /// [Mediator] 异步发送查询并返回结果 - /// 通过Mediator模式发送查询请求,支持取消操作 - /// - /// 查询响应类型 - /// 要发送的查询对象 - /// 取消令牌,用于取消操作 - /// 包含查询结果的ValueTask - public async ValueTask SendQueryAsync(Mediator.IQuery 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); - } - - /// - /// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性) - /// - /// 查询响应类型 - /// 要发送的查询对象 - /// 查询结果 - public TResponse SendQuery(Mediator.IQuery command) - { - return SendQueryAsync(command).AsTask().GetAwaiter().GetResult(); - } - /// /// [Mediator] 发布通知(一对多) /// 用于事件驱动场景,多个处理器可以同时处理同一个通知 @@ -205,10 +143,6 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext return mediator.CreateStream(request, cancellationToken); } - #endregion - - #region Mediator Extension Methods (便捷方法) - /// /// [扩展] 发送命令(无返回值) /// 语法糖,等同于 SendRequestAsync<Unit> @@ -273,6 +207,17 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext return queryBus.Send(query); } + /// + /// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性) + /// + /// 查询响应类型 + /// 要发送的查询对象 + /// 查询结果 + public TResponse SendQuery(Mediator.IQuery command) + { + return SendQueryAsync(command).AsTask().GetAwaiter().GetResult(); + } + /// /// 异步发送一个查询请求 /// @@ -287,6 +232,26 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext return await asyncQueryBus.SendAsync(query); } + /// + /// [Mediator] 异步发送查询并返回结果 + /// 通过Mediator模式发送查询请求,支持取消操作 + /// + /// 查询响应类型 + /// 要发送的查询对象 + /// 取消令牌,用于取消操作 + /// 包含查询结果的ValueTask + public async ValueTask SendQueryAsync(Mediator.IQuery 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); + } + #endregion #region Component Retrieval @@ -326,28 +291,23 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext #region Command Execution /// - /// 发送一个命令请求 + /// [Mediator] 异步发送命令并返回结果 + /// 通过Mediator模式发送命令请求,支持取消操作 /// - /// 要发送的命令 - public void SendCommand(ICommand command) + /// 命令响应类型 + /// 要发送的命令对象 + /// 取消令牌,用于取消操作 + /// 包含命令执行结果的ValueTask + public async ValueTask SendCommandAsync(Mediator.ICommand command, + CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(command); - var commandBus = GetOrCache(); - commandBus?.Send(command); - } - /// - /// 发送一个带返回值的命令请求 - /// - /// 命令执行结果类型 - /// 要发送的命令 - /// 命令执行结果 - public TResult SendCommand(Abstractions.command.ICommand command) - { - ArgumentNullException.ThrowIfNull(command); - var commandBus = GetOrCache(); - if (commandBus == null) throw new InvalidOperationException("ICommandExecutor not registered"); - return commandBus.Send(command); + var sender = Sender; + if (sender == null) + throw new InvalidOperationException("Sender not registered."); + + return await sender.Send(command, cancellationToken); } /// @@ -376,6 +336,42 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext return await commandBus.SendAsync(command); } + /// + /// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性) + /// + /// 命令响应类型 + /// 要发送的命令对象 + /// 命令执行结果 + public TResponse SendCommand(Mediator.ICommand command) + { + return SendCommandAsync(command).AsTask().GetAwaiter().GetResult(); + } + + /// + /// 发送一个命令请求 + /// + /// 要发送的命令 + public void SendCommand(ICommand command) + { + ArgumentNullException.ThrowIfNull(command); + var commandBus = GetOrCache(); + commandBus?.Send(command); + } + + /// + /// 发送一个带返回值的命令请求 + /// + /// 命令执行结果类型 + /// 要发送的命令 + /// 命令执行结果 + public TResult SendCommand(Abstractions.command.ICommand command) + { + ArgumentNullException.ThrowIfNull(command); + var commandBus = GetOrCache(); + if (commandBus == null) throw new InvalidOperationException("ICommandExecutor not registered"); + return commandBus.Send(command); + } + #endregion #region Event Management