using GFramework.Core.Abstractions.rule; using Mediator; namespace GFramework.Core.extensions; /// /// 提供对 IContextAware 接口的 Mediator 命令扩展方法 /// 使用 Mediator 库的命令模式 /// public static class ContextAwareMediatorCommandExtensions { /// /// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性) /// /// 命令响应类型 /// 实现 IContextAware 接口的对象 /// 要发送的命令对象 /// 命令执行结果 /// 当 contextAware 或 command 为 null 时抛出 public static TResponse SendCommand(this IContextAware contextAware, ICommand command) { ArgumentNullException.ThrowIfNull(contextAware); ArgumentNullException.ThrowIfNull(command); var context = contextAware.GetContext(); return context.SendCommand(command); } /// /// [Mediator] 异步发送命令并返回结果 /// /// 命令响应类型 /// 实现 IContextAware 接口的对象 /// 要发送的命令对象 /// 取消令牌,用于取消操作 /// 包含命令执行结果的ValueTask /// 当 contextAware 或 command 为 null 时抛出 public static ValueTask SendCommandAsync(this IContextAware contextAware, ICommand command, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(contextAware); ArgumentNullException.ThrowIfNull(command); var context = contextAware.GetContext(); return context.SendCommandAsync(command, cancellationToken); } }