mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 将原有的 ContextAwareCommandExtensions 中的 Mediator 相关方法分离到新的 ContextAwareMediatorCommandExtensions 类中 - 将原有的 ContextAwareQueryExtensions 中的 Mediator 相关方法分离到新的 ContextAwareMediatorQueryExtensions 类中 - 移除 ContextAwareCommandExtensions 中的异步命令方法 SendCommandAsync - 移除 ContextAwareQueryExtensions 中的异步查询方法 SendQueryAsync - 修复了 ContextAwareCommandExtensions 中的泛型类型参数引用问题 - 统一了代码格式和命名空间引用 - 保持了原有功能的向后兼容性
95 lines
4.0 KiB
C#
95 lines
4.0 KiB
C#
using GFramework.Core.Abstractions.command;
|
|
using GFramework.Core.Abstractions.rule;
|
|
|
|
namespace GFramework.Core.extensions;
|
|
|
|
/// <summary>
|
|
/// 提供对 IContextAware 接口的命令执行扩展方法
|
|
/// </summary>
|
|
public static class ContextAwareCommandExtensions
|
|
{
|
|
/// <summary>
|
|
/// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性)
|
|
/// </summary>
|
|
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
|
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
|
/// <param name="command">要发送的命令对象</param>
|
|
/// <returns>命令执行结果</returns>
|
|
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
|
|
public static TResponse SendCommand<TResponse>(this IContextAware contextAware,
|
|
Mediator.ICommand<TResponse> command)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(contextAware);
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
|
|
var context = contextAware.GetContext();
|
|
return context.SendCommand(command);
|
|
}
|
|
|
|
/// <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,
|
|
Abstractions.command.ICommand<TResult> command)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(contextAware);
|
|
ArgumentNullException.ThrowIfNull(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, Abstractions.command.ICommand command)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(contextAware);
|
|
ArgumentNullException.ThrowIfNull(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)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(contextAware);
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
|
|
var context = contextAware.GetContext();
|
|
await context.SendCommandAsync(command);
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(contextAware);
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
|
|
var context = contextAware.GetContext();
|
|
return await context.SendCommandAsync(command);
|
|
}
|
|
} |