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);