GFramework/GFramework.Core/Extensions/ContextAwareCommandExtensions.cs
gewuyou 9ce1fa630c refactor(core): 收敛 Core 扩展与测试的机械 warning
- 更新 ContextAware、Store 与通用扩展中的参数空校验写法以满足 analyzer 约束

- 简化 coroutine、pause、log 与 async 测试中的等待和断言包装并保持测试语义不变

- 调整测试替身异常类型与 Result 系列断言样例以减少低风险 warning 噪音
2026-04-25 10:38:48 +08:00

106 lines
3.6 KiB
C#

using GFramework.Core.Abstractions.Command;
using GFramework.Core.Abstractions.Rule;
namespace GFramework.Core.Extensions;
/// <summary>
/// 提供对 IContextAware 接口的命令执行扩展方法
/// </summary>
public static class ContextAwareCommandExtensions
{
/// <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)
{
if (contextAware is null)
{
throw new ArgumentNullException(nameof(contextAware));
}
if (command is null)
{
throw new ArgumentNullException(nameof(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)
{
if (contextAware is null)
{
throw new ArgumentNullException(nameof(contextAware));
}
if (command is null)
{
throw new ArgumentNullException(nameof(command));
}
var context = contextAware.GetContext();
context.SendCommand(command);
}
/// <summary>
/// 发送并异步执行一个无返回值的命令
/// </summary>
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
/// <param name="command">要发送的命令</param>
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
public static async Task SendCommandAsync(this IContextAware contextAware, IAsyncCommand command)
{
if (contextAware is null)
{
throw new ArgumentNullException(nameof(contextAware));
}
if (command is null)
{
throw new ArgumentNullException(nameof(command));
}
var context = contextAware.GetContext();
await context.SendCommandAsync(command).ConfigureAwait(false);
}
/// <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 async Task<TResult> SendCommandAsync<TResult>(this IContextAware contextAware,
IAsyncCommand<TResult> command)
{
if (contextAware is null)
{
throw new ArgumentNullException(nameof(contextAware));
}
if (command is null)
{
throw new ArgumentNullException(nameof(command));
}
var context = contextAware.GetContext();
return await context.SendCommandAsync(command).ConfigureAwait(false);
}
}