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