Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0d5c050
docs(architecture): 更新架构服务接口文档注释
GeWuYou Feb 13, 2026
ffe00e4
feat(ioc): 添加基于Type的依赖注入方法
GeWuYou Feb 13, 2026
6a03581
feat(ioc): 替换自定义IoC容器为Microsoft.Extensions.DependencyInjection
GeWuYou Feb 14, 2026
8e543a8
feat(ioc): 添加类型注册工厂和多接口注册功能
GeWuYou Feb 14, 2026
0f41d60
refactor(ioc): 重构MicrosoftDiContainer以支持未冻结状态下的服务获取
GeWuYou Feb 14, 2026
9ebd7d7
feat(core): 集成 Mediator 框架并配置服务
GeWuYou Feb 14, 2026
1430892
feat(core): 集成Mediator框架并添加CQRS行为支持
GeWuYou Feb 14, 2026
bb41897
feat(cqrs): 添加CQRS模式的抽象处理器基类
GeWuYou Feb 14, 2026
084d97e
feat(cqrs): 添加流式命令、查询和请求处理器基类
GeWuYou Feb 14, 2026
2fe0b28
feat(architecture): 集成 Mediator 模式支持
GeWuYou Feb 14, 2026
c4e82dc
feat(architecture): 添加Mediator模式支持并扩展架构上下文功能
GeWuYou Feb 14, 2026
f4e6fd1
refactor(tests): 更新查询接口类型引用
GeWuYou Feb 14, 2026
06c88ed
refactor(core): 替换Mediator集成实现为通用服务配置机制
GeWuYou Feb 14, 2026
ef98739
test(mediator): 添加Mediator高级特性和架构集成测试
GeWuYou Feb 14, 2026
8ee9c43
fix(mediator): 修复高级功能测试中的异常处理和断路器逻辑
GeWuYou Feb 14, 2026
ef3bfa1
refactor(architecture): 更新IArchitectureContext接口中的命令和查询方法签名
GeWuYou Feb 14, 2026
01f5f17
perf(cqrs): 优化LoggingBehavior性能并改进错误处理
GeWuYou Feb 14, 2026
9b966b9
perf(cqrs): 优化性能行为中的请求处理逻辑
GeWuYou Feb 14, 2026
0592441
fix(architecture): 修复中介者行为注册方法
GeWuYou Feb 14, 2026
4effffd
refactor(ioc): 为依赖注入容器注册方法添加线程安全锁
GeWuYou Feb 14, 2026
4f5bed0
refactor(ioc): 将Services属性重命名为GetServicesUnsafe以提高安全性
GeWuYou Feb 14, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Mediator.Abstractions" Version="3.0.1"/>
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions GFramework.Core.Abstractions/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

global using System;
global using System.Collections.Generic;
global using System.Runtime;
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;
40 changes: 40 additions & 0 deletions GFramework.Core.Abstractions/architecture/IArchitecture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GFramework.Core.Abstractions.model;
using GFramework.Core.Abstractions.system;
using GFramework.Core.Abstractions.utility;
using Microsoft.Extensions.DependencyInjection;

namespace GFramework.Core.Abstractions.architecture;

Expand All @@ -15,6 +16,14 @@ public interface IArchitecture : IAsyncInitializable
/// </summary>
IArchitectureContext Context { get; }

/// <summary>
/// 获取或设置用于配置服务集合的委托
/// </summary>
/// <value>
/// 一个可为空的委托,用于配置IServiceCollection实例
/// </value>
Action<IServiceCollection>? Configurator { get; }

/// <summary>
/// 初始化方法,用于执行对象的初始化操作
/// </summary>
Expand All @@ -39,6 +48,13 @@ public interface IArchitecture : IAsyncInitializable
/// <returns>注册的系统实例</returns>
T RegisterSystem<T>(T system) where T : ISystem;

/// <summary>
/// 注册系统实例到架构中
/// </summary>
/// <typeparam name="T">系统类型,必须实现ISystem接口</typeparam>
/// <param name="onCreated">系统实例创建后的回调函数,可为null</param>
void RegisterSystem<T>(Action<T>? onCreated = null) where T : class, ISystem;

/// <summary>
/// 注册模型实例到架构中
/// </summary>
Expand All @@ -47,6 +63,13 @@ public interface IArchitecture : IAsyncInitializable
/// <returns>注册的模型实例</returns>
T RegisterModel<T>(T model) where T : IModel;

/// <summary>
/// 注册模型实例到架构中
/// </summary>
/// <typeparam name="T">模型类型,必须实现IModel接口</typeparam>
/// <param name="onCreated">模型实例创建后的回调函数,可为null</param>
void RegisterModel<T>(Action<T>? onCreated = null) where T : class, IModel;

/// <summary>
/// 注册工具实例到架构中
/// </summary>
Expand All @@ -55,6 +78,23 @@ public interface IArchitecture : IAsyncInitializable
/// <returns>注册的工具实例</returns>
T RegisterUtility<T>(T utility) where T : IUtility;


/// <summary>
/// 注册工具类型并可选地指定创建回调
/// 当工具实例被创建时会调用指定的回调函数
/// </summary>
/// <typeparam name="T">工具类型,必须是引用类型且实现IUtility接口</typeparam>
/// <param name="onCreated">工具实例创建后的回调函数,可为null</param>
void RegisterUtility<T>(Action<T>? onCreated = null) where T : class, IUtility;

/// <summary>
/// 注册中介行为管道
/// 用于配置Mediator框架的行为拦截和处理逻辑
/// </summary>
/// <typeparam name="TBehavior">行为类型,必须是引用类型</typeparam>
void RegisterMediatorBehavior<TBehavior>()
where TBehavior : class;

/// <summary>
/// 安装架构模块
/// </summary>
Expand Down
103 changes: 101 additions & 2 deletions GFramework.Core.Abstractions/architecture/IArchitectureContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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;

Expand Down Expand Up @@ -53,7 +55,7 @@ public interface IArchitectureContext
/// <typeparam name="TResult">命令执行结果类型</typeparam>
/// <param name="command">要发送的命令</param>
/// <returns>命令执行结果</returns>
TResult SendCommand<TResult>(ICommand<TResult> command);
TResult SendCommand<TResult>(command.ICommand<TResult> command);

/// <summary>
/// 发送并异步执行一个命令
Comment thread
GeWuYou marked this conversation as resolved.
Expand All @@ -75,7 +77,7 @@ public interface IArchitectureContext
/// <typeparam name="TResult">查询结果类型</typeparam>
/// <param name="query">要发送的查询</param>
/// <returns>查询结果</returns>
TResult SendQuery<TResult>(IQuery<TResult> query);
TResult SendQuery<TResult>(query.IQuery<TResult> query);

/// <summary>
/// 异步发送一个查询请求
Expand Down Expand Up @@ -113,6 +115,103 @@ public interface IArchitectureContext
/// <param name="onEvent">要取消注册的事件回调方法</param>
void UnRegisterEvent<TEvent>(Action<TEvent> onEvent);

/// <summary>
/// 发送请求(统一处理 Command/Query)
/// </summary>
ValueTask<TResponse> SendRequestAsync<TResponse>(
IRequest<TResponse> request,
CancellationToken cancellationToken = default);

/// <summary>
/// 发送请求(同步版本,不推荐)
/// </summary>
TResponse SendRequest<TResponse>(IRequest<TResponse> request);

/// <summary>
/// [Mediator] 异步发送命令并返回结果
/// 通过Mediator模式发送命令请求,支持取消操作
/// </summary>
/// <typeparam name="TResponse">命令响应类型</typeparam>
/// <param name="command">要发送的命令对象</param>
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
/// <returns>包含命令执行结果的ValueTask</returns>
ValueTask<TResponse> SendCommandAsync<TResponse>(Mediator.ICommand<TResponse> command,
CancellationToken cancellationToken = default);

/// <summary>
/// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性)
/// </summary>
/// <typeparam name="TResponse">命令响应类型</typeparam>
/// <param name="command">要发送的命令对象</param>
/// <returns>命令执行结果</returns>
TResponse SendCommand<TResponse>(Mediator.ICommand<TResponse> command);

/// <summary>
/// [Mediator] 异步发送查询并返回结果
/// 通过Mediator模式发送查询请求,支持取消操作
/// </summary>
/// <typeparam name="TResponse">查询响应类型</typeparam>
/// <param name="command">要发送的查询对象</param>
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
/// <returns>包含查询结果的ValueTask</returns>
ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command,
CancellationToken cancellationToken = default);

/// <summary>
/// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性)
/// </summary>
/// <typeparam name="TResponse">查询响应类型</typeparam>
/// <param name="command">要发送的查询对象</param>
/// <returns>查询结果</returns>
TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command);

/// <summary>
/// 发布通知(一对多事件)
/// </summary>
ValueTask PublishAsync<TNotification>(
TNotification notification,
CancellationToken cancellationToken = default)
where TNotification : INotification;

/// <summary>
/// 创建流式请求(用于大数据集)
/// </summary>
IAsyncEnumerable<TResponse> CreateStream<TResponse>(
IStreamRequest<TResponse> request,
CancellationToken cancellationToken = default);

// === 便捷扩展方法 ===

/// <summary>
/// 发送命令(无返回值)
/// </summary>
ValueTask SendAsync<TCommand>(
TCommand command,
CancellationToken cancellationToken = default)
where TCommand : IRequest<Unit>;

/// <summary>
/// 发送命令(有返回值)
/// </summary>
ValueTask<TResponse> SendAsync<TResponse>(
IRequest<TResponse> command,
CancellationToken cancellationToken = default);

/// <summary>
/// 发送查询
/// </summary>
ValueTask<TResponse> QueryAsync<TResponse>(
IRequest<TResponse> query,
CancellationToken cancellationToken = default);

/// <summary>
/// 发布事件通知
/// </summary>
ValueTask PublishEventAsync<TNotification>(
TNotification notification,
CancellationToken cancellationToken = default)
where TNotification : INotification;

/// <summary>
/// 获取环境对象
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ public interface IArchitectureServices : IContextAware
IEventBus EventBus { get; }

/// <summary>
/// 获取命令总线
/// 获取命令执行器
/// </summary>
/// <returns>ICommandExecutor类型的命令执行器实例</returns>
public ICommandExecutor CommandExecutor { get; }

/// <summary>
/// 获取查询总线
/// 获取查询执行器
/// </summary>
/// <returns>IQueryExecutor类型的查询执行器实例</returns>
public IQueryExecutor QueryExecutor { get; }

/// <summary>
/// 获取异步查询总线
/// 获取异步查询执行器
/// </summary>
/// <returns>IAsyncQueryExecutor类型的异步查询执行器实例</returns>
public IAsyncQueryExecutor AsyncQueryExecutor { get; }
}
75 changes: 74 additions & 1 deletion GFramework.Core.Abstractions/ioc/IIocContainer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GFramework.Core.Abstractions.rule;
using GFramework.Core.Abstractions.system;
using Microsoft.Extensions.DependencyInjection;

namespace GFramework.Core.Abstractions.ioc;

Expand All @@ -19,6 +20,14 @@ public interface IIocContainer : IContextAware
/// <exception cref="InvalidOperationException">当该类型已经注册过单例时抛出异常</exception>
void RegisterSingleton<T>(T instance);

/// <summary>
/// 注册单例服务,指定服务类型和实现类型
/// 创建单例实例并在容器中注册
/// </summary>
/// <typeparam name="TService">服务接口或基类类型</typeparam>
/// <typeparam name="TImpl">具体的实现类型</typeparam>
void RegisterSingleton<TService, TImpl>()
where TImpl : class, TService where TService : class;

/// <summary>
/// 注册多个实例
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Expand All @@ -27,6 +36,13 @@ public interface IIocContainer : IContextAware
/// <param name="instance">要注册的实例</param>
public void RegisterPlurality(object instance);

/// <summary>
/// 注册多个实例
/// 将实例注册到其实现所有接口
/// </summary>
/// <typeparam name="T">要注册的实例类型</typeparam>
public void RegisterPlurality<T>() where T : class;

/// <summary>
/// 注册系统实例,将其绑定到其所有实现的接口上
/// </summary>
Expand All @@ -47,6 +63,29 @@ public interface IIocContainer : IContextAware
/// <param name="instance">要注册的实例对象</param>
void Register(Type type, object instance);

/// <summary>
/// 注册工厂方法来创建服务实例
/// 通过委托函数动态创建服务实例
/// </summary>
/// <typeparam name="TService">服务类型</typeparam>
/// <param name="factory">创建服务实例的工厂委托函数</param>
void RegisterFactory<TService>(Func<IServiceProvider, TService> factory) where TService : class;

/// <summary>
/// 注册中介行为管道
/// 用于配置Mediator框架的行为拦截和处理逻辑
/// </summary>
/// <typeparam name="TBehavior">行为类型,必须是引用类型</typeparam>
void RegisterMediatorBehavior<TBehavior>()
where TBehavior : class;


/// <summary>
/// 配置服务
/// </summary>
/// <param name="configurator">服务配置委托</param>
void ExecuteServicesHook(Action<IServiceCollection>? configurator = null);

#endregion

#region Get Methods
Expand All @@ -59,6 +98,15 @@ public interface IIocContainer : IContextAware
/// <returns>找到的第一个实例;如果未找到则返回 null</returns>
T? Get<T>() where T : class;

/// <summary>
/// 根据指定类型获取单个实例
/// 如果存在多个,只返回第一个
/// </summary>
/// <param name="type">期望获取的实例类型</param>
/// <returns>找到的第一个实例;如果未找到则返回 null</returns>
object? Get(Type type);


/// <summary>
/// 获取指定类型的必需实例
/// </summary>
Expand All @@ -67,13 +115,30 @@ public interface IIocContainer : IContextAware
/// <exception cref="InvalidOperationException">当没有注册实例或注册了多个实例时抛出</exception>
T GetRequired<T>() where T : class;

/// <summary>
/// 获取指定类型的必需实例
/// </summary>
/// <param name="type">期望获取的实例类型</param>
/// <returns>找到的唯一实例</returns>
/// <exception cref="InvalidOperationException">当没有注册实例或注册了多个实例时抛出</exception>
object GetRequired(Type type);


/// <summary>
/// 获取指定类型的所有实例(接口 / 抽象类推荐使用)
/// </summary>
/// <typeparam name="T">期望获取的实例类型</typeparam>
/// <returns>所有符合条件的实例列表;如果没有则返回空数组</returns>
IReadOnlyList<T> GetAll<T>() where T : class;

/// <summary>
/// 获取指定类型的所有实例
/// </summary>
/// <param name="type">期望获取的实例类型</param>
/// <returns>所有符合条件的实例列表;如果没有则返回空数组</returns>
IReadOnlyList<object> GetAll(Type type);


/// <summary>
/// 获取并排序(系统调度专用)
/// </summary>
Expand All @@ -91,7 +156,7 @@ public interface IIocContainer : IContextAware
/// </summary>
/// <typeparam name="T">要检查的类型</typeparam>
/// <returns>如果容器中包含指定类型的实例则返回true,否则返回false</returns>
bool Contains<T>();
bool Contains<T>() where T : class;

/// <summary>
/// 判断容器中是否包含某个具体的实例对象
Expand All @@ -107,8 +172,16 @@ public interface IIocContainer : IContextAware

/// <summary>
/// 冻结容器,防止后续修改
/// 调用此方法后,容器将变为只读状态,不能再注册新的服务实例
/// </summary>
void Freeze();

/// <summary>
/// 获取底层的服务集合
/// 提供对内部IServiceCollection的访问权限,用于高级配置和自定义操作
/// </summary>
/// <returns>底层的IServiceCollection实例</returns>
IServiceCollection Services { get; }

#endregion
}
5 changes: 5 additions & 0 deletions GFramework.Core.Tests/GFramework.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<TargetFrameworks>net10.0;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mediator.Abstractions" Version="3.0.1"/>
<PackageReference Include="Mediator.SourceGenerator" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1"/>
<PackageReference Include="Moq" Version="4.20.72"/>
<PackageReference Include="NUnit" Version="4.4.0"/>
Expand Down
Loading
Loading