using GFramework.Core.Abstractions.architecture; using GFramework.Core.Abstractions.command; using GFramework.Core.Abstractions.environment; 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) : IArchitectureContext { private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container)); private readonly Dictionary _serviceCache = new(); /// /// 获取指定类型的服务实例,如果缓存中存在则直接返回,否则从容器中获取并缓存 /// /// 服务类型,必须为引用类型 /// 服务实例,如果未找到则返回null public TService? GetService() where TService : class { return GetOrCache(); } /// /// 从缓存中获取或创建指定类型的服务实例 /// 首先尝试从缓存中获取服务实例,如果缓存中不存在则从容器中获取并存入缓存 /// /// 服务类型,必须为引用类型 /// 服务实例,如果未找到则返回null private TService? GetOrCache() where TService : class { // 尝试从服务缓存中获取已存在的服务实例 if (_serviceCache.TryGetValue(typeof(TService), out var cached)) return (TService)cached; // 从依赖注入容器中获取服务实例 var service = _container.Get(); // 如果服务实例存在,则将其存入缓存以供后续使用 if (service != null) _serviceCache[typeof(TService)] = service; return service; } #region Query Execution /// /// 发送一个查询请求 /// /// 查询结果类型 /// 要发送的查询 /// 查询结果 public TResult SendQuery(IQuery query) { if (query == null) throw new ArgumentNullException(nameof(query)); var queryBus = GetOrCache(); if (queryBus == null) throw new InvalidOperationException("IQueryBus not registered"); return queryBus.Send(query); } /// /// 异步发送一个查询请求 /// /// 查询结果类型 /// 要发送的异步查询 /// 查询结果 public async Task SendQueryAsync(IAsyncQuery query) { if (query == null) throw new ArgumentNullException(nameof(query)); var asyncQueryBus = GetOrCache(); if (asyncQueryBus == null) throw new InvalidOperationException("IAsyncQueryBus not registered"); return await asyncQueryBus.SendAsync(query); } #endregion #region Component Retrieval /// /// 从IOC容器中获取指定类型的系统实例 /// /// 目标系统类型 /// 对应的系统实例 public TSystem? GetSystem() where TSystem : class, ISystem { return GetService(); } /// /// 从IOC容器中获取指定类型的模型实例 /// /// 目标模型类型 /// 对应的模型实例 public TModel? GetModel() where TModel : class, IModel { return GetService(); } /// /// 从IOC容器中获取指定类型的工具实例 /// /// 目标工具类型 /// 对应的工具实例 public TUtility? GetUtility() where TUtility : class, IUtility { return GetService(); } #endregion #region Command Execution /// /// 发送一个命令请求 /// /// 要发送的命令 public void SendCommand(ICommand command) { ArgumentNullException.ThrowIfNull(command); var commandBus = GetOrCache(); commandBus?.Send(command); } /// /// 发送一个带返回值的命令请求 /// /// 命令执行结果类型 /// 要发送的命令 /// 命令执行结果 public TResult SendCommand(ICommand command) { ArgumentNullException.ThrowIfNull(command); var commandBus = GetOrCache(); if (commandBus == null) throw new InvalidOperationException("ICommandBus not registered"); return commandBus.Send(command); } /// /// 发送并异步执行一个命令请求 /// /// 要发送的命令 public async Task SendCommandAsync(IAsyncCommand command) { ArgumentNullException.ThrowIfNull(command); var commandBus = GetOrCache(); if (commandBus == null) throw new InvalidOperationException("ICommandBus not registered"); await commandBus.SendAsync(command); } /// /// 发送并异步执行一个带返回值的命令请求 /// /// 命令执行结果类型 /// 要发送的命令 /// 命令执行结果 public async Task SendCommandAsync(IAsyncCommand command) { ArgumentNullException.ThrowIfNull(command); var commandBus = GetOrCache(); if (commandBus == null) throw new InvalidOperationException("ICommandBus not registered"); return await commandBus.SendAsync(command); } #endregion #region Event Management /// /// 发送一个默认构造的新事件 /// /// 事件类型 public void SendEvent() where TEvent : new() { var eventBus = GetOrCache(); eventBus?.Send(); } /// /// 发送一个具体的事件实例 /// /// 事件类型 /// 事件参数 public void SendEvent(TEvent e) where TEvent : class { ArgumentNullException.ThrowIfNull(e); var eventBus = GetOrCache(); eventBus?.Send(e); } /// /// 注册事件处理器 /// /// 事件类型 /// 事件处理委托 /// 事件注销接口 public IUnRegister RegisterEvent(Action handler) { ArgumentNullException.ThrowIfNull(handler); var eventBus = GetOrCache(); if (eventBus == null) throw new InvalidOperationException("IEventBus not registered"); return eventBus.Register(handler); } /// /// 取消对某类型事件的监听 /// /// 事件类型 /// 之前绑定的事件处理器 public void UnRegisterEvent(Action onEvent) { ArgumentNullException.ThrowIfNull(onEvent); var eventBus = GetOrCache(); eventBus?.UnRegister(onEvent); } /// /// 获取当前环境对象 /// /// 环境对象实例 public IEnvironment GetEnvironment() { var environment = GetOrCache(); return environment ?? throw new InvalidOperationException("IEnvironment not registered"); } #endregion }