diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs index 7344913..c4c59c7 100644 --- a/GFramework.Core/architecture/Architecture.cs +++ b/GFramework.Core/architecture/Architecture.cs @@ -15,40 +15,53 @@ namespace GFramework.Core.architecture; /// 派生类类型,用于实现单例 public abstract class Architecture : IArchitecture where T : Architecture, new() { - public ArchitecturePhase CurrentPhase { get; private set; } + #region Fields and Properties - private void EnterPhase(ArchitecturePhase next) - { - if (!ArchitectureConstants.PhaseTransitions.TryGetValue(CurrentPhase, out var allowed) || - !allowed.Contains(next)) - { - throw new InvalidOperationException( - $"Invalid phase transition: {CurrentPhase} -> {next}"); - } + /// + /// 控制反转容器,用于存储和获取各种服务(如系统、模型、工具) + /// + private readonly IocContainer _mContainer = new(); - CurrentPhase = next; - NotifyPhase(next); - foreach (var obj in _mContainer.GetAll()) - { - obj.OnArchitecturePhase(next); - } - } + /// + /// 存储尚未初始化的模型集合,在初始化阶段统一调用Init方法 + /// + private readonly HashSet _mModels = []; + /// + /// 存储尚未初始化的系统集合,在初始化阶段统一调用Init方法 + /// + private readonly HashSet _mSystems = []; + + /// + /// 存储所有已注册的系统,用于销毁 + /// + private readonly HashSet _allSystems = []; + + /// + /// 类型化事件系统,负责事件的发布与订阅管理 + /// + private readonly TypeEventSystem _mTypeEventSystem = new(); + + /// + /// 标记架构是否已初始化完成 + /// + private bool _mInited; + + /// + /// 获取架构实例的静态属性 + /// + /// 返回IArchitecture类型的架构实例 + public static IArchitecture Instance => MArchitectureLazy.Value; + + /// + /// 生命周期感知对象列表 + /// private readonly List _lifecycleHooks = []; - - public void RegisterLifecycleHook(IArchitectureLifecycle hook) - { - if (CurrentPhase >= ArchitecturePhase.Ready) - throw new InvalidOperationException( - "Cannot register lifecycle hook after architecture is Ready"); - _lifecycleHooks.Add(hook); - } - - private void NotifyPhase(ArchitecturePhase phase) - { - foreach (var hook in _lifecycleHooks) - hook.OnPhase(phase, this); - } + + /// + /// 当前架构的阶段 + /// + public ArchitecturePhase CurrentPhase { get; private set; } /// /// 静态只读字段,用于延迟初始化架构实例 @@ -100,39 +113,99 @@ public abstract class Architecture : IArchitecture where T : Architecture, return arch; }, LazyThreadSafetyMode.ExecutionAndPublication); - /// - /// 控制反转容器,用于存储和获取各种服务(如系统、模型、工具) - /// - private readonly IocContainer _mContainer = new(); + #endregion + + #region Lifecycle Management /// - /// 存储尚未初始化的模型集合,在初始化阶段统一调用Init方法 + /// 进入指定的架构阶段,并执行相应的生命周期管理操作 /// - private readonly HashSet _mModels = []; + /// 要进入的下一个架构阶段 + /// 当阶段转换不被允许时抛出异常 + private void EnterPhase(ArchitecturePhase next) + { + // 验证阶段转换是否合法 + if (!ArchitectureConstants.PhaseTransitions.TryGetValue(CurrentPhase, out var allowed) || + !allowed.Contains(next)) + { + throw new InvalidOperationException( + $"Invalid phase transition: {CurrentPhase} -> {next}"); + } + + CurrentPhase = next; + NotifyPhase(next); + + // 通知所有架构阶段感知对象阶段变更 + foreach (var obj in _mContainer.GetAll()) + { + obj.OnArchitecturePhase(next); + } + } /// - /// 存储尚未初始化的系统集合,在初始化阶段统一调用Init方法 + /// 通知所有生命周期钩子当前阶段变更 /// - private readonly HashSet _mSystems = []; - - private readonly HashSet _allSystems = []; + /// 当前架构阶段 + private void NotifyPhase(ArchitecturePhase phase) + { + foreach (var hook in _lifecycleHooks) + hook.OnPhase(phase, this); + } /// - /// 类型化事件系统,负责事件的发布与订阅管理 + /// 注册生命周期钩子 /// - private readonly TypeEventSystem _mTypeEventSystem = new(); + /// 生命周期钩子实例 + public void RegisterLifecycleHook(IArchitectureLifecycle hook) + { + if (CurrentPhase >= ArchitecturePhase.Ready) + throw new InvalidOperationException( + "Cannot register lifecycle hook after architecture is Ready"); + _lifecycleHooks.Add(hook); + } + /// - /// 标记架构是否已初始化完成 + /// 抽象初始化方法,由子类重写以进行自定义初始化操作 /// - private bool _mInited; + protected abstract void Init(); /// - /// 获取架构实例的静态属性 + /// 销毁架构,同时销毁所有已注册的系统 /// - /// 返回IArchitecture类型的架构实例 - public static IArchitecture Instance => MArchitectureLazy.Value; + public void Destroy() + { + if (CurrentPhase >= ArchitecturePhase.Destroying) + return; + EnterPhase(ArchitecturePhase.Destroying); + + foreach (var system in _allSystems) + system.Destroy(); + + _allSystems.Clear(); + + EnterPhase(ArchitecturePhase.Destroyed); + } + + #endregion + + #region Module Management + + /// + /// 安装架构模块 + /// + /// 要安装的模块 + public void InstallModule(IArchitectureModule module) + { + RegisterLifecycleHook(module); + _mContainer.RegisterPlurality(module); + module.Install(this); + } + + #endregion + + #region Component Registration /// /// 注册一个系统到架构中。 @@ -185,6 +258,10 @@ public abstract class Architecture : IArchitecture where T : Architecture, _mContainer.RegisterPlurality(utility); } + #endregion + + #region Component Retrieval + /// /// 从IOC容器中获取指定类型的系统实例 /// @@ -215,6 +292,10 @@ public abstract class Architecture : IArchitecture where T : Architecture, return _mContainer.Get(); } + #endregion + + #region Command Execution + /// /// 发送一个带返回结果的命令请求 /// @@ -236,6 +317,32 @@ public abstract class Architecture : IArchitecture where T : Architecture, ExecuteCommand(command); } + /// + /// 执行一个带返回结果的命令 + /// + /// 命令执行后的返回值类型 + /// 要执行的命令对象 + /// 命令执行的结果 + protected virtual TResult ExecuteCommand(ICommand command) + { + command.SetArchitecture(this); + return command.Execute(); + } + + /// + /// 执行一个无返回结果的命令 + /// + /// 要执行的命令对象 + protected virtual void ExecuteCommand(ICommand command) + { + command.SetArchitecture(this); + command.Execute(); + } + + #endregion + + #region Query Execution + /// /// 发起一次查询请求并获得其结果 /// @@ -247,6 +354,22 @@ public abstract class Architecture : IArchitecture where T : Architecture, return DoQuery(query); } + /// + /// 实际执行查询逻辑的方法 + /// + /// 查询结果的数据类型 + /// 要处理的查询对象 + /// 查询结果 + protected virtual TResult DoQuery(IQuery query) + { + query.SetArchitecture(this); + return query.Do(); + } + + #endregion + + #region Event Management + /// /// 发布一个默认构造的新事件对象 /// @@ -287,68 +410,5 @@ public abstract class Architecture : IArchitecture where T : Architecture, _mTypeEventSystem.UnRegister(onEvent); } - /// - /// 销毁架构,同时销毁所有已注册的系统 - /// - public void Destroy() - { - if (CurrentPhase >= ArchitecturePhase.Destroying) - return; - - EnterPhase(ArchitecturePhase.Destroying); - - foreach (var system in _allSystems) - system.Destroy(); - - _allSystems.Clear(); - - EnterPhase(ArchitecturePhase.Destroyed); - } - - - /// - /// 抽象初始化方法,由子类重写以进行自定义初始化操作 - /// - protected abstract void Init(); - - /// - /// 执行一个带返回结果的命令 - /// - /// 命令执行后的返回值类型 - /// 要执行的命令对象 - /// 命令执行的结果 - protected virtual TResult ExecuteCommand(ICommand command) - { - command.SetArchitecture(this); - return command.Execute(); - } - - /// - /// 执行一个无返回结果的命令 - /// - /// 要执行的命令对象 - protected virtual void ExecuteCommand(ICommand command) - { - command.SetArchitecture(this); - command.Execute(); - } - - /// - /// 实际执行查询逻辑的方法 - /// - /// 查询结果的数据类型 - /// 要处理的查询对象 - /// 查询结果 - protected virtual TResult DoQuery(IQuery query) - { - query.SetArchitecture(this); - return query.Do(); - } - - public void InstallModule(IArchitectureModule module) - { - RegisterLifecycleHook(module); - _mContainer.RegisterPlurality(module); - module.Install(this); - } + #endregion } \ No newline at end of file