mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 04:59:01 +08:00
refactor(architecture): 重构日志记录器的初始化和使用方式
- 移除 ArchitectureContext 构造函数中的 ILogger 参数 - 从 IArchitectureContext 接口中移除 Logger 属性 - 更新 AbstractContextUtility 使用 LoggerFactory 创建日志记录器 - 修改 AbstractSystem 使用 LoggerFactory 获取日志记录器 - 调整 Architecture 类中上下文创建时的日志工厂使用 - 更新 IocContainer 初始化时的日志记录器获取方式 - 移除 IIocContainer 接口中的 Init 方法定义 - [no tag]
This commit is contained in:
parent
543e32eb6a
commit
7fa2a1e4cb
@ -59,6 +59,37 @@ public abstract class Architecture(
|
|||||||
/// </value>
|
/// </value>
|
||||||
public IArchitectureRuntime Runtime { get; private set; } = null!;
|
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
|
#region Fields and Properties
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -209,30 +240,12 @@ public abstract class Architecture(
|
|||||||
|
|
||||||
#endregion
|
#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
|
#region Component Registration
|
||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
_logger = Configuration.LoggerFactory.GetLogger(GetType().Name);
|
_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);
|
Runtime = new ArchitectureRuntime(_context);
|
||||||
@ -283,7 +296,7 @@ public abstract class Architecture(
|
|||||||
public async Task InitializeAsync()
|
public async Task InitializeAsync()
|
||||||
{
|
{
|
||||||
_logger = Configuration.LoggerFactory.GetLogger(GetType().Name);
|
_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);
|
Runtime = new ArchitectureRuntime(_context);
|
||||||
@ -411,18 +424,4 @@ public abstract class Architecture(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region IArchitectureLifecycle Implementation
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 处理架构阶段变更通知
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="phase">当前架构阶段</param>
|
|
||||||
/// <param name="architecture">架构实例</param>
|
|
||||||
public virtual void OnPhase(ArchitecturePhase phase, IArchitecture architecture)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
@ -15,7 +15,6 @@ namespace GFramework.Core.architecture;
|
|||||||
public class ArchitectureContext(
|
public class ArchitectureContext(
|
||||||
IIocContainer container,
|
IIocContainer container,
|
||||||
ITypeEventSystem typeEventSystem,
|
ITypeEventSystem typeEventSystem,
|
||||||
ILogger logger,
|
|
||||||
ILoggerFactory? loggerFactory)
|
ILoggerFactory? loggerFactory)
|
||||||
: IArchitectureContext
|
: IArchitectureContext
|
||||||
{
|
{
|
||||||
@ -24,10 +23,25 @@ public class ArchitectureContext(
|
|||||||
private readonly ITypeEventSystem _typeEventSystem =
|
private readonly ITypeEventSystem _typeEventSystem =
|
||||||
typeEventSystem ?? throw new ArgumentNullException(nameof(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!;
|
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
|
#region Component Retrieval
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -88,21 +102,6 @@ public class ArchitectureContext(
|
|||||||
|
|
||||||
#endregion
|
#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
|
#region Event Management
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -13,6 +13,11 @@ namespace GFramework.Core.architecture;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IArchitectureContext
|
public interface IArchitectureContext
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取日志工厂
|
||||||
|
/// </summary>
|
||||||
|
ILoggerFactory LoggerFactory { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的系统实例
|
/// 获取指定类型的系统实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -77,14 +82,6 @@ public interface IArchitectureContext
|
|||||||
/// <returns>事件注销接口</returns>
|
/// <returns>事件注销接口</returns>
|
||||||
IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler);
|
IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取日志记录器
|
|
||||||
/// </summary>
|
|
||||||
ILogger Logger { get; }
|
|
||||||
/// <summary>
|
|
||||||
/// 获取日志工厂
|
|
||||||
/// </summary>
|
|
||||||
ILoggerFactory LoggerFactory { get; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 取消注册事件监听器
|
/// 取消注册事件监听器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -6,11 +6,8 @@ namespace GFramework.Core.ioc;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 依赖注入容器接口,定义了服务注册、解析和管理的基本操作
|
/// 依赖注入容器接口,定义了服务注册、解析和管理的基本操作
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IIocContainer:IContextAware{
|
public interface IIocContainer : IContextAware
|
||||||
/// <summary>
|
{
|
||||||
/// 初始化方法
|
|
||||||
/// </summary>
|
|
||||||
void Init();
|
|
||||||
#region Register Methods
|
#region Register Methods
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -9,24 +9,6 @@ namespace GFramework.Core.ioc;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class IocContainer : ContextAwareBase, IIocContainer
|
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
|
#region Lock
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -48,12 +30,30 @@ public class IocContainer : ContextAwareBase, IIocContainer
|
|||||||
|
|
||||||
#endregion
|
#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
|
#region Register
|
||||||
|
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
_logger = Context.Logger;
|
_logger = Context.LoggerFactory.GetLogger(nameof(IocContainer));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
using GFramework.Core.architecture;
|
|
||||||
using GFramework.Core.logging;
|
using GFramework.Core.logging;
|
||||||
using GFramework.Core.rule;
|
using GFramework.Core.rule;
|
||||||
|
|
||||||
@ -17,7 +16,7 @@ public abstract class AbstractSystem : ContextAwareBase, ISystem
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void ISystem.Init()
|
void ISystem.Init()
|
||||||
{
|
{
|
||||||
_logger = Context.Logger;
|
_logger = Context.LoggerFactory.GetLogger(nameof(AbstractSystem));
|
||||||
_logger.Debug($"Initializing system: {GetType().Name}");
|
_logger.Debug($"Initializing system: {GetType().Name}");
|
||||||
|
|
||||||
OnInit();
|
OnInit();
|
||||||
|
|||||||
@ -9,7 +9,7 @@ namespace GFramework.Core.utility;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility
|
public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility
|
||||||
{
|
{
|
||||||
protected ILogger _logger = new NoopLoggerFactory().GetLogger(nameof(AbstractContextUtility));
|
protected ILogger Logger = null !;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化上下文工具类
|
/// 初始化上下文工具类
|
||||||
@ -17,14 +17,14 @@ public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility
|
|||||||
void IContextUtility.Init()
|
void IContextUtility.Init()
|
||||||
{
|
{
|
||||||
// 获取上下文中的日志记录器
|
// 获取上下文中的日志记录器
|
||||||
_logger = Context.Logger;
|
Logger = Context.LoggerFactory.GetLogger(nameof(AbstractContextUtility));
|
||||||
_logger.Debug($"Initializing Context Utility: {GetType().Name}");
|
Logger.Debug($"Initializing Context Utility: {GetType().Name}");
|
||||||
|
|
||||||
// 执行子类实现的初始化逻辑
|
// 执行子类实现的初始化逻辑
|
||||||
OnInit();
|
OnInit();
|
||||||
|
|
||||||
// 记录初始化完成信息
|
// 记录初始化完成信息
|
||||||
_logger.Info($"Context Utility initialized: {GetType().Name}");
|
Logger.Info($"Context Utility initialized: {GetType().Name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user