From a082b770a9c9c45325829fecb9168bfaa7443c54 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sat, 14 Feb 2026 13:49:13 +0800
Subject: [PATCH] =?UTF-8?q?feat(architecture):=20=E9=9B=86=E6=88=90=20Medi?=
=?UTF-8?q?ator=20=E6=A8=A1=E5=BC=8F=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 添加 Mediator 和 IPublisher 实例的延迟加载功能
- 实现 SendRequestAsync 方法用于发送 Command/Query 请求
- 添加 PublishAsync 方法用于发布通知事件
- 提供 CreateStream 方法支持流式数据处理
- 增加 SendAsync、QueryAsync、PublishEventAsync 等便捷扩展方法
- 更新 SendCommand 和 SendQuery 方法使用完整命名空间
- 在 IArchitectureContext 接口中添加 Mediator 相关方法定义
---
.../architecture/IArchitectureContext.cs | 65 +++++++-
.../architecture/ArchitectureContext.cs | 144 +++++++++++++++++-
2 files changed, 205 insertions(+), 4 deletions(-)
diff --git a/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs b/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs
index 760f7cb..01b121d 100644
--- a/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs
+++ b/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs
@@ -5,6 +5,8 @@ using GFramework.Core.Abstractions.model;
using GFramework.Core.Abstractions.query;
using GFramework.Core.Abstractions.system;
using GFramework.Core.Abstractions.utility;
+using Mediator;
+using ICommand = GFramework.Core.Abstractions.command.ICommand;
namespace GFramework.Core.Abstractions.architecture;
@@ -53,7 +55,7 @@ public interface IArchitectureContext
/// 命令执行结果类型
/// 要发送的命令
/// 命令执行结果
- TResult SendCommand(ICommand command);
+ TResult SendCommand(command.ICommand command);
///
/// 发送并异步执行一个命令
@@ -75,7 +77,7 @@ public interface IArchitectureContext
/// 查询结果类型
/// 要发送的查询
/// 查询结果
- TResult SendQuery(IQuery query);
+ TResult SendQuery(query.IQuery query);
///
/// 异步发送一个查询请求
@@ -113,6 +115,65 @@ public interface IArchitectureContext
/// 要取消注册的事件回调方法
void UnRegisterEvent(Action onEvent);
+ ///
+ /// 发送请求(统一处理 Command/Query)
+ ///
+ ValueTask SendRequestAsync(
+ IRequest request,
+ CancellationToken cancellationToken = default);
+
+ ///
+ /// 发送请求(同步版本,不推荐)
+ ///
+ TResponse SendRequest(IRequest request);
+
+ ///
+ /// 发布通知(一对多事件)
+ ///
+ ValueTask PublishAsync(
+ TNotification notification,
+ CancellationToken cancellationToken = default)
+ where TNotification : INotification;
+
+ ///
+ /// 创建流式请求(用于大数据集)
+ ///
+ IAsyncEnumerable CreateStream(
+ IStreamRequest request,
+ CancellationToken cancellationToken = default);
+
+ // === 便捷扩展方法 ===
+
+ ///
+ /// 发送命令(无返回值)
+ ///
+ ValueTask SendAsync(
+ TCommand command,
+ CancellationToken cancellationToken = default)
+ where TCommand : IRequest;
+
+ ///
+ /// 发送命令(有返回值)
+ ///
+ ValueTask SendAsync(
+ IRequest command,
+ CancellationToken cancellationToken = default);
+
+ ///
+ /// 发送查询
+ ///
+ ValueTask QueryAsync(
+ IRequest query,
+ CancellationToken cancellationToken = default);
+
+ ///
+ /// 发布事件通知
+ ///
+ ValueTask PublishEventAsync(
+ TNotification notification,
+ CancellationToken cancellationToken = default)
+ where TNotification : INotification;
+
///
/// 获取环境对象
///
diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs
index 5d43303..2dda931 100644
--- a/GFramework.Core/architecture/ArchitectureContext.cs
+++ b/GFramework.Core/architecture/ArchitectureContext.cs
@@ -7,6 +7,8 @@ using GFramework.Core.Abstractions.model;
using GFramework.Core.Abstractions.query;
using GFramework.Core.Abstractions.system;
using GFramework.Core.Abstractions.utility;
+using Mediator;
+using ICommand = GFramework.Core.Abstractions.command.ICommand;
namespace GFramework.Core.architecture;
@@ -18,6 +20,18 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
private readonly Dictionary _serviceCache = new();
+ #region Mediator Integration (新实现)
+
+ ///
+ /// 获取 Mediator 实例(延迟加载)
+ ///
+ private IMediator? Mediator => GetOrCache();
+
+ ///
+ /// 获取 IPublisher 实例(用于发布通知)
+ ///
+ private IPublisher? Publisher => GetOrCache();
+
///
/// 获取指定类型的服务实例,如果缓存中存在则直接返回,否则从容器中获取并缓存
///
@@ -49,6 +63,132 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
return service;
}
+ ///
+ /// [Mediator] 发送请求(Command/Query)
+ /// 这是推荐的新方式,统一处理命令和查询
+ ///
+ /// 响应类型
+ /// 请求对象(Command 或 Query)
+ /// 取消令牌
+ /// 响应结果
+ /// 当 Mediator 未注册时抛出
+ public async ValueTask SendRequestAsync(
+ IRequest request,
+ CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(request);
+
+ var mediator = Mediator;
+ if (mediator == null)
+ throw new InvalidOperationException(
+ "Mediator not registered. Call EnableMediator() in your Architecture.Init() method.");
+
+ return await mediator.Send(request, cancellationToken);
+ }
+
+ ///
+ /// [Mediator] 发送请求的同步版本(不推荐,仅用于兼容性)
+ ///
+ /// 响应类型
+ /// 请求对象
+ /// 响应结果
+ public TResponse SendRequest(IRequest request)
+ {
+ return SendRequestAsync(request).AsTask().GetAwaiter().GetResult();
+ }
+
+ ///
+ /// [Mediator] 发布通知(一对多)
+ /// 用于事件驱动场景,多个处理器可以同时处理同一个通知
+ ///
+ /// 通知类型
+ /// 通知对象
+ /// 取消令牌
+ public async ValueTask PublishAsync(
+ TNotification notification,
+ CancellationToken cancellationToken = default)
+ where TNotification : INotification
+ {
+ ArgumentNullException.ThrowIfNull(notification);
+
+ var publisher = Publisher;
+ if (publisher == null)
+ throw new InvalidOperationException("Publisher not registered.");
+
+ await publisher.Publish(notification, cancellationToken);
+ }
+
+ ///
+ /// [Mediator] 发送请求并返回流(用于大数据集)
+ ///
+ /// 响应项类型
+ /// 流式请求
+ /// 取消令牌
+ /// 异步流
+ public IAsyncEnumerable CreateStream(
+ IStreamRequest request,
+ CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(request);
+
+ var mediator = Mediator;
+ if (mediator == null)
+ throw new InvalidOperationException("Mediator not registered.");
+
+ return mediator.CreateStream(request, cancellationToken);
+ }
+
+ #endregion
+
+ #region Mediator Extension Methods (便捷方法)
+
+ ///
+ /// [扩展] 发送命令(无返回值)
+ /// 语法糖,等同于 SendRequestAsync<Unit>
+ ///
+ public async ValueTask SendAsync(
+ TCommand command,
+ CancellationToken cancellationToken = default)
+ where TCommand : IRequest
+ {
+ await SendRequestAsync(command, cancellationToken);
+ }
+
+ ///
+ /// [扩展] 发送命令(有返回值)
+ /// 语法糖,等同于 SendRequestAsync<TResponse>
+ ///
+ public async ValueTask SendAsync(
+ IRequest command,
+ CancellationToken cancellationToken = default)
+ {
+ return await SendRequestAsync(command, cancellationToken);
+ }
+
+ ///
+ /// [扩展] 发送查询
+ /// 语法糖,等同于 SendRequestAsync,语义更清晰
+ ///
+ public async ValueTask QueryAsync(
+ IRequest query,
+ CancellationToken cancellationToken = default)
+ {
+ return await SendRequestAsync(query, cancellationToken);
+ }
+
+ ///
+ /// [扩展] 发布事件通知
+ /// 语法糖,等同于 PublishAsync
+ ///
+ public async ValueTask PublishEventAsync(
+ TNotification notification,
+ CancellationToken cancellationToken = default)
+ where TNotification : INotification
+ {
+ await PublishAsync(notification, cancellationToken);
+ }
+
+ #endregion
#region Query Execution
@@ -58,7 +198,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
/// 查询结果类型
/// 要发送的查询
/// 查询结果
- public TResult SendQuery(IQuery query)
+ public TResult SendQuery(Abstractions.query.IQuery query)
{
if (query == null) throw new ArgumentNullException(nameof(query));
var queryBus = GetOrCache();
@@ -135,7 +275,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
/// 命令执行结果类型
/// 要发送的命令
/// 命令执行结果
- public TResult SendCommand(ICommand command)
+ public TResult SendCommand(Abstractions.command.ICommand command)
{
ArgumentNullException.ThrowIfNull(command);
var commandBus = GetOrCache();