GFramework/GFramework.Core/architecture/ArchitectureContext.cs
GwWuYou 5b7eaea142 refactor(architecture): 重构架构基类以支持上下文和运行时模式
- 移除架构中的命令和查询执行方法,将业务操作委托给 ArchitectureRuntime
- 引入 ArchitectureContext 类统一管理组件访问和事件处理
- 创建 ArchitectureRuntime 类作为统一的命令、查询、事件操作入口
- 更新架构生命周期管理,添加对 IArchitectureLifecycle 的支持
- 重命名 DefaultArchitectureConfiguration 为 ArchitectureConfiguration
- 重命名 DefaultArchitectureServices 为 ArchitectureServices
- 删除旧的 DefaultArchitectureContext 类
- 更新查询接口实现,使用 ContextAwareBase 基类
- 修改系统和模型注册逻辑,使用上下文而非架构实例
- 重构事件发送机制,统一使用 TypeEventSystem
- [no tag]
2025-12-24 23:39:34 +08:00

147 lines
4.7 KiB
C#

using GFramework.Core.command;
using GFramework.Core.events;
using GFramework.Core.ioc;
using GFramework.Core.logging;
using GFramework.Core.model;
using GFramework.Core.query;
using GFramework.Core.system;
using GFramework.Core.utility;
namespace GFramework.Core.architecture;
/// <summary>
/// 架构上下文类,提供对系统、模型、工具等组件的访问以及命令、查询、事件的执行管理
/// </summary>
public class ArchitectureContext(
IIocContainer container,
ITypeEventSystem typeEventSystem,
ILogger logger)
: IArchitectureContext
{
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
private readonly ITypeEventSystem _typeEventSystem = typeEventSystem ?? throw new ArgumentNullException(nameof(typeEventSystem));
public ILogger Logger { get; } = logger ?? throw new ArgumentNullException(nameof(logger));
internal IArchitectureRuntime Runtime { get; set; } = null!;
#region Component Retrieval
/// <summary>
/// 从IOC容器中获取指定类型的系统实例
/// </summary>
/// <typeparam name="TSystem">目标系统类型</typeparam>
/// <returns>对应的系统实例</returns>
public TSystem? GetSystem<TSystem>() where TSystem : class, ISystem
{
return _container.Get<TSystem>();
}
/// <summary>
/// 从IOC容器中获取指定类型的模型实例
/// </summary>
/// <typeparam name="TModel">目标模型类型</typeparam>
/// <returns>对应的模型实例</returns>
public TModel? GetModel<TModel>() where TModel : class, IModel
{
return _container.Get<TModel>();
}
/// <summary>
/// 从IOC容器中获取指定类型的工具实例
/// </summary>
/// <typeparam name="TUtility">目标工具类型</typeparam>
/// <returns>对应的工具实例</returns>
public TUtility? GetUtility<TUtility>() where TUtility : class, IUtility
{
return _container.Get<TUtility>();
}
#endregion
#region Command Execution
/// <summary>
/// 发送一个无返回结果的命令
/// </summary>
/// <param name="command">要发送的命令</param>
public void SendCommand(ICommand command)
{
ArgumentNullException.ThrowIfNull(command);
Runtime.SendCommand(command);
}
/// <summary>
/// 发送一个带返回值的命令
/// </summary>
/// <typeparam name="TResult">命令执行结果类型</typeparam>
/// <param name="command">要发送的命令</param>
/// <returns>命令执行结果</returns>
public TResult SendCommand<TResult>(ICommand<TResult> command)
{
ArgumentNullException.ThrowIfNull(command);
return Runtime.SendCommand(command);
}
#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>
/// 发送一个默认构造的新事件
/// </summary>
/// <typeparam name="TEvent">事件类型</typeparam>
public void SendEvent<TEvent>() where TEvent : new()
{
_typeEventSystem.Send<TEvent>();
}
/// <summary>
/// 发送一个具体的事件实例
/// </summary>
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="e">事件参数</param>
public void SendEvent<TEvent>(TEvent e) where TEvent : class
{
if (e == null) throw new ArgumentNullException(nameof(e));
_typeEventSystem.Send(e);
}
/// <summary>
/// 注册事件处理器
/// </summary>
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="handler">事件处理委托</param>
/// <returns>事件注销接口</returns>
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler)
{
return handler == null ? throw new ArgumentNullException(nameof(handler)) : _typeEventSystem.Register(handler);
}
/// <summary>
/// 取消对某类型事件的监听
/// </summary>
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="onEvent">之前绑定的事件处理器</param>
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
{
ArgumentNullException.ThrowIfNull(onEvent);
_typeEventSystem.UnRegister(onEvent);
}
#endregion
}