mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 04:59:01 +08:00
feat(ioc): 添加类型注册工厂和多接口注册功能
- 为 Architecture 添加 RegisterSystem<T>、RegisterModel<T> 和 RegisterUtility<T> 泛型方法 - 支持通过 DI 容器自动创建实例并提供创建后回调钩子 - 在 MicrosoftDiContainer 中实现 RegisterPlurality<T> 方法 - 支持将单个实例注册到其实现的所有接口 - 更新 IIocContainer 接口定义以匹配新功能 - 为 RegisterFactory 方法添加 class 约束确保类型安全
This commit is contained in:
parent
55ec42a670
commit
853bcf4315
@ -39,6 +39,13 @@ public interface IArchitecture : IAsyncInitializable
|
|||||||
/// <returns>注册的系统实例</returns>
|
/// <returns>注册的系统实例</returns>
|
||||||
T RegisterSystem<T>(T system) where T : ISystem;
|
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>
|
||||||
/// 注册模型实例到架构中
|
/// 注册模型实例到架构中
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -47,6 +54,13 @@ public interface IArchitecture : IAsyncInitializable
|
|||||||
/// <returns>注册的模型实例</returns>
|
/// <returns>注册的模型实例</returns>
|
||||||
T RegisterModel<T>(T model) where T : IModel;
|
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>
|
||||||
/// 注册工具实例到架构中
|
/// 注册工具实例到架构中
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -55,6 +69,15 @@ public interface IArchitecture : IAsyncInitializable
|
|||||||
/// <returns>注册的工具实例</returns>
|
/// <returns>注册的工具实例</returns>
|
||||||
T RegisterUtility<T>(T utility) where T : IUtility;
|
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>
|
/// <summary>
|
||||||
/// 安装架构模块
|
/// 安装架构模块
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -36,6 +36,13 @@ public interface IIocContainer : IContextAware
|
|||||||
/// <param name="instance">要注册的实例</param>
|
/// <param name="instance">要注册的实例</param>
|
||||||
public void RegisterPlurality(object instance);
|
public void RegisterPlurality(object instance);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注册多个实例
|
||||||
|
/// 将实例注册到其实现所有接口
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">要注册的实例类型</typeparam>
|
||||||
|
public void RegisterPlurality<T>() where T : class;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册系统实例,将其绑定到其所有实现的接口上
|
/// 注册系统实例,将其绑定到其所有实现的接口上
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -62,7 +69,7 @@ public interface IIocContainer : IContextAware
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TService">服务类型</typeparam>
|
/// <typeparam name="TService">服务类型</typeparam>
|
||||||
/// <param name="factory">创建服务实例的工厂委托函数</param>
|
/// <param name="factory">创建服务实例的工厂委托函数</param>
|
||||||
void RegisterFactory<TService>(Func<IServiceProvider, TService> factory);
|
void RegisterFactory<TService>(Func<IServiceProvider, TService> factory) where TService : class;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@ -433,6 +433,35 @@ public abstract class Architecture(
|
|||||||
return system;
|
return system;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注册系统类型,由 DI 容器自动创建实例
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">系统类型</typeparam>
|
||||||
|
/// <param name="onCreated">可选的实例创建后回调,用于自定义配置</param>
|
||||||
|
public void RegisterSystem<T>(Action<T>? onCreated = null) where T : class, ISystem
|
||||||
|
{
|
||||||
|
ValidateRegistration("system");
|
||||||
|
_logger.Debug($"Registering system type: {typeof(T).Name}");
|
||||||
|
|
||||||
|
Container.RegisterFactory<T>(sp =>
|
||||||
|
{
|
||||||
|
// 1. DI 创建实例
|
||||||
|
var system = ActivatorUtilities.CreateInstance<T>(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}");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册一个模型到架构中。
|
/// 注册一个模型到架构中。
|
||||||
/// 若当前未初始化,则暂存至待初始化列表;否则立即初始化该模型。
|
/// 若当前未初始化,则暂存至待初始化列表;否则立即初始化该模型。
|
||||||
@ -456,6 +485,32 @@ public abstract class Architecture(
|
|||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注册模型类型,由 DI 容器自动创建实例
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">模型类型</typeparam>
|
||||||
|
/// <param name="onCreated">可选的实例创建后回调,用于自定义配置</param>
|
||||||
|
public void RegisterModel<T>(Action<T>? onCreated = null) where T : class, IModel
|
||||||
|
{
|
||||||
|
ValidateRegistration("model");
|
||||||
|
_logger.Debug($"Registering model type: {typeof(T).Name}");
|
||||||
|
|
||||||
|
Container.RegisterFactory<T>(sp =>
|
||||||
|
{
|
||||||
|
var model = ActivatorUtilities.CreateInstance<T>(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}");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册一个工具到架构中
|
/// 注册一个工具到架构中
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -479,6 +534,36 @@ public abstract class Architecture(
|
|||||||
return utility;
|
return utility;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注册工具类型,由 DI 容器自动创建实例
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">工具类型</typeparam>
|
||||||
|
/// <param name="onCreated">可选的实例创建后回调,用于自定义配置</param>
|
||||||
|
public void RegisterUtility<T>(Action<T>? onCreated = null) where T : class, IUtility
|
||||||
|
{
|
||||||
|
_logger.Debug($"Registering utility type: {typeof(T).Name}");
|
||||||
|
|
||||||
|
Container.RegisterFactory<T>(sp =>
|
||||||
|
{
|
||||||
|
var utility = ActivatorUtilities.CreateInstance<T>(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
|
#endregion
|
||||||
|
|
||||||
#region Initialization
|
#region Initialization
|
||||||
|
|||||||
@ -138,6 +138,37 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注册多个实例到其所有接口
|
||||||
|
/// 实现一个实例支持多种接口类型的解析
|
||||||
|
/// </summary>
|
||||||
|
public void RegisterPlurality<T>() where T : class
|
||||||
|
{
|
||||||
|
_lock.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ThrowIfFrozen();
|
||||||
|
|
||||||
|
var concreteType = typeof(T);
|
||||||
|
var interfaces = concreteType.GetInterfaces();
|
||||||
|
|
||||||
|
// 注册具体类型
|
||||||
|
Services.AddSingleton<T>();
|
||||||
|
|
||||||
|
// 注册所有接口(指向同一个实例)
|
||||||
|
foreach (var interfaceType in interfaces)
|
||||||
|
{
|
||||||
|
Services.AddSingleton(interfaceType, sp => sp.GetRequiredService<T>());
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.Debug($"Type registered: {concreteType.Name} with {interfaces.Length} interfaces");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_lock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册系统实例
|
/// 注册系统实例
|
||||||
/// 通过RegisterPlurality方法注册ISystem类型实例
|
/// 通过RegisterPlurality方法注册ISystem类型实例
|
||||||
@ -198,7 +229,7 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TService">服务类型</typeparam>
|
/// <typeparam name="TService">服务类型</typeparam>
|
||||||
/// <param name="factory">创建服务实例的工厂委托函数,接收IServiceProvider参数</param>
|
/// <param name="factory">创建服务实例的工厂委托函数,接收IServiceProvider参数</param>
|
||||||
public void RegisterFactory<TService>(Func<IServiceProvider, TService> factory)
|
public void RegisterFactory<TService>(Func<IServiceProvider, TService> factory) where TService : class
|
||||||
{
|
{
|
||||||
ThrowIfFrozen();
|
ThrowIfFrozen();
|
||||||
Services.AddSingleton(factory);
|
Services.AddSingleton(factory);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user