refactor(ContextAwareExtensions): 重构命令发送方法并优化代码结构

- 移除重复的SendCommand同步方法实现
- 统一Context变量命名规范,修复大小写不一致问题
- 调整方法顺序并重新组织代码结构
- 完善IAsyncCommand接口的XML文档注释
- 优化异步命令执行的实现逻辑
This commit is contained in:
GeWuYou 2026-01-17 19:26:13 +08:00
parent a45bf27c8b
commit e29b6da9e1
2 changed files with 52 additions and 52 deletions

View File

@ -3,12 +3,27 @@ using GFramework.Core.Abstractions.rule;
namespace GFramework.Core.Abstractions.command;
/// <summary>
/// 表示一个异步命令接口,该命令不返回结果
/// </summary>
public interface IAsyncCommand : IContextAware
{
/// <summary>
/// 异步执行命令
/// </summary>
/// <returns>表示异步操作的任务</returns>
Task ExecuteAsync();
}
/// <summary>
/// 表示一个异步命令接口,该命令返回指定类型的结果
/// </summary>
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
public interface IAsyncCommand<TResult> : IContextAware
{
/// <summary>
/// 异步执行命令并返回结果
/// </summary>
/// <returns>表示异步操作的任务,任务结果为命令执行的返回值</returns>
Task<TResult> ExecuteAsync();
}

View File

@ -73,38 +73,6 @@ public static class ContextAwareExtensions
return context.SendQuery(query);
}
/// <summary>
/// 发送一个无返回结果的命令
/// </summary>
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
/// <param name="command">要发送的命令</param>
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
public static void SendCommand(this IContextAware contextAware, ICommand command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var context = contextAware.GetContext();
context.SendCommand(command);
}
/// <summary>
/// 发送一个带返回值的命令
/// </summary>
/// <typeparam name="TResult">命令执行结果类型</typeparam>
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
/// <param name="command">要发送的命令</param>
/// <returns>命令执行结果</returns>
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
public static TResult SendCommand<TResult>(this IContextAware contextAware, ICommand<TResult> command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var Context = contextAware.GetContext();
return Context.SendCommand(command);
}
/// <summary>
/// 发送并异步执行一个无返回值的命令
/// </summary>
@ -116,8 +84,8 @@ public static class ContextAwareExtensions
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var Context = contextAware.GetContext();
await Context.SendCommandAsync(command);
var context = contextAware.GetContext();
await context.SendCommandAsync(command);
}
/// <summary>
@ -134,27 +102,11 @@ public static class ContextAwareExtensions
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var Context = contextAware.GetContext();
return await Context.SendCommandAsync(command);
}
/// <summary>
/// 发送一个事件
/// </summary>
/// <typeparam name="TResult">命令执行结果类型</typeparam>
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
/// <param name="command">要发送的命令</param>
/// <returns>命令执行结果</returns>
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
public static TResult SendCommand<TResult>(this IContextAware contextAware, ICommand<TResult> command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var context = contextAware.GetContext();
return context.SendCommand(command);
return await context.SendCommandAsync(command);
}
/// <summary>
/// 发送一个事件
/// </summary>
@ -184,6 +136,39 @@ public static class ContextAwareExtensions
context.SendEvent(e);
}
/// <summary>
/// 发送一个事件
/// </summary>
/// <typeparam name="TResult">命令执行结果类型</typeparam>
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
/// <param name="command">要发送的命令</param>
/// <returns>命令执行结果</returns>
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
public static TResult SendCommand<TResult>(this IContextAware contextAware, ICommand<TResult> command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var context = contextAware.GetContext();
return context.SendCommand(command);
}
/// <summary>
/// 发送一个无返回结果的命令
/// </summary>
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
/// <param name="command">要发送的命令</param>
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
public static void SendCommand(this IContextAware contextAware, ICommand command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var context = contextAware.GetContext();
context.SendCommand(command);
}
/// <summary>
/// 注册事件处理器
/// </summary>