GFramework/GFramework.Core/Extensions/ContextAwareCqrsExtensions.cs
GeWuYou 115fe65e88 docs(core): 添加 CQRS 和核心框架文档
- 新增 CQRS 模块详细文档,介绍命令查询职责分离模式
- 添加核心框架架构概述和五层架构设计说明
- 补充快速开始指南和最佳实践建议
- 完善包说明和组件联动机制介绍
- 添加架构生命周期管理和模块化设计说明
2026-04-14 22:54:27 +08:00

124 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GFramework.Core.Abstractions.Cqrs;
using GFramework.Core.Abstractions.Rule;
namespace GFramework.Core.Cqrs.Extensions;
/// <summary>
/// 提供对 <see cref="IContextAware" /> 接口的 CQRS 统一扩展方法。
/// 这些扩展直接委托给架构上下文的内建 CQRS runtime作为新的中性命名入口。
/// </summary>
public static class ContextAwareCqrsExtensions
{
/// <summary>
/// 发送请求(统一处理 Command/Query
/// </summary>
/// <typeparam name="TResponse">响应类型。</typeparam>
/// <param name="contextAware">实现 <see cref="IContextAware" /> 接口的对象。</param>
/// <param name="request">要发送的请求。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>请求结果。</returns>
public static ValueTask<TResponse> SendRequestAsync<TResponse>(
this IContextAware contextAware,
IRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(request);
return contextAware.GetContext().SendRequestAsync(request, cancellationToken);
}
/// <summary>
/// 发送请求(同步版本,不推荐)。
/// </summary>
/// <typeparam name="TResponse">响应类型。</typeparam>
/// <param name="contextAware">实现 <see cref="IContextAware" /> 接口的对象。</param>
/// <param name="request">要发送的请求。</param>
/// <returns>请求结果。</returns>
public static TResponse SendRequest<TResponse>(this IContextAware contextAware, IRequest<TResponse> request)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(request);
return contextAware.GetContext().SendRequest(request);
}
/// <summary>
/// 发布通知(一对多事件)。
/// </summary>
/// <typeparam name="TNotification">通知类型。</typeparam>
/// <param name="contextAware">实现 <see cref="IContextAware" /> 接口的对象。</param>
/// <param name="notification">要发布的通知。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>异步任务。</returns>
public static ValueTask PublishAsync<TNotification>(
this IContextAware contextAware,
TNotification notification,
CancellationToken cancellationToken = default)
where TNotification : INotification
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(notification);
return contextAware.GetContext().PublishAsync(notification, cancellationToken);
}
/// <summary>
/// 创建流式请求。
/// </summary>
/// <typeparam name="TResponse">响应类型。</typeparam>
/// <param name="contextAware">实现 <see cref="IContextAware" /> 接口的对象。</param>
/// <param name="request">流式请求。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>异步响应流。</returns>
public static IAsyncEnumerable<TResponse> CreateStream<TResponse>(
this IContextAware contextAware,
IStreamRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(request);
return contextAware.GetContext().CreateStream(request, cancellationToken);
}
/// <summary>
/// 发送无返回值命令。
/// </summary>
/// <typeparam name="TCommand">命令类型。</typeparam>
/// <param name="contextAware">实现 <see cref="IContextAware" /> 接口的对象。</param>
/// <param name="command">要发送的命令。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>异步任务。</returns>
public static ValueTask SendAsync<TCommand>(
this IContextAware contextAware,
TCommand command,
CancellationToken cancellationToken = default)
where TCommand : IRequest<Unit>
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
return contextAware.GetContext().SendAsync(command, cancellationToken);
}
/// <summary>
/// 发送带返回值命令。
/// </summary>
/// <typeparam name="TResponse">响应类型。</typeparam>
/// <param name="contextAware">实现 <see cref="IContextAware" /> 接口的对象。</param>
/// <param name="command">要发送的命令。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>命令执行结果。</returns>
public static ValueTask<TResponse> SendAsync<TResponse>(
this IContextAware contextAware,
IRequest<TResponse> command,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
return contextAware.GetContext().SendAsync(command, cancellationToken);
}
}