refactor(architecture): 重构日志记录器的初始化和使用方式

- 移除 ArchitectureContext 构造函数中的 ILogger 参数
- 从 IArchitectureContext 接口中移除 Logger 属性
- 更新 AbstractContextUtility 使用 LoggerFactory 创建日志记录器
- 修改 AbstractSystem 使用 LoggerFactory 获取日志记录器
- 调整 Architecture 类中上下文创建时的日志工厂使用
- 更新 IocContainer 初始化时的日志记录器获取方式
- 移除 IIocContainer 接口中的 Init 方法定义
- [no tag]
This commit is contained in:
GeWuYou 2025-12-25 13:42:16 +08:00
parent 543e32eb6a
commit 7fa2a1e4cb
7 changed files with 98 additions and 107 deletions

View File

@ -16,7 +16,7 @@ public abstract class Architecture(
IArchitectureConfiguration? configuration = null,
IArchitectureServices? services = null,
IArchitectureContext? context = null
)
)
: IArchitecture, IArchitectureLifecycle
{
/// <summary>
@ -42,7 +42,7 @@ public abstract class Architecture(
/// 通过Services属性获取的IArchitectureServices中的Container属性
/// </value>
private IIocContainer Container => Services.Container;
/// <summary>
/// 获取类型事件系统
/// </summary>
@ -59,6 +59,37 @@ public abstract class Architecture(
/// </value>
public IArchitectureRuntime Runtime { get; private set; } = null!;
#region Module Management
/// <summary>
/// 安装架构模块
/// </summary>
/// <param name="module">要安装的模块</param>
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
/// <summary>
/// 处理架构阶段变更通知
/// </summary>
/// <param name="phase">当前架构阶段</param>
/// <param name="architecture">架构实例</param>
public virtual void OnPhase(ArchitecturePhase phase, IArchitecture architecture)
{
}
#endregion
#region Fields and Properties
/// <summary>
@ -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
/// <summary>
/// 安装架构模块
/// </summary>
/// <param name="module">要安装的模块</param>
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
/// <summary>
/// 处理架构阶段变更通知
/// </summary>
/// <param name="phase">当前架构阶段</param>
/// <param name="architecture">架构实例</param>
public virtual void OnPhase(ArchitecturePhase phase, IArchitecture architecture)
{
}
#endregion
}

View File

@ -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
/// <summary>
/// 发送一个查询请求
/// </summary>
/// <typeparam name="TResult">查询结果类型</typeparam>
/// <param name="query">要发送的查询</param>
/// <returns>查询结果</returns>
public TResult SendQuery<TResult>(IQuery<TResult> query)
{
return query == null ? throw new ArgumentNullException(nameof(query)) : Runtime.SendQuery(query);
}
#endregion
#region Component Retrieval
/// <summary>
@ -88,21 +102,6 @@ public class ArchitectureContext(
#endregion
#region Query Execution
/// <summary>
/// 发送一个查询请求
/// </summary>
/// <typeparam name="TResult">查询结果类型</typeparam>
/// <param name="query">要发送的查询</param>
/// <returns>查询结果</returns>
public TResult SendQuery<TResult>(IQuery<TResult> query)
{
return query == null ? throw new ArgumentNullException(nameof(query)) : Runtime.SendQuery(query);
}
#endregion
#region Event Management
/// <summary>

View File

@ -13,33 +13,38 @@ namespace GFramework.Core.architecture;
/// </summary>
public interface IArchitectureContext
{
/// <summary>
/// 获取日志工厂
/// </summary>
ILoggerFactory LoggerFactory { get; }
/// <summary>
/// 获取指定类型的系统实例
/// </summary>
/// <typeparam name="TSystem">系统类型必须继承自ISystem接口</typeparam>
/// <returns>系统实例如果不存在则返回null</returns>
TSystem? GetSystem<TSystem>() where TSystem : class, ISystem;
/// <summary>
/// 获取指定类型的模型实例
/// </summary>
/// <typeparam name="TModel">模型类型必须继承自IModel接口</typeparam>
/// <returns>模型实例如果不存在则返回null</returns>
TModel? GetModel<TModel>() where TModel : class, IModel;
/// <summary>
/// 获取指定类型的工具类实例
/// </summary>
/// <typeparam name="TUtility">工具类类型必须继承自IUtility接口</typeparam>
/// <returns>工具类实例如果不存在则返回null</returns>
TUtility? GetUtility<TUtility>() where TUtility : class, IUtility;
/// <summary>
/// 发送一个命令
/// </summary>
/// <param name="command">要发送的命令</param>
void SendCommand(ICommand command);
/// <summary>
/// 发送一个带返回值的命令
/// </summary>
@ -47,7 +52,7 @@ public interface IArchitectureContext
/// <param name="command">要发送的命令</param>
/// <returns>命令执行结果</returns>
TResult SendCommand<TResult>(ICommand<TResult> command);
/// <summary>
/// 发送一个查询请求
/// </summary>
@ -55,7 +60,7 @@ public interface IArchitectureContext
/// <param name="query">要发送的查询</param>
/// <returns>查询结果</returns>
TResult SendQuery<TResult>(IQuery<TResult> query);
/// <summary>
/// 发送一个事件
/// </summary>
@ -68,7 +73,7 @@ public interface IArchitectureContext
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="e">事件参数</param>
void SendEvent<TEvent>(TEvent e) where TEvent : class;
/// <summary>
/// 注册事件处理器
/// </summary>
@ -76,19 +81,11 @@ public interface IArchitectureContext
/// <param name="handler">事件处理委托</param>
/// <returns>事件注销接口</returns>
IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler);
/// <summary>
/// 获取日志记录器
/// </summary>
ILogger Logger { get; }
/// <summary>
/// 获取日志工厂
/// </summary>
ILoggerFactory LoggerFactory { get; }
/// <summary>
/// 取消注册事件监听器
/// </summary>
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="onEvent">要取消注册的事件回调方法</param>
void UnRegisterEvent<TEvent>(Action<TEvent> onEvent);
}
}

View File

@ -6,11 +6,8 @@ namespace GFramework.Core.ioc;
/// <summary>
/// 依赖注入容器接口,定义了服务注册、解析和管理的基本操作
/// </summary>
public interface IIocContainer:IContextAware{
/// <summary>
/// 初始化方法
/// </summary>
void Init();
public interface IIocContainer : IContextAware
{
#region Register Methods
/// <summary>

View File

@ -9,24 +9,6 @@ namespace GFramework.Core.ioc;
/// </summary>
public class IocContainer : ContextAwareBase, IIocContainer
{
#region Core
/// <summary>
/// 存储所有已注册对象实例的集合,用于跟踪和管理容器中的所有对象
/// 使用HashSet确保对象唯一性避免重复注册同一实例
/// </summary>
private readonly HashSet<object> _objects = [];
/// <summary>
/// 类型索引字典,用于快速查找指定类型的所有实例
/// 键为类型对象,值为该类型对应的所有实例集合
/// </summary>
private readonly Dictionary<Type, HashSet<object>> _typeIndex = new();
private ILogger _logger = null!;
#endregion
#region Lock
/// <summary>
@ -48,12 +30,30 @@ public class IocContainer : ContextAwareBase, IIocContainer
#endregion
#region Core
/// <summary>
/// 存储所有已注册对象实例的集合,用于跟踪和管理容器中的所有对象
/// 使用HashSet确保对象唯一性避免重复注册同一实例
/// </summary>
private readonly HashSet<object> _objects = [];
/// <summary>
/// 类型索引字典,用于快速查找指定类型的所有实例
/// 键为类型对象,值为该类型对应的所有实例集合
/// </summary>
private readonly Dictionary<Type, HashSet<object>> _typeIndex = new();
private ILogger _logger = null!;
#endregion
#region Register
public void Init()
{
_logger = Context.Logger;
_logger = Context.LoggerFactory.GetLogger(nameof(IocContainer));
}
/// <summary>

View File

@ -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
/// </summary>
void ISystem.Init()
{
_logger = Context.Logger;
_logger = Context.LoggerFactory.GetLogger(nameof(AbstractSystem));
_logger.Debug($"Initializing system: {GetType().Name}");
OnInit();

View File

@ -9,26 +9,26 @@ namespace GFramework.Core.utility;
/// </summary>
public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility
{
protected ILogger _logger = new NoopLoggerFactory().GetLogger(nameof(AbstractContextUtility));
protected ILogger Logger = null !;
/// <summary>
/// 初始化上下文工具类
/// </summary>
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}");
}
/// <summary>
/// 抽象初始化方法,由子类实现具体的初始化逻辑
/// </summary>
protected abstract void OnInit();
}
}