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