mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(core): 重构上下文感知命令和查询扩展方法
- 将原有的 ContextAwareCommandExtensions 中的 Mediator 相关方法分离到新的 ContextAwareMediatorCommandExtensions 类中 - 将原有的 ContextAwareQueryExtensions 中的 Mediator 相关方法分离到新的 ContextAwareMediatorQueryExtensions 类中 - 移除 ContextAwareCommandExtensions 中的异步命令方法 SendCommandAsync - 移除 ContextAwareQueryExtensions 中的异步查询方法 SendQueryAsync - 修复了 ContextAwareCommandExtensions 中的泛型类型参数引用问题 - 统一了代码格式和命名空间引用 - 保持了原有功能的向后兼容性
This commit is contained in:
parent
bde1af2c2e
commit
ffaace4538
@ -59,24 +59,6 @@ public static class ContextAwareCommandExtensions
|
|||||||
context.SendCommand(command);
|
context.SendCommand(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// [Mediator] 异步发送命令并返回结果
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
|
||||||
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
|
||||||
/// <param name="command">要发送的命令对象</param>
|
|
||||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
|
||||||
/// <returns>包含命令执行结果的ValueTask</returns>
|
|
||||||
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
|
|
||||||
public static ValueTask<TResponse> SendCommandAsync<TResponse>(this IContextAware contextAware,
|
|
||||||
Mediator.ICommand<TResponse> command, CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
ArgumentNullException.ThrowIfNull(contextAware);
|
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
|
||||||
|
|
||||||
var context = contextAware.GetContext();
|
|
||||||
return context.SendCommandAsync(command, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送并异步执行一个无返回值的命令
|
/// 发送并异步执行一个无返回值的命令
|
||||||
|
|||||||
@ -0,0 +1,48 @@
|
|||||||
|
using GFramework.Core.Abstractions.rule;
|
||||||
|
using Mediator;
|
||||||
|
|
||||||
|
namespace GFramework.Core.extensions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 提供对 IContextAware 接口的 Mediator 命令扩展方法
|
||||||
|
/// 使用 Mediator 库的命令模式
|
||||||
|
/// </summary>
|
||||||
|
public static class ContextAwareMediatorCommandExtensions
|
||||||
|
{
|
||||||
|
/// <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,
|
||||||
|
ICommand<TResponse> command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(contextAware);
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
var context = contextAware.GetContext();
|
||||||
|
return context.SendCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [Mediator] 异步发送命令并返回结果
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
||||||
|
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
||||||
|
/// <param name="command">要发送的命令对象</param>
|
||||||
|
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||||
|
/// <returns>包含命令执行结果的ValueTask</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
|
||||||
|
public static ValueTask<TResponse> SendCommandAsync<TResponse>(this IContextAware contextAware,
|
||||||
|
ICommand<TResponse> command, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(contextAware);
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
var context = contextAware.GetContext();
|
||||||
|
return context.SendCommandAsync(command, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
using GFramework.Core.Abstractions.rule;
|
||||||
|
using Mediator;
|
||||||
|
|
||||||
|
namespace GFramework.Core.extensions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 提供对 IContextAware 接口的 Mediator 查询扩展方法
|
||||||
|
/// 使用 Mediator 库的查询模式
|
||||||
|
/// </summary>
|
||||||
|
public static class ContextAwareMediatorQueryExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性)
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||||
|
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
||||||
|
/// <param name="query">要发送的查询对象</param>
|
||||||
|
/// <returns>查询结果</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">当 contextAware 或 query 为 null 时抛出</exception>
|
||||||
|
public static TResponse SendQuery<TResponse>(this IContextAware contextAware, IQuery<TResponse> query)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(contextAware);
|
||||||
|
ArgumentNullException.ThrowIfNull(query);
|
||||||
|
|
||||||
|
var context = contextAware.GetContext();
|
||||||
|
return context.SendQuery(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [Mediator] 异步发送查询并返回结果
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||||
|
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
||||||
|
/// <param name="query">要发送的查询对象</param>
|
||||||
|
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||||
|
/// <returns>包含查询结果的ValueTask</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">当 contextAware 或 query 为 null 时抛出</exception>
|
||||||
|
public static ValueTask<TResponse> SendQueryAsync<TResponse>(this IContextAware contextAware,
|
||||||
|
IQuery<TResponse> query, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(contextAware);
|
||||||
|
ArgumentNullException.ThrowIfNull(query);
|
||||||
|
|
||||||
|
var context = contextAware.GetContext();
|
||||||
|
return context.SendQueryAsync(query, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -42,24 +42,6 @@ public static class ContextAwareQueryExtensions
|
|||||||
return context.SendQuery(query);
|
return context.SendQuery(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// [Mediator] 异步发送查询并返回结果
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
|
||||||
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
|
||||||
/// <param name="query">要发送的查询对象</param>
|
|
||||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
|
||||||
/// <returns>包含查询结果的ValueTask</returns>
|
|
||||||
/// <exception cref="ArgumentNullException">当 contextAware 或 query 为 null 时抛出</exception>
|
|
||||||
public static ValueTask<TResponse> SendQueryAsync<TResponse>(this IContextAware contextAware,
|
|
||||||
Mediator.IQuery<TResponse> query, CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
ArgumentNullException.ThrowIfNull(contextAware);
|
|
||||||
ArgumentNullException.ThrowIfNull(query);
|
|
||||||
|
|
||||||
var context = contextAware.GetContext();
|
|
||||||
return context.SendQueryAsync(query, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步发送一个查询请求
|
/// 异步发送一个查询请求
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user