From 853bcf4315719e2d3731f5d974e608a3cffb6c9a Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 14 Feb 2026 10:55:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(ioc):=20=E6=B7=BB=E5=8A=A0=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=B3=A8=E5=86=8C=E5=B7=A5=E5=8E=82=E5=92=8C=E5=A4=9A?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=B3=A8=E5=86=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为 Architecture 添加 RegisterSystem、RegisterModel 和 RegisterUtility 泛型方法 - 支持通过 DI 容器自动创建实例并提供创建后回调钩子 - 在 MicrosoftDiContainer 中实现 RegisterPlurality 方法 - 支持将单个实例注册到其实现的所有接口 - 更新 IIocContainer 接口定义以匹配新功能 - 为 RegisterFactory 方法添加 class 约束确保类型安全 --- .../architecture/IArchitecture.cs | 23 +++++ .../ioc/IIocContainer.cs | 9 +- GFramework.Core/architecture/Architecture.cs | 85 +++++++++++++++++++ GFramework.Core/ioc/MicrosoftDiContainer.cs | 33 ++++++- 4 files changed, 148 insertions(+), 2 deletions(-) diff --git a/GFramework.Core.Abstractions/architecture/IArchitecture.cs b/GFramework.Core.Abstractions/architecture/IArchitecture.cs index 9f1a37e..8c4e0cf 100644 --- a/GFramework.Core.Abstractions/architecture/IArchitecture.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitecture.cs @@ -39,6 +39,13 @@ public interface IArchitecture : IAsyncInitializable /// 注册的系统实例 T RegisterSystem(T system) where T : ISystem; + /// + /// 注册系统实例到架构中 + /// + /// 系统类型,必须实现ISystem接口 + /// 系统实例创建后的回调函数,可为null + void RegisterSystem(Action? onCreated = null) where T : class, ISystem; + /// /// 注册模型实例到架构中 /// @@ -47,6 +54,13 @@ public interface IArchitecture : IAsyncInitializable /// 注册的模型实例 T RegisterModel(T model) where T : IModel; + /// + /// 注册模型实例到架构中 + /// + /// 模型类型,必须实现IModel接口 + /// 模型实例创建后的回调函数,可为null + void RegisterModel(Action? onCreated = null) where T : class, IModel; + /// /// 注册工具实例到架构中 /// @@ -55,6 +69,15 @@ public interface IArchitecture : IAsyncInitializable /// 注册的工具实例 T RegisterUtility(T utility) where T : IUtility; + + /// + /// 注册工具类型并可选地指定创建回调 + /// 当工具实例被创建时会调用指定的回调函数 + /// + /// 工具类型,必须是引用类型且实现IUtility接口 + /// 工具实例创建后的回调函数,可为null + void RegisterUtility(Action? onCreated = null) where T : class, IUtility; + /// /// 安装架构模块 /// diff --git a/GFramework.Core.Abstractions/ioc/IIocContainer.cs b/GFramework.Core.Abstractions/ioc/IIocContainer.cs index 06c1538..9b092c8 100644 --- a/GFramework.Core.Abstractions/ioc/IIocContainer.cs +++ b/GFramework.Core.Abstractions/ioc/IIocContainer.cs @@ -36,6 +36,13 @@ public interface IIocContainer : IContextAware /// 要注册的实例 public void RegisterPlurality(object instance); + /// + /// 注册多个实例 + /// 将实例注册到其实现所有接口 + /// + /// 要注册的实例类型 + public void RegisterPlurality() where T : class; + /// /// 注册系统实例,将其绑定到其所有实现的接口上 /// @@ -62,7 +69,7 @@ public interface IIocContainer : IContextAware /// /// 服务类型 /// 创建服务实例的工厂委托函数 - void RegisterFactory(Func factory); + void RegisterFactory(Func factory) where TService : class; #endregion diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs index d5902ca..5b418f9 100644 --- a/GFramework.Core/architecture/Architecture.cs +++ b/GFramework.Core/architecture/Architecture.cs @@ -433,6 +433,35 @@ public abstract class Architecture( return system; } + /// + /// 注册系统类型,由 DI 容器自动创建实例 + /// + /// 系统类型 + /// 可选的实例创建后回调,用于自定义配置 + public void RegisterSystem(Action? onCreated = null) where T : class, ISystem + { + ValidateRegistration("system"); + _logger.Debug($"Registering system type: {typeof(T).Name}"); + + Container.RegisterFactory(sp => + { + // 1. DI 创建实例 + var system = ActivatorUtilities.CreateInstance(sp); + + // 2. 框架默认处理 + system.SetContext(Context); + RegisterLifecycleComponent(system); + + // 3. 用户自定义处理(钩子) + onCreated?.Invoke(system); + + _logger.Debug($"System created: {typeof(T).Name}"); + return system; + }); + + _logger.Info($"System type registered: {typeof(T).Name}"); + } + /// /// 注册一个模型到架构中。 /// 若当前未初始化,则暂存至待初始化列表;否则立即初始化该模型。 @@ -456,6 +485,32 @@ public abstract class Architecture( return model; } + /// + /// 注册模型类型,由 DI 容器自动创建实例 + /// + /// 模型类型 + /// 可选的实例创建后回调,用于自定义配置 + public void RegisterModel(Action? onCreated = null) where T : class, IModel + { + ValidateRegistration("model"); + _logger.Debug($"Registering model type: {typeof(T).Name}"); + + Container.RegisterFactory(sp => + { + var model = ActivatorUtilities.CreateInstance(sp); + model.SetContext(Context); + RegisterLifecycleComponent(model); + + // 用户自定义钩子 + onCreated?.Invoke(model); + + _logger.Debug($"Model created: {typeof(T).Name}"); + return model; + }); + + _logger.Info($"Model type registered: {typeof(T).Name}"); + } + /// /// 注册一个工具到架构中 /// @@ -479,6 +534,36 @@ public abstract class Architecture( return utility; } + /// + /// 注册工具类型,由 DI 容器自动创建实例 + /// + /// 工具类型 + /// 可选的实例创建后回调,用于自定义配置 + public void RegisterUtility(Action? onCreated = null) where T : class, IUtility + { + _logger.Debug($"Registering utility type: {typeof(T).Name}"); + + Container.RegisterFactory(sp => + { + var utility = ActivatorUtilities.CreateInstance(sp); + + // 如果是 IContextUtility,设置上下文 + if (utility is IContextUtility contextUtility) + { + contextUtility.SetContext(Context); + RegisterLifecycleComponent(contextUtility); + } + + // 用户自定义钩子 + onCreated?.Invoke(utility); + + _logger.Debug($"Utility created: {typeof(T).Name}"); + return utility; + }); + + _logger.Info($"Utility type registered: {typeof(T).Name}"); + } + #endregion #region Initialization diff --git a/GFramework.Core/ioc/MicrosoftDiContainer.cs b/GFramework.Core/ioc/MicrosoftDiContainer.cs index 5ea7392..2989eb3 100644 --- a/GFramework.Core/ioc/MicrosoftDiContainer.cs +++ b/GFramework.Core/ioc/MicrosoftDiContainer.cs @@ -138,6 +138,37 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) } } + /// + /// 注册多个实例到其所有接口 + /// 实现一个实例支持多种接口类型的解析 + /// + public void RegisterPlurality() where T : class + { + _lock.EnterWriteLock(); + try + { + ThrowIfFrozen(); + + var concreteType = typeof(T); + var interfaces = concreteType.GetInterfaces(); + + // 注册具体类型 + Services.AddSingleton(); + + // 注册所有接口(指向同一个实例) + foreach (var interfaceType in interfaces) + { + Services.AddSingleton(interfaceType, sp => sp.GetRequiredService()); + } + + _logger.Debug($"Type registered: {concreteType.Name} with {interfaces.Length} interfaces"); + } + finally + { + _lock.ExitWriteLock(); + } + } + /// /// 注册系统实例 /// 通过RegisterPlurality方法注册ISystem类型实例 @@ -198,7 +229,7 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) /// /// 服务类型 /// 创建服务实例的工厂委托函数,接收IServiceProvider参数 - public void RegisterFactory(Func factory) + public void RegisterFactory(Func factory) where TService : class { ThrowIfFrozen(); Services.AddSingleton(factory);