diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs index 3974033..15ddf2f 100644 --- a/GFramework.Core/architecture/Architecture.cs +++ b/GFramework.Core/architecture/Architecture.cs @@ -1,24 +1,23 @@ -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; /// -/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。 -/// 使用单例模式确保全局唯一实例,并支持命令、查询和事件机制。 +/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。 +/// 专注于生命周期管理、初始化流程控制和架构阶段转换。 +/// 不直接提供业务操作方法,业务操作通过 ArchitectureRuntime 提供。 /// public abstract class Architecture( IArchitectureConfiguration? configuration = null, IArchitectureServices? services = null, IArchitectureContext? context = null - ) - : IArchitecture + ) + : IArchitecture, IArchitectureLifecycle { /// /// 获取架构配置对象 @@ -26,7 +25,7 @@ public abstract class Architecture( /// /// 返回一个IArchitectureConfiguration接口的实例,默认为DefaultArchitectureConfiguration类型 /// - private IArchitectureConfiguration Configuration { get; } = configuration ?? new DefaultArchitectureConfiguration(); + private IArchitectureConfiguration Configuration { get; } = configuration ?? new ArchitectureConfiguration(); /// /// 获取架构服务对象 @@ -34,7 +33,7 @@ public abstract class Architecture( /// /// 返回一个IArchitectureServices接口的实例,默认为DefaultArchitectureServices类型 /// - private IArchitectureServices Services { get; } = services ?? new DefaultArchitectureServices(); + private IArchitectureServices Services { get; } = services ?? new ArchitectureServices(); /// /// 获取依赖注入容器 @@ -52,6 +51,13 @@ public abstract class Architecture( /// private ITypeEventSystem TypeEventSystem => Services.TypeEventSystem; + /// + /// 获取架构运行时实例 + /// + /// + /// 统一的操作入口,负责命令、查询、事件的执行 + /// + public IArchitectureRuntime Runtime { get; private set; } = null!; #region Fields and Properties @@ -183,7 +189,7 @@ public abstract class Architecture( // 进入销毁阶段并发送销毁开始事件 logger.Info("Starting architecture destruction"); EnterPhase(ArchitecturePhase.Destroying); - SendEvent(new ArchitectureEvents.ArchitectureDestroyingEvent()); + TypeEventSystem.Send(new ArchitectureEvents.ArchitectureDestroyingEvent()); // 销毁所有系统组件并清空系统列表 logger.Info($"Destroying {_allSystems.Count} systems"); @@ -197,7 +203,7 @@ public abstract class Architecture( // 进入已销毁阶段并发送销毁完成事件 EnterPhase(ArchitecturePhase.Destroyed); - SendEvent(new ArchitectureEvents.ArchitectureDestroyedEvent()); + TypeEventSystem.Send(new ArchitectureEvents.ArchitectureDestroyedEvent()); logger.Info("Architecture destruction completed"); } @@ -226,7 +232,11 @@ public abstract class Architecture( public void Initialize() { _logger = Configuration.LoggerFactory.GetLogger(GetType().Name); - _context ??= new DefaultArchitectureContext(Container, TypeEventSystem, _logger); + _context ??= new ArchitectureContext(Container, TypeEventSystem, _logger); + + // 创建架构运行时实例 + Runtime = new ArchitectureRuntime(_context); + ((ArchitectureContext)_context).Runtime = Runtime; // 调用用户实现的初始化 Init(); @@ -245,7 +255,6 @@ public abstract class Architecture( EnterPhase(ArchitecturePhase.AfterModelInit); _logger.Info("All models initialized"); - // == System Init == EnterPhase(ArchitecturePhase.BeforeSystemInit); _logger.Info($"Initializing {_mSystems.Count} systems"); @@ -261,21 +270,24 @@ public abstract class Architecture( EnterPhase(ArchitecturePhase.AfterSystemInit); _logger.Info("All systems initialized"); - // == Finalize == // 冻结IOC容器,不允许 anymore Container.Freeze(); _mInited = true; EnterPhase(ArchitecturePhase.Ready); // 发送架构生命周期就绪事件 - SendEvent(new ArchitectureEvents.ArchitectureLifecycleReadyEvent()); + TypeEventSystem.Send(new ArchitectureEvents.ArchitectureLifecycleReadyEvent()); _logger.Info($"Architecture {GetType().Name} is ready - all components initialized"); } public async Task InitializeAsync() { _logger = Configuration.LoggerFactory.GetLogger(GetType().Name); - + _context ??= new ArchitectureContext(Container, TypeEventSystem, _logger); + + // 创建架构运行时实例 + Runtime = new ArchitectureRuntime(_context); + ((ArchitectureContext)_context).Runtime = Runtime; // 调用用户实现的初始化 Init(); @@ -319,7 +331,7 @@ public abstract class Architecture( Container.Freeze(); _mInited = true; EnterPhase(ArchitecturePhase.Ready); - SendEvent(new ArchitectureEvents.ArchitectureLifecycleReadyEvent()); + TypeEventSystem.Send(new ArchitectureEvents.ArchitectureLifecycleReadyEvent()); _logger.Info($"Architecture {GetType().Name} is ready - all components initialized"); } @@ -339,7 +351,7 @@ public abstract class Architecture( } _logger.Debug($"Registering system: {typeof(TSystem).Name}"); - system.SetArchitecture(this); + system.SetContext(Context); Container.RegisterPlurality(system); _allSystems.Add(system); if (!_mInited) @@ -369,7 +381,9 @@ public abstract class Architecture( } _logger.Debug($"Registering model: {typeof(TModel).Name}"); - model.SetArchitecture(this); + // 对于有 SetArchitecture 方法的模型,尝试调用该方法 + var setArchitectureMethod = typeof(TModel).GetMethod("SetArchitecture", [typeof(IArchitecture)]); + setArchitectureMethod?.Invoke(model, [this]); Container.RegisterPlurality(model); if (!_mInited) @@ -398,122 +412,16 @@ public abstract class Architecture( #endregion - - - #region Command Execution + #region IArchitectureLifecycle Implementation /// - /// 发送一个带返回结果的命令请求 + /// 处理架构阶段变更通知 /// - /// 命令执行后的返回值类型 - /// 要发送的命令对象 - /// 命令执行的结果 - public TResult SendCommand(ICommand command) + /// 当前架构阶段 + /// 架构实例 + public virtual void OnPhase(ArchitecturePhase phase, IArchitecture architecture) { - return ExecuteCommand(command); - } - - /// - /// 发送一个无返回结果的命令请求 - /// - /// 命令的具体类型 - /// 要发送的命令对象 - public void SendCommand(TCommand command) where TCommand : ICommand - { - ExecuteCommand(command); - } - - /// - /// 执行一个带返回结果的命令 - /// - /// 命令执行后的返回值类型 - /// 要执行的命令对象 - /// 命令执行的结果 - protected virtual TResult ExecuteCommand(ICommand command) - { - command.SetArchitecture(this); - return command.Execute(); - } - - /// - /// 执行一个无返回结果的命令 - /// - /// 要执行的命令对象 - protected virtual void ExecuteCommand(ICommand command) - { - command.SetArchitecture(this); - command.Execute(); - } - - #endregion - - #region Query Execution - - /// - /// 发起一次查询请求并获得其结果 - /// - /// 查询结果的数据类型 - /// 要发起的查询对象 - /// 查询得到的结果数据 - public TResult SendQuery(IQuery query) - { - return DoQuery(query); - } - - /// - /// 实际执行查询逻辑的方法 - /// - /// 查询结果的数据类型 - /// 要处理的查询对象 - /// 查询结果 - protected virtual TResult DoQuery(IQuery query) - { - query.SetArchitecture(this); - return query.Do(); - } - - #endregion - - #region Event Management - - /// - /// 发布一个默认构造的新事件对象 - /// - /// 事件类型 - public void SendEvent() where TEvent : new() - { - TypeEventSystem.Send(); - } - - /// - /// 发布一个具体的事件对象 - /// - /// 事件类型 - /// 要发布的事件实例 - public void SendEvent(TEvent e) - { - TypeEventSystem.Send(e); - } - - /// - /// 订阅某个特定类型的事件 - /// - /// 事件类型 - /// 当事件发生时触发的动作 - /// 可用于取消订阅的对象 - public IUnRegister RegisterEvent(Action onEvent) - { - return TypeEventSystem.Register(onEvent); - } - - /// - /// 取消对某类型事件的监听 - /// - /// 事件类型 - /// 之前绑定的事件处理器 - public void UnRegisterEvent(Action onEvent) - { - TypeEventSystem.UnRegister(onEvent); + } #endregion diff --git a/GFramework.Core/architecture/DefaultArchitectureConfiguration.cs b/GFramework.Core/architecture/ArchitectureConfiguration.cs similarity index 91% rename from GFramework.Core/architecture/DefaultArchitectureConfiguration.cs rename to GFramework.Core/architecture/ArchitectureConfiguration.cs index 837e1e9..f61335a 100644 --- a/GFramework.Core/architecture/DefaultArchitectureConfiguration.cs +++ b/GFramework.Core/architecture/ArchitectureConfiguration.cs @@ -6,7 +6,7 @@ namespace GFramework.Core.architecture; /// 默认架构配置类,实现IArchitectureConfiguration接口 /// 提供日志工厂、日志级别和架构选项的默认配置 /// -public class DefaultArchitectureConfiguration: IArchitectureConfiguration +public class ArchitectureConfiguration: IArchitectureConfiguration { /// /// 获取或设置日志工厂实例 diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs new file mode 100644 index 0000000..bbb5773 --- /dev/null +++ b/GFramework.Core/architecture/ArchitectureContext.cs @@ -0,0 +1,146 @@ +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; + +/// +/// 架构上下文类,提供对系统、模型、工具等组件的访问以及命令、查询、事件的执行管理 +/// +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 + + /// + /// 从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 Query Execution + + /// + /// 发送一个查询请求 + /// + /// 查询结果类型 + /// 要发送的查询 + /// 查询结果 + public TResult SendQuery(IQuery query) + { + return query == null ? throw new ArgumentNullException(nameof(query)) : Runtime.SendQuery(query); + } + + #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 +} diff --git a/GFramework.Core/architecture/ArchitectureRuntime.cs b/GFramework.Core/architecture/ArchitectureRuntime.cs new file mode 100644 index 0000000..dfe5af2 --- /dev/null +++ b/GFramework.Core/architecture/ArchitectureRuntime.cs @@ -0,0 +1,97 @@ +using GFramework.Core.command; +using GFramework.Core.events; +using GFramework.Core.query; + +namespace GFramework.Core.architecture; + +/// +/// 架构运行时默认实现,委托 ArchitectureContext 执行具体操作 +/// +public class ArchitectureRuntime(IArchitectureContext context) : IArchitectureRuntime +{ + private readonly IArchitectureContext _context = context ?? throw new ArgumentNullException(nameof(context)); + + #region Command Execution + + /// + /// 发送一个无返回结果的命令请求 + /// + /// 命令的具体类型 + /// 要发送的命令对象 + public void SendCommand(TCommand command) where TCommand : ICommand + { + _context.SendCommand(command); + } + + /// + /// 发送一个带返回结果的命令请求 + /// + /// 命令执行后的返回值类型 + /// 要发送的命令对象 + /// 命令执行的结果 + public TResult SendCommand(ICommand command) + { + return _context.SendCommand(command); + } + + #endregion + + #region Query Execution + + /// + /// 发起一次查询请求并获得其结果 + /// + /// 查询结果的数据类型 + /// 要发起的查询对象 + /// 查询得到的结果数据 + public TResult SendQuery(IQuery query) + { + return _context.SendQuery(query); + } + + #endregion + + #region Event Management + + /// + /// 发布一个默认构造的新事件对象 + /// + /// 事件类型 + public void SendEvent() where TEvent : new() + { + _context.SendEvent(); + } + + /// + /// 发布一个具体的事件对象 + /// + /// 事件类型 + /// 要发布的事件实例 + public void SendEvent(TEvent e) + { + _context.SendEvent(e); + } + + /// + /// 订阅某个特定类型的事件 + /// + /// 事件类型 + /// 当事件发生时触发的动作 + /// 可用于取消订阅的对象 + public IUnRegister RegisterEvent(Action onEvent) + { + return _context.RegisterEvent(onEvent); + } + + /// + /// 取消对某类型事件的监听 + /// + /// 事件类型 + /// 之前绑定的事件处理器 + public void UnRegisterEvent(Action onEvent) + { + _context.UnRegisterEvent(onEvent); + } + + #endregion +} \ No newline at end of file diff --git a/GFramework.Core/architecture/DefaultArchitectureServices.cs b/GFramework.Core/architecture/ArchitectureServices.cs similarity index 79% rename from GFramework.Core/architecture/DefaultArchitectureServices.cs rename to GFramework.Core/architecture/ArchitectureServices.cs index c38b1aa..09a8412 100644 --- a/GFramework.Core/architecture/DefaultArchitectureServices.cs +++ b/GFramework.Core/architecture/ArchitectureServices.cs @@ -4,7 +4,7 @@ using GFramework.Core.ioc; namespace GFramework.Core.architecture; -public class DefaultArchitectureServices: IArchitectureServices +public class ArchitectureServices: IArchitectureServices { public IIocContainer Container { get; } = new IocContainer(); public ITypeEventSystem TypeEventSystem { get; } = new TypeEventSystem(); diff --git a/GFramework.Core/architecture/DefaultArchitectureContext.cs b/GFramework.Core/architecture/DefaultArchitectureContext.cs deleted file mode 100644 index 370384e..0000000 --- a/GFramework.Core/architecture/DefaultArchitectureContext.cs +++ /dev/null @@ -1,88 +0,0 @@ - -using System.ComponentModel; -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; - -public class DefaultArchitectureContext( - IIocContainer container, - ITypeEventSystem typeEventSystem, - ILogger logger) - : IArchitectureContext -{ - private readonly ITypeEventSystem _typeEventSystem = typeEventSystem; - public ILogger Logger { get; } = logger; - - #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 - - public void SendCommand(ICommand command) - { - throw new NotImplementedException(); - } - - public TResult SendCommand(ICommand command) - { - throw new NotImplementedException(); - } - - public TResult SendQuery(IQuery query) - { - throw new NotImplementedException(); - } - - public void SendEvent() - { - throw new NotImplementedException(); - } - - public void SendEvent(TEvent e) - { - throw new NotImplementedException(); - } - - public IUnRegister RegisterEvent(Action handler) - { - throw new NotImplementedException(); - } - - -} \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitecture.cs b/GFramework.Core/architecture/IArchitecture.cs index 02eab4e..33818db 100644 --- a/GFramework.Core/architecture/IArchitecture.cs +++ b/GFramework.Core/architecture/IArchitecture.cs @@ -1,4 +1,5 @@ -using GFramework.Core.command; +using GFramework.Core.architecture; +using GFramework.Core.command; using GFramework.Core.events; using GFramework.Core.model; using GFramework.Core.query; @@ -8,8 +9,8 @@ using GFramework.Core.utility; namespace GFramework.Core.architecture; /// -/// 架构接口,定义了应用程序架构的核心功能,包括系统、模型、工具的注册和获取, -/// 以及命令、查询、事件的发送和处理机制 +/// 架构接口,专注于生命周期管理,包括系统、模型、工具的注册和获取 +/// 业务操作通过 ArchitectureRuntime 提供 /// public interface IArchitecture: IAsyncInitializable { @@ -29,78 +30,46 @@ public interface IArchitecture: IAsyncInitializable /// void Destroy(); - /// - /// 注册系统实例到架构中 + /// 注册系统实例到架构中 /// /// 系统类型,必须实现ISystem接口 /// 要注册的系统实例 void RegisterSystem(T system) where T : ISystem; /// - /// 注册模型实例到架构中 + /// 注册模型实例到架构中 /// /// 模型类型,必须实现IModel接口 /// 要注册的模型实例 void RegisterModel(T model) where T : IModel; /// - /// 注册工具实例到架构中 + /// 注册工具实例到架构中 /// /// 工具类型,必须实现IUtility接口 /// 要注册的工具实例 void RegisterUtility(T utility) where T : IUtility; - + /// + /// 安装架构模块 + /// + /// 要安装的模块 + void InstallModule(IArchitectureModule module); /// - /// 发送并执行指定的命令 + /// 注册生命周期钩子 /// - /// 命令类型,必须实现ICommand接口 - /// 要执行的命令实例 - void SendCommand(T command) where T : ICommand; + /// 生命周期钩子实例 + void RegisterLifecycleHook(IArchitectureLifecycle hook); /// - /// 发送并执行带有返回值的命令 + /// 获取架构上下文 /// - /// 命令执行结果的类型 - /// 要执行的命令实例 - /// 命令执行的结果 - TResult SendCommand(ICommand command); + IArchitectureContext Context { get; } /// - /// 发送并执行查询操作 + /// 获取架构运行时实例 /// - /// 查询结果的类型 - /// 要执行的查询实例 - /// 查询的结果 - TResult SendQuery(IQuery query); - - /// - /// 发送无参事件 - /// - /// 事件类型,必须具有无参构造函数 - void SendEvent() where T : new(); - - /// - /// 发送指定的事件实例 - /// - /// 事件类型 - /// 要发送的事件实例 - void SendEvent(T e); - - /// - /// 注册事件监听器 - /// - /// 事件类型 - /// 事件触发时的回调方法 - /// 用于取消注册的句柄 - IUnRegister RegisterEvent(Action onEvent); - - /// - /// 取消注册事件监听器 - /// - /// 事件类型 - /// 要取消注册的事件回调方法 - void UnRegisterEvent(Action onEvent); + IArchitectureRuntime Runtime { get; } } \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitectureContext.cs b/GFramework.Core/architecture/IArchitectureContext.cs index d46af92..592d546 100644 --- a/GFramework.Core/architecture/IArchitectureContext.cs +++ b/GFramework.Core/architecture/IArchitectureContext.cs @@ -59,15 +59,15 @@ public interface IArchitectureContext /// /// 发送一个事件 /// - /// 事件类型 - void SendEvent(); - + /// 事件类型,必须具有无参构造函数 + void SendEvent() where TEvent : new(); + /// /// 发送一个带参数的事件 /// /// 事件类型 /// 事件参数 - void SendEvent(TEvent e); + void SendEvent(TEvent e) where TEvent : class; /// /// 注册事件处理器 @@ -81,4 +81,11 @@ public interface IArchitectureContext /// 获取日志记录器 /// ILogger Logger { get; } + + /// + /// 取消注册事件监听器 + /// + /// 事件类型 + /// 要取消注册的事件回调方法 + void UnRegisterEvent(Action onEvent); } diff --git a/GFramework.Core/architecture/IArchitectureLifecycle.cs b/GFramework.Core/architecture/IArchitectureLifecycle.cs index 2d88f90..370d608 100644 --- a/GFramework.Core/architecture/IArchitectureLifecycle.cs +++ b/GFramework.Core/architecture/IArchitectureLifecycle.cs @@ -9,7 +9,7 @@ public interface IArchitectureLifecycle /// 当架构进入指定阶段时触发的回调方法 /// /// 当前的架构阶段 - /// 相关的架构实例 - void OnPhase(ArchitecturePhase phase, IArchitecture arch); + /// 相关的架构实例 + void OnPhase(ArchitecturePhase phase, IArchitecture architecture); } diff --git a/GFramework.Core/architecture/IArchitectureRuntime.cs b/GFramework.Core/architecture/IArchitectureRuntime.cs new file mode 100644 index 0000000..5433195 --- /dev/null +++ b/GFramework.Core/architecture/IArchitectureRuntime.cs @@ -0,0 +1,66 @@ +using GFramework.Core.command; +using GFramework.Core.events; +using GFramework.Core.model; +using GFramework.Core.query; +using GFramework.Core.system; +using GFramework.Core.utility; + +namespace GFramework.Core.architecture; + +/// +/// 架构运行时接口,提供统一的命令、查询、事件操作入口 +/// 负责委托 ArchitectureContext 的能力执行具体操作 +/// +public interface IArchitectureRuntime +{ + /// + /// 发送并执行指定的命令 + /// + /// 命令类型,必须实现ICommand接口 + /// 要执行的命令实例 + void SendCommand(T command) where T : ICommand; + + /// + /// 发送并执行带有返回值的命令 + /// + /// 命令执行结果的类型 + /// 要执行的命令实例 + /// 命令执行的结果 + TResult SendCommand(ICommand command); + + /// + /// 发送并执行查询操作 + /// + /// 查询结果的类型 + /// 要执行的查询实例 + /// 查询的结果 + TResult SendQuery(IQuery query); + + /// + /// 发送无参事件 + /// + /// 事件类型,必须具有无参构造函数 + void SendEvent() where T : new(); + + /// + /// 发送指定的事件实例 + /// + /// 事件类型 + /// 要发送的事件实例 + void SendEvent(T e); + + /// + /// 注册事件监听器 + /// + /// 事件类型 + /// 事件触发时的回调方法 + /// 用于取消注册的句柄 + IUnRegister RegisterEvent(Action onEvent); + + /// + /// 取消注册事件监听器 + /// + /// 事件类型 + /// 要取消注册的事件回调方法 + void UnRegisterEvent(Action onEvent); +} \ No newline at end of file diff --git a/GFramework.Core/query/AbstractQuery.cs b/GFramework.Core/query/AbstractQuery.cs index 756c0d6..65bc706 100644 --- a/GFramework.Core/query/AbstractQuery.cs +++ b/GFramework.Core/query/AbstractQuery.cs @@ -1,4 +1,4 @@ -using GFramework.Core.architecture; +using GFramework.Core.rule; namespace GFramework.Core.query; @@ -6,10 +6,8 @@ namespace GFramework.Core.query; /// 抽象查询类,提供查询操作的基础实现 /// /// 查询结果的类型 -public abstract class AbstractQuery : IQuery +public abstract class AbstractQuery :ContextAwareBase, IQuery { - private IArchitecture _mArchitecture; - /// /// 执行查询操作 /// @@ -18,25 +16,7 @@ public abstract class AbstractQuery : IQuery { return OnDo(); } - - /// - /// 获取架构实例 - /// - /// 架构实例 - public IArchitecture GetArchitecture() - { - return _mArchitecture; - } - - /// - /// 设置架构实例 - /// - /// 要设置的架构实例 - public void SetArchitecture(IArchitecture architecture) - { - _mArchitecture = architecture; - } - + /// /// 抽象方法,由子类实现具体的查询逻辑 /// diff --git a/GFramework.Core/query/IQuery.cs b/GFramework.Core/query/IQuery.cs index 6ab3dec..5d8417a 100644 --- a/GFramework.Core/query/IQuery.cs +++ b/GFramework.Core/query/IQuery.cs @@ -1,6 +1,4 @@ -using GFramework.Core.model; using GFramework.Core.rule; -using GFramework.Core.system; namespace GFramework.Core.query; @@ -8,7 +6,7 @@ namespace GFramework.Core.query; /// 查询接口,定义了执行查询操作的契约 /// /// 查询结果的类型 -public interface IQuery +public interface IQuery:IContextAware { /// /// 执行查询操作并返回结果