GeWuYou 42ddb80248 feat(core): 添加异步查询系统并优化命令系统实现
- 新增 AbstractAsyncQuery 基类支持异步查询操作
- 实现 AsyncQueryBus 和 IAsyncQueryBus 查询总线功能
- 添加 IAsyncQuery 接口定义异步查询契约
- 重构 CommandBus 的 SendAsync 方法移除不必要的 await
- 为 AbstractAsyncCommand 添加完整 XML 文档注释
- 更新 TEST_COVERAGE_PLAN.md 反映测试覆盖率提升至 91.5%
2026-01-18 20:39:23 +08:00

62 lines
2.2 KiB
C#

using GFramework.Core.Abstractions.command;
using IAsyncCommand = GFramework.Core.Abstractions.command.IAsyncCommand;
namespace GFramework.Core.command;
/// <summary>
/// 命令总线实现类,用于发送和执行命令
/// </summary>
public sealed class CommandBus : ICommandBus
{
/// <summary>
/// 发送并执行无返回值的命令
/// </summary>
/// <param name="command">要执行的命令对象,不能为空</param>
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
public void Send(ICommand command)
{
ArgumentNullException.ThrowIfNull(command);
command.Execute();
}
/// <summary>
/// 发送并执行有返回值的命令
/// </summary>
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
/// <param name="command">要执行的命令对象,不能为空</param>
/// <returns>命令执行的结果</returns>
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
public TResult Send<TResult>(ICommand<TResult> command)
{
ArgumentNullException.ThrowIfNull(command);
return command.Execute();
}
/// <summary>
/// 发送并异步执行无返回值的命令
/// </summary>
/// <param name="command">要执行的命令对象,不能为空</param>
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
public Task SendAsync(IAsyncCommand command)
{
ArgumentNullException.ThrowIfNull(command);
return command.ExecuteAsync();
}
/// <summary>
/// 发送并异步执行有返回值的命令
/// </summary>
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
/// <param name="command">要执行的命令对象,不能为空</param>
/// <returns>命令执行的结果</returns>
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
public Task<TResult> SendAsync<TResult>(IAsyncCommand<TResult> command)
{
ArgumentNullException.ThrowIfNull(command);
return command.ExecuteAsync();
}
}