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