using GFramework.Core.Abstractions.Cqrs.Command;
using GFramework.Core.Abstractions.Rule;
namespace GFramework.Core.Cqrs.Extensions;
///
/// 提供对 接口的 CQRS 命令扩展方法。
///
public static class ContextAwareCqrsCommandExtensions
{
///
/// 发送命令的同步版本(不推荐,仅用于兼容同步调用链)。
///
/// 命令响应类型。
/// 实现 接口的对象。
/// 要发送的命令对象。
/// 命令执行结果。
///
/// 当 或 为 时抛出。
///
public static TResponse SendCommand(this IContextAware contextAware, ICommand command)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
return contextAware.GetContext().SendCommand(command);
}
///
/// 异步发送命令并返回结果。
///
/// 命令响应类型。
/// 实现 接口的对象。
/// 要发送的命令对象。
/// 取消令牌,用于取消操作。
/// 包含命令执行结果的 。
///
/// 当 或 为 时抛出。
///
public static ValueTask SendCommandAsync(
this IContextAware contextAware,
ICommand command,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
return contextAware.GetContext().SendCommandAsync(command, cancellationToken);
}
}