From 7fa2a1e4cbab3f94ac5fb30d94fb745b5d4899dd Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Thu, 25 Dec 2025 13:42:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(architecture):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E5=99=A8=E7=9A=84=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=92=8C=E4=BD=BF=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 ArchitectureContext 构造函数中的 ILogger 参数 - 从 IArchitectureContext 接口中移除 Logger 属性 - 更新 AbstractContextUtility 使用 LoggerFactory 创建日志记录器 - 修改 AbstractSystem 使用 LoggerFactory 获取日志记录器 - 调整 Architecture 类中上下文创建时的日志工厂使用 - 更新 IocContainer 初始化时的日志记录器获取方式 - 移除 IIocContainer 接口中的 Init 方法定义 - [no tag] --- GFramework.Core/architecture/Architecture.cs | 77 +++++++++---------- .../architecture/ArchitectureContext.cs | 35 ++++----- .../architecture/IArchitectureContext.cs | 31 ++++---- GFramework.Core/ioc/IIocContainer.cs | 7 +- GFramework.Core/ioc/IocContainer.cs | 38 ++++----- GFramework.Core/system/AbstractSystem.cs | 3 +- .../utility/AbstractContextUtility.cs | 14 ++-- 7 files changed, 98 insertions(+), 107 deletions(-) diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs index 3f38328..8d24f6d 100644 --- a/GFramework.Core/architecture/Architecture.cs +++ b/GFramework.Core/architecture/Architecture.cs @@ -16,7 +16,7 @@ public abstract class Architecture( IArchitectureConfiguration? configuration = null, IArchitectureServices? services = null, IArchitectureContext? context = null - ) +) : IArchitecture, IArchitectureLifecycle { /// @@ -42,7 +42,7 @@ public abstract class Architecture( /// 通过Services属性获取的IArchitectureServices中的Container属性 /// private IIocContainer Container => Services.Container; - + /// /// 获取类型事件系统 /// @@ -59,6 +59,37 @@ public abstract class Architecture( /// public IArchitectureRuntime Runtime { get; private set; } = null!; + #region Module Management + + /// + /// 安装架构模块 + /// + /// 要安装的模块 + public void InstallModule(IArchitectureModule module) + { + var logger = Configuration.LoggerFactory.GetLogger(nameof(Architecture)); + logger.Debug($"Installing module: {module.GetType().Name}"); + RegisterLifecycleHook(module); + Container.RegisterPlurality(module); + module.Install(this); + logger.Info($"Module installed: {module.GetType().Name}"); + } + + #endregion + + #region IArchitectureLifecycle Implementation + + /// + /// 处理架构阶段变更通知 + /// + /// 当前架构阶段 + /// 架构实例 + public virtual void OnPhase(ArchitecturePhase phase, IArchitecture architecture) + { + } + + #endregion + #region Fields and Properties /// @@ -97,7 +128,7 @@ public abstract class Architecture( private ILogger _logger = null!; private IArchitectureContext? _context = context; - + public IArchitectureContext Context => _context!; #endregion @@ -209,31 +240,13 @@ public abstract class Architecture( #endregion - #region Module Management - - /// - /// 安装架构模块 - /// - /// 要安装的模块 - public void InstallModule(IArchitectureModule module) - { - var logger = Configuration.LoggerFactory.GetLogger(nameof(Architecture)); - logger.Debug($"Installing module: {module.GetType().Name}"); - RegisterLifecycleHook(module); - Container.RegisterPlurality(module); - module.Install(this); - logger.Info($"Module installed: {module.GetType().Name}"); - } - - #endregion - #region Component Registration public void Initialize() { _logger = Configuration.LoggerFactory.GetLogger(GetType().Name); - _context ??= new ArchitectureContext(Container, TypeEventSystem, _logger, Configuration.LoggerFactory); - + _context ??= new ArchitectureContext(Container, TypeEventSystem, Configuration.LoggerFactory); + // 创建架构运行时实例 Runtime = new ArchitectureRuntime(_context); ((ArchitectureContext)_context).Runtime = Runtime; @@ -283,8 +296,8 @@ public abstract class Architecture( public async Task InitializeAsync() { _logger = Configuration.LoggerFactory.GetLogger(GetType().Name); - _context ??= new ArchitectureContext(Container, TypeEventSystem, _logger, Configuration.LoggerFactory); - + _context ??= new ArchitectureContext(Container, TypeEventSystem, Configuration.LoggerFactory); + // 创建架构运行时实例 Runtime = new ArchitectureRuntime(_context); ((ArchitectureContext)_context).Runtime = Runtime; @@ -411,18 +424,4 @@ public abstract class Architecture( } #endregion - - #region IArchitectureLifecycle Implementation - - /// - /// 处理架构阶段变更通知 - /// - /// 当前架构阶段 - /// 架构实例 - public virtual void OnPhase(ArchitecturePhase phase, IArchitecture architecture) - { - - } - - #endregion } \ No newline at end of file diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs index 9450a70..e907ca6 100644 --- a/GFramework.Core/architecture/ArchitectureContext.cs +++ b/GFramework.Core/architecture/ArchitectureContext.cs @@ -15,7 +15,6 @@ namespace GFramework.Core.architecture; public class ArchitectureContext( IIocContainer container, ITypeEventSystem typeEventSystem, - ILogger logger, ILoggerFactory? loggerFactory) : IArchitectureContext { @@ -24,10 +23,25 @@ public class ArchitectureContext( private readonly ITypeEventSystem _typeEventSystem = typeEventSystem ?? throw new ArgumentNullException(nameof(typeEventSystem)); - public ILogger Logger { get; } = logger ?? throw new ArgumentNullException(nameof(logger)); - public ILoggerFactory LoggerFactory { get; } = loggerFactory ?? new NoopLoggerFactory(); internal IArchitectureRuntime Runtime { get; set; } = null!; + public ILoggerFactory LoggerFactory { get; } = loggerFactory ?? new NoopLoggerFactory(); + + #region Query Execution + + /// + /// 发送一个查询请求 + /// + /// 查询结果类型 + /// 要发送的查询 + /// 查询结果 + public TResult SendQuery(IQuery query) + { + return query == null ? throw new ArgumentNullException(nameof(query)) : Runtime.SendQuery(query); + } + + #endregion + #region Component Retrieval /// @@ -88,21 +102,6 @@ public class ArchitectureContext( #endregion - #region Query Execution - - /// - /// 发送一个查询请求 - /// - /// 查询结果类型 - /// 要发送的查询 - /// 查询结果 - public TResult SendQuery(IQuery query) - { - return query == null ? throw new ArgumentNullException(nameof(query)) : Runtime.SendQuery(query); - } - - #endregion - #region Event Management /// diff --git a/GFramework.Core/architecture/IArchitectureContext.cs b/GFramework.Core/architecture/IArchitectureContext.cs index a9b5485..2a2fa88 100644 --- a/GFramework.Core/architecture/IArchitectureContext.cs +++ b/GFramework.Core/architecture/IArchitectureContext.cs @@ -13,33 +13,38 @@ namespace GFramework.Core.architecture; /// public interface IArchitectureContext { + /// + /// 获取日志工厂 + /// + ILoggerFactory LoggerFactory { get; } + /// /// 获取指定类型的系统实例 /// /// 系统类型,必须继承自ISystem接口 /// 系统实例,如果不存在则返回null TSystem? GetSystem() where TSystem : class, ISystem; - + /// /// 获取指定类型的模型实例 /// /// 模型类型,必须继承自IModel接口 /// 模型实例,如果不存在则返回null TModel? GetModel() where TModel : class, IModel; - + /// /// 获取指定类型的工具类实例 /// /// 工具类类型,必须继承自IUtility接口 /// 工具类实例,如果不存在则返回null TUtility? GetUtility() where TUtility : class, IUtility; - + /// /// 发送一个命令 /// /// 要发送的命令 void SendCommand(ICommand command); - + /// /// 发送一个带返回值的命令 /// @@ -47,7 +52,7 @@ public interface IArchitectureContext /// 要发送的命令 /// 命令执行结果 TResult SendCommand(ICommand command); - + /// /// 发送一个查询请求 /// @@ -55,7 +60,7 @@ public interface IArchitectureContext /// 要发送的查询 /// 查询结果 TResult SendQuery(IQuery query); - + /// /// 发送一个事件 /// @@ -68,7 +73,7 @@ public interface IArchitectureContext /// 事件类型 /// 事件参数 void SendEvent(TEvent e) where TEvent : class; - + /// /// 注册事件处理器 /// @@ -76,19 +81,11 @@ public interface IArchitectureContext /// 事件处理委托 /// 事件注销接口 IUnRegister RegisterEvent(Action handler); - - /// - /// 获取日志记录器 - /// - ILogger Logger { get; } - /// - /// 获取日志工厂 - /// - ILoggerFactory LoggerFactory { get; } + /// /// 取消注册事件监听器 /// /// 事件类型 /// 要取消注册的事件回调方法 void UnRegisterEvent(Action onEvent); -} +} \ No newline at end of file diff --git a/GFramework.Core/ioc/IIocContainer.cs b/GFramework.Core/ioc/IIocContainer.cs index 9eb3b13..ddc9207 100644 --- a/GFramework.Core/ioc/IIocContainer.cs +++ b/GFramework.Core/ioc/IIocContainer.cs @@ -6,11 +6,8 @@ namespace GFramework.Core.ioc; /// /// 依赖注入容器接口,定义了服务注册、解析和管理的基本操作 /// -public interface IIocContainer:IContextAware{ - /// - /// 初始化方法 - /// - void Init(); +public interface IIocContainer : IContextAware +{ #region Register Methods /// diff --git a/GFramework.Core/ioc/IocContainer.cs b/GFramework.Core/ioc/IocContainer.cs index 524b7ac..4091954 100644 --- a/GFramework.Core/ioc/IocContainer.cs +++ b/GFramework.Core/ioc/IocContainer.cs @@ -9,24 +9,6 @@ namespace GFramework.Core.ioc; /// public class IocContainer : ContextAwareBase, IIocContainer { - #region Core - - /// - /// 存储所有已注册对象实例的集合,用于跟踪和管理容器中的所有对象 - /// 使用HashSet确保对象唯一性,避免重复注册同一实例 - /// - private readonly HashSet _objects = []; - - /// - /// 类型索引字典,用于快速查找指定类型的所有实例 - /// 键为类型对象,值为该类型对应的所有实例集合 - /// - private readonly Dictionary> _typeIndex = new(); - - private ILogger _logger = null!; - - #endregion - #region Lock /// @@ -48,12 +30,30 @@ public class IocContainer : ContextAwareBase, IIocContainer #endregion + #region Core + + /// + /// 存储所有已注册对象实例的集合,用于跟踪和管理容器中的所有对象 + /// 使用HashSet确保对象唯一性,避免重复注册同一实例 + /// + private readonly HashSet _objects = []; + + /// + /// 类型索引字典,用于快速查找指定类型的所有实例 + /// 键为类型对象,值为该类型对应的所有实例集合 + /// + private readonly Dictionary> _typeIndex = new(); + + private ILogger _logger = null!; + + #endregion + #region Register public void Init() { - _logger = Context.Logger; + _logger = Context.LoggerFactory.GetLogger(nameof(IocContainer)); } /// diff --git a/GFramework.Core/system/AbstractSystem.cs b/GFramework.Core/system/AbstractSystem.cs index 2fef273..d066820 100644 --- a/GFramework.Core/system/AbstractSystem.cs +++ b/GFramework.Core/system/AbstractSystem.cs @@ -1,4 +1,3 @@ -using GFramework.Core.architecture; using GFramework.Core.logging; using GFramework.Core.rule; @@ -17,7 +16,7 @@ public abstract class AbstractSystem : ContextAwareBase, ISystem /// void ISystem.Init() { - _logger = Context.Logger; + _logger = Context.LoggerFactory.GetLogger(nameof(AbstractSystem)); _logger.Debug($"Initializing system: {GetType().Name}"); OnInit(); diff --git a/GFramework.Core/utility/AbstractContextUtility.cs b/GFramework.Core/utility/AbstractContextUtility.cs index 361f617..5033fa5 100644 --- a/GFramework.Core/utility/AbstractContextUtility.cs +++ b/GFramework.Core/utility/AbstractContextUtility.cs @@ -9,26 +9,26 @@ namespace GFramework.Core.utility; /// public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility { - protected ILogger _logger = new NoopLoggerFactory().GetLogger(nameof(AbstractContextUtility)); - + protected ILogger Logger = null !; + /// /// 初始化上下文工具类 /// void IContextUtility.Init() { // 获取上下文中的日志记录器 - _logger = Context.Logger; - _logger.Debug($"Initializing Context Utility: {GetType().Name}"); + Logger = Context.LoggerFactory.GetLogger(nameof(AbstractContextUtility)); + Logger.Debug($"Initializing Context Utility: {GetType().Name}"); // 执行子类实现的初始化逻辑 OnInit(); // 记录初始化完成信息 - _logger.Info($"Context Utility initialized: {GetType().Name}"); + Logger.Info($"Context Utility initialized: {GetType().Name}"); } - + /// /// 抽象初始化方法,由子类实现具体的初始化逻辑 /// protected abstract void OnInit(); -} +} \ No newline at end of file