using GFramework.Core.Abstractions.Command;
using GFramework.Core.Abstractions.Rule;
namespace GFramework.Core.Extensions;
///
/// 提供对 IContextAware 接口的命令执行扩展方法
///
public static class ContextAwareCommandExtensions
{
///
/// 发送一个带返回结果的命令
///
/// 命令执行结果类型
/// 实现 IContextAware 接口的对象
/// 要发送的命令
/// 命令执行结果
/// 当 contextAware 或 command 为 null 时抛出
public static TResult SendCommand(this IContextAware contextAware,
ICommand command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var context = contextAware.GetContext();
return context.SendCommand(command);
}
///
/// 发送一个无返回结果的命令
///
/// 实现 IContextAware 接口的对象
/// 要发送的命令
/// 当 contextAware 或 command 为 null 时抛出
public static void SendCommand(this IContextAware contextAware, ICommand command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var context = contextAware.GetContext();
context.SendCommand(command);
}
///
/// 发送并异步执行一个无返回值的命令
///
/// 实现 IContextAware 接口的对象
/// 要发送的命令
/// 当 contextAware 或 command 为 null 时抛出
public static async Task SendCommandAsync(this IContextAware contextAware, IAsyncCommand command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var context = contextAware.GetContext();
await context.SendCommandAsync(command);
}
///
/// 发送并异步执行一个带返回值的命令
///
/// 命令执行结果类型
/// 实现 IContextAware 接口的对象
/// 要发送的命令
/// 命令执行结果
/// 当 contextAware 或 command 为 null 时抛出
public static async Task SendCommandAsync(this IContextAware contextAware,
IAsyncCommand command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
var context = contextAware.GetContext();
return await context.SendCommandAsync(command);
}
}