using GFramework.Core.Abstractions.command; using GFramework.Core.Abstractions.rule; namespace GFramework.Core.extensions; /// /// 提供对 IContextAware 接口的命令执行扩展方法 /// public static class ContextAwareCommandExtensions { /// /// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性) /// /// 命令响应类型 /// 实现 IContextAware 接口的对象 /// 要发送的命令对象 /// 命令执行结果 /// 当 contextAware 或 command 为 null 时抛出 public static TResponse SendCommand(this IContextAware contextAware, Mediator.ICommand command) { ArgumentNullException.ThrowIfNull(contextAware); ArgumentNullException.ThrowIfNull(command); var context = contextAware.GetContext(); return context.SendCommand(command); } /// /// 发送一个带返回结果的命令 /// /// 命令执行结果类型 /// 实现 IContextAware 接口的对象 /// 要发送的命令 /// 命令执行结果 /// 当 contextAware 或 command 为 null 时抛出 public static TResult SendCommand(this IContextAware contextAware, Abstractions.command.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, Abstractions.command.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); } }