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