From ec07f6b2ef27a090a04da66f40bad1f81a037797 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 14 Feb 2026 11:04:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ioc):=20=E9=87=8D=E6=9E=84MicrosoftDiC?= =?UTF-8?q?ontainer=E4=BB=A5=E6=94=AF=E6=8C=81=E6=9C=AA=E5=86=BB=E7=BB=93?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=B8=8B=E7=9A=84=E6=9C=8D=E5=8A=A1=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除OnContextReady方法,将日志记录器初始化改为直接赋值 - 将日志记录器改为readonly字段并直接初始化 - 修改Get()方法以支持未冻结状态下的实例获取逻辑 - 修改Get(Type)方法以支持未冻结状态下的类型实例获取 - 更新GetAll()方法以支持未冻结状态下的批量服务获取 - 更新GetAll(Type)方法以支持未冻结状态下的批量类型服务获取 - 移除EnsureProvider私有方法,内联检查逻辑到各个获取方法中 --- GFramework.Core/ioc/MicrosoftDiContainer.cs | 136 +++++++++++++------- 1 file changed, 90 insertions(+), 46 deletions(-) diff --git a/GFramework.Core/ioc/MicrosoftDiContainer.cs b/GFramework.Core/ioc/MicrosoftDiContainer.cs index 2989eb3..b0e1ab2 100644 --- a/GFramework.Core/ioc/MicrosoftDiContainer.cs +++ b/GFramework.Core/ioc/MicrosoftDiContainer.cs @@ -15,15 +15,21 @@ namespace GFramework.Core.ioc; /// 可选的IServiceCollection实例,默认创建新的ServiceCollection public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) : ContextAwareBase, IIocContainer { - #region Context Ready + #region Helper Methods /// - /// 上下文准备就绪时的回调方法 - /// 初始化日志记录器实例 + /// 检查容器是否已冻结,如果已冻结则抛出异常 + /// 用于保护注册操作的安全性 /// - protected override void OnContextReady() + /// 当容器已冻结时抛出 + private void ThrowIfFrozen() { - _logger = LoggerFactoryResolver.Provider.CreateLogger(nameof(MicrosoftDiContainer)); + if (_frozen) + { + const string errorMsg = "MicrosoftDiContainer is frozen"; + _logger.Error(errorMsg); + throw new InvalidOperationException(errorMsg); + } } #endregion @@ -53,7 +59,7 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) /// /// 日志记录器,用于记录容器操作日志 /// - private ILogger _logger = null!; + private readonly ILogger _logger = LoggerFactoryResolver.Provider.CreateLogger(nameof(MicrosoftDiContainer)); #endregion @@ -245,10 +251,24 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) /// /// 服务类型 /// 服务实例或null - /// 当容器未冻结时抛出 public T? Get() where T : class { - EnsureProvider(); + if (_provider == null) + { + // 如果容器未冻结,从服务集合中查找已注册的实例 + var serviceType = typeof(T); + var descriptor = Services.FirstOrDefault(s => + s.ServiceType == serviceType || serviceType.IsAssignableFrom(s.ServiceType)); + + if (descriptor?.ImplementationInstance is T instance) + { + return instance; + } + + // 在未冻结状态下无法调用工厂方法或创建实例,返回null + return null; + } + _lock.EnterReadLock(); try { @@ -270,10 +290,17 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) /// /// 服务类型 /// 服务实例或null - /// 当容器未冻结时抛出 public object? Get(Type type) { - EnsureProvider(); + if (_provider == null) + { + // 如果容器未冻结,从服务集合中查找已注册的实例 + var descriptor = + Services.FirstOrDefault(s => s.ServiceType == type || type.IsAssignableFrom(s.ServiceType)); + + return descriptor?.ImplementationInstance; + } + _lock.EnterReadLock(); try { @@ -352,10 +379,35 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) /// /// 服务类型 /// 只读的服务实例列表 - /// 当容器未冻结时抛出 public IReadOnlyList GetAll() where T : class { - EnsureProvider(); + if (_provider == null) + { + // 如果容器未冻结,从服务集合中获取已注册的实例 + var serviceType = typeof(T); + var registeredServices = Services + .Where(s => s.ServiceType == serviceType || serviceType.IsAssignableFrom(s.ServiceType)).ToList(); + + var result = new List(); + foreach (var descriptor in registeredServices) + { + if (descriptor.ImplementationInstance is T instance) + { + result.Add(instance); + } + else if (descriptor.ImplementationFactory != null) + { + // 在未冻结状态下无法调用工厂方法,跳过 + } + else if (descriptor.ImplementationType != null) + { + // 在未冻结状态下无法创建实例,跳过 + } + } + + return result; + } + _lock.EnterReadLock(); try { @@ -377,7 +429,32 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) /// 当容器未冻结时抛出 public IReadOnlyList GetAll(Type type) { - EnsureProvider(); + if (_provider == null) + { + // 如果容器未冻结,从服务集合中获取已注册的实例 + var registeredServices = Services.Where(s => s.ServiceType == type || type.IsAssignableFrom(s.ServiceType)) + .ToList(); + + var result = new List(); + foreach (var descriptor in registeredServices) + { + if (descriptor.ImplementationInstance != null) + { + result.Add(descriptor.ImplementationInstance); + } + else if (descriptor.ImplementationFactory != null) + { + // 在未冻结状态下无法调用工厂方法,跳过 + } + else if (descriptor.ImplementationType != null) + { + // 在未冻结状态下无法创建实例,跳过 + } + } + + return result; + } + _lock.EnterReadLock(); try { @@ -511,37 +588,4 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) public IServiceCollection Services { get; } = serviceCollection ?? new ServiceCollection(); #endregion - - #region Helper Methods - - /// - /// 检查容器是否已冻结,如果已冻结则抛出异常 - /// 用于保护注册操作的安全性 - /// - /// 当容器已冻结时抛出 - private void ThrowIfFrozen() - { - if (_frozen) - { - const string errorMsg = "MicrosoftDiContainer is frozen"; - _logger.Error(errorMsg); - throw new InvalidOperationException(errorMsg); - } - } - - /// - /// 确保ServiceProvider已构建,如果未构建则抛出异常 - /// 用于保护获取服务操作的安全性 - /// - /// 当ServiceProvider未构建时抛出 - private void EnsureProvider() - { - if (_provider == null) - { - throw new InvalidOperationException( - "Container has not been frozen yet. Call Freeze() before retrieving services."); - } - } - - #endregion } \ No newline at end of file