diff --git a/GFramework.Core.Tests/Architectures/TestArchitectureBase.cs b/GFramework.Core.Tests/Architectures/TestArchitectureBase.cs
index c3d5e76..90f3d43 100644
--- a/GFramework.Core.Tests/Architectures/TestArchitectureBase.cs
+++ b/GFramework.Core.Tests/Architectures/TestArchitectureBase.cs
@@ -41,16 +41,8 @@ public abstract class TestArchitectureBase : Architecture
{
InitCalled = true;
_postRegistrationHook?.Invoke(this);
- }
- ///
- /// 进入指定架构阶段时的处理方法,记录阶段历史
- ///
- /// 要进入的下一个架构阶段
- protected override void EnterPhase(ArchitecturePhase next)
- {
- base.EnterPhase(next);
- // 记录进入的架构阶段到历史列表中
- PhaseHistory.Add(next);
+ // 订阅阶段变更事件以记录历史
+ PhaseChanged += phase => PhaseHistory.Add(phase);
}
}
\ No newline at end of file
diff --git a/GFramework.Core/Architectures/Architecture.cs b/GFramework.Core/Architectures/Architecture.cs
index 3bf2a98..c3e4743 100644
--- a/GFramework.Core/Architectures/Architecture.cs
+++ b/GFramework.Core/Architectures/Architecture.cs
@@ -1,14 +1,11 @@
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Abstractions.Enums;
using GFramework.Core.Abstractions.Environment;
-using GFramework.Core.Abstractions.Ioc;
-using GFramework.Core.Abstractions.Lifecycle;
using GFramework.Core.Abstractions.Logging;
using GFramework.Core.Abstractions.Model;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
using GFramework.Core.Environment;
-using GFramework.Core.Extensions;
using GFramework.Core.Logging;
using Microsoft.Extensions.DependencyInjection;
@@ -17,41 +14,56 @@ namespace GFramework.Core.Architectures;
///
/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。
/// 专注于生命周期管理、初始化流程控制和架构阶段转换。
+///
+/// 重构说明:此类已重构为协调器模式,将职责委托给专门的管理器:
+/// - ArchitectureLifecycle: 生命周期管理
+/// - ArchitectureComponentRegistry: 组件注册管理
+/// - ArchitectureModules: 模块管理
///
-public abstract class Architecture(
- IArchitectureConfiguration? configuration = null,
- IEnvironment? environment = null,
- IArchitectureServices? services = null,
- IArchitectureContext? context = null
-)
- : IArchitecture
+public abstract class Architecture : IArchitecture
{
- #region Module Management
+ #region Constructor
///
- /// 注册中介行为管道
- /// 用于配置Mediator框架的行为拦截和处理逻辑
+ /// 构造函数,初始化架构和管理器
///
- /// 行为类型,必须是引用类型
- public void RegisterMediatorBehavior() where TBehavior : class
+ /// 架构配置
+ /// 环境配置
+ /// 服务管理器
+ /// 架构上下文
+ protected Architecture(
+ IArchitectureConfiguration? configuration = null,
+ IEnvironment? environment = null,
+ IArchitectureServices? services = null,
+ IArchitectureContext? context = null)
{
- _logger.Debug($"Registering mediator behavior: {typeof(TBehavior).Name}");
- Container.RegisterMediatorBehavior();
+ Configuration = configuration ?? new ArchitectureConfiguration();
+ Environment = environment ?? new DefaultEnvironment();
+ Services = services ?? new ArchitectureServices();
+ _context = context;
+
+ // 初始化 Logger
+ LoggerFactoryResolver.Provider = Configuration.LoggerProperties.LoggerFactoryProvider;
+ _logger = LoggerFactoryResolver.Provider.CreateLogger(GetType().Name);
+
+ // 初始化管理器
+ _lifecycle = new ArchitectureLifecycle(this, Configuration, Services, _logger);
+ _componentRegistry = new ArchitectureComponentRegistry(this, Configuration, Services, _lifecycle, _logger);
+ _modules = new ArchitectureModules(this, Services, _logger);
}
+ #endregion
+
+ #region Lifecycle Hook Management
+
///
- /// 安装架构模块
+ /// 注册生命周期钩子
///
- /// 要安装的模块
- /// 安装的模块实例
- public IArchitectureModule InstallModule(IArchitectureModule module)
+ /// 生命周期钩子实例
+ /// 注册的钩子实例
+ public IArchitectureLifecycleHook RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
- var name = module.GetType().Name;
- var logger = LoggerFactoryResolver.Provider.CreateLogger(name);
- logger.Debug($"Installing module: {name}");
- module.Install(this);
- logger.Info($"Module installed: {name}");
- return module;
+ return _lifecycle.RegisterLifecycleHook(hook);
}
#endregion
@@ -61,361 +73,99 @@ public abstract class Architecture(
///
/// 获取架构配置对象
///
- private IArchitectureConfiguration Configuration { get; } = configuration ?? new ArchitectureConfiguration();
+ private IArchitectureConfiguration Configuration { get; }
///
/// 获取环境配置对象
///
- private IEnvironment Environment { get; } = environment ?? new DefaultEnvironment();
-
- private IArchitectureServices Services { get; } = services ?? new ArchitectureServices();
+ private IEnvironment Environment { get; }
///
- /// 获取依赖注入容器
+ /// 获取服务管理器
///
- private IIocContainer Container => Services.Container;
+ private IArchitectureServices Services { get; }
///
/// 当前架构的阶段
///
- public ArchitecturePhase CurrentPhase { get; private set; }
+ public ArchitecturePhase CurrentPhase => _lifecycle.CurrentPhase;
///
/// 架构上下文
///
public IArchitectureContext Context => _context!;
+ ///
+ /// 获取一个布尔值,指示当前架构是否处于就绪状态
+ ///
+ public bool IsReady => _lifecycle.IsReady;
+
+ ///
+ /// 获取用于配置服务集合的委托
+ /// 默认实现返回null,子类可以重写此属性以提供自定义配置逻辑
+ ///
+ public virtual Action? Configurator => null;
+
+ ///
+ /// 阶段变更事件(用于测试和扩展)
+ ///
+ public event Action? PhaseChanged
+ {
+ add => _lifecycle.PhaseChanged += value;
+ remove => _lifecycle.PhaseChanged -= value;
+ }
+
#endregion
#region Fields
- private readonly TaskCompletionSource _readyTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
-
- ///
- /// 获取一个布尔值,指示当前架构是否处于就绪状态。
- /// 当前架构的阶段等于 ArchitecturePhase.Ready 时返回 true,否则返回 false。
- ///
- public bool IsReady => CurrentPhase == ArchitecturePhase.Ready;
-
- ///
- /// 待初始化组件的去重集合。
- /// 用于存储需要初始化的组件实例,确保每个组件仅被初始化一次。
- ///
- private readonly HashSet _pendingInitializableSet = [];
-
- ///
- /// 存储所有待初始化的组件(统一管理,保持注册顺序)
- ///
- private readonly List _pendingInitializableList = [];
-
- ///
- /// 可销毁组件的去重集合(支持 IDestroyable 和 IAsyncDestroyable)
- ///
- private readonly HashSet