mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(architecture): 重构架构基类以支持上下文和运行时模式
- 移除架构中的命令和查询执行方法,将业务操作委托给 ArchitectureRuntime - 引入 ArchitectureContext 类统一管理组件访问和事件处理 - 创建 ArchitectureRuntime 类作为统一的命令、查询、事件操作入口 - 更新架构生命周期管理,添加对 IArchitectureLifecycle 的支持 - 重命名 DefaultArchitectureConfiguration 为 ArchitectureConfiguration - 重命名 DefaultArchitectureServices 为 ArchitectureServices - 删除旧的 DefaultArchitectureContext 类 - 更新查询接口实现,使用 ContextAwareBase 基类 - 修改系统和模型注册逻辑,使用上下文而非架构实例 - 重构事件发送机制,统一使用 TypeEventSystem - [no tag]
This commit is contained in:
parent
3e672cf56f
commit
5b7eaea142
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。
|
||||
/// 使用单例模式确保全局唯一实例,并支持命令、查询和事件机制。
|
||||
/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。
|
||||
/// 专注于生命周期管理、初始化流程控制和架构阶段转换。
|
||||
/// 不直接提供业务操作方法,业务操作通过 ArchitectureRuntime 提供。
|
||||
/// </summary>
|
||||
public abstract class Architecture(
|
||||
IArchitectureConfiguration? configuration = null,
|
||||
IArchitectureServices? services = null,
|
||||
IArchitectureContext? context = null
|
||||
)
|
||||
: IArchitecture
|
||||
)
|
||||
: IArchitecture, IArchitectureLifecycle
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取架构配置对象
|
||||
@ -26,7 +25,7 @@ public abstract class Architecture(
|
||||
/// <value>
|
||||
/// 返回一个IArchitectureConfiguration接口的实例,默认为DefaultArchitectureConfiguration类型
|
||||
/// </value>
|
||||
private IArchitectureConfiguration Configuration { get; } = configuration ?? new DefaultArchitectureConfiguration();
|
||||
private IArchitectureConfiguration Configuration { get; } = configuration ?? new ArchitectureConfiguration();
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构服务对象
|
||||
@ -34,7 +33,7 @@ public abstract class Architecture(
|
||||
/// <value>
|
||||
/// 返回一个IArchitectureServices接口的实例,默认为DefaultArchitectureServices类型
|
||||
/// </value>
|
||||
private IArchitectureServices Services { get; } = services ?? new DefaultArchitectureServices();
|
||||
private IArchitectureServices Services { get; } = services ?? new ArchitectureServices();
|
||||
|
||||
/// <summary>
|
||||
/// 获取依赖注入容器
|
||||
@ -52,6 +51,13 @@ public abstract class Architecture(
|
||||
/// </value>
|
||||
private ITypeEventSystem TypeEventSystem => Services.TypeEventSystem;
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构运行时实例
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// 统一的操作入口,负责命令、查询、事件的执行
|
||||
/// </value>
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个带返回结果的命令请求
|
||||
/// 处理架构阶段变更通知
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行后的返回值类型</typeparam>
|
||||
/// <param name="command">要发送的命令对象</param>
|
||||
/// <returns>命令执行的结果</returns>
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
||||
/// <param name="phase">当前架构阶段</param>
|
||||
/// <param name="architecture">架构实例</param>
|
||||
public virtual void OnPhase(ArchitecturePhase phase, IArchitecture architecture)
|
||||
{
|
||||
return ExecuteCommand(command);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个无返回结果的命令请求
|
||||
/// </summary>
|
||||
/// <typeparam name="TCommand">命令的具体类型</typeparam>
|
||||
/// <param name="command">要发送的命令对象</param>
|
||||
public void SendCommand<TCommand>(TCommand command) where TCommand : ICommand
|
||||
{
|
||||
ExecuteCommand(command);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行一个带返回结果的命令
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行后的返回值类型</typeparam>
|
||||
/// <param name="command">要执行的命令对象</param>
|
||||
/// <returns>命令执行的结果</returns>
|
||||
protected virtual TResult ExecuteCommand<TResult>(ICommand<TResult> command)
|
||||
{
|
||||
command.SetArchitecture(this);
|
||||
return command.Execute();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行一个无返回结果的命令
|
||||
/// </summary>
|
||||
/// <param name="command">要执行的命令对象</param>
|
||||
protected virtual void ExecuteCommand(ICommand command)
|
||||
{
|
||||
command.SetArchitecture(this);
|
||||
command.Execute();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Query Execution
|
||||
|
||||
/// <summary>
|
||||
/// 发起一次查询请求并获得其结果
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的数据类型</typeparam>
|
||||
/// <param name="query">要发起的查询对象</param>
|
||||
/// <returns>查询得到的结果数据</returns>
|
||||
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
||||
{
|
||||
return DoQuery(query);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实际执行查询逻辑的方法
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的数据类型</typeparam>
|
||||
/// <param name="query">要处理的查询对象</param>
|
||||
/// <returns>查询结果</returns>
|
||||
protected virtual TResult DoQuery<TResult>(IQuery<TResult> query)
|
||||
{
|
||||
query.SetArchitecture(this);
|
||||
return query.Do();
|
||||
}
|
||||
|
||||
#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)
|
||||
{
|
||||
TypeEventSystem.Send(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅某个特定类型的事件
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="onEvent">当事件发生时触发的动作</param>
|
||||
/// <returns>可用于取消订阅的对象</returns>
|
||||
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> onEvent)
|
||||
{
|
||||
return TypeEventSystem.Register(onEvent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消对某类型事件的监听
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="onEvent">之前绑定的事件处理器</param>
|
||||
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
|
||||
{
|
||||
TypeEventSystem.UnRegister(onEvent);
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -6,7 +6,7 @@ namespace GFramework.Core.architecture;
|
||||
/// 默认架构配置类,实现IArchitectureConfiguration接口
|
||||
/// 提供日志工厂、日志级别和架构选项的默认配置
|
||||
/// </summary>
|
||||
public class DefaultArchitectureConfiguration: IArchitectureConfiguration
|
||||
public class ArchitectureConfiguration: IArchitectureConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置日志工厂实例
|
||||
146
GFramework.Core/architecture/ArchitectureContext.cs
Normal file
146
GFramework.Core/architecture/ArchitectureContext.cs
Normal file
@ -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;
|
||||
|
||||
/// <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
|
||||
}
|
||||
97
GFramework.Core/architecture/ArchitectureRuntime.cs
Normal file
97
GFramework.Core/architecture/ArchitectureRuntime.cs
Normal file
@ -0,0 +1,97 @@
|
||||
using GFramework.Core.command;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.query;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 架构运行时默认实现,委托 ArchitectureContext 执行具体操作
|
||||
/// </summary>
|
||||
public class ArchitectureRuntime(IArchitectureContext context) : IArchitectureRuntime
|
||||
{
|
||||
private readonly IArchitectureContext _context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
|
||||
#region Command Execution
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个无返回结果的命令请求
|
||||
/// </summary>
|
||||
/// <typeparam name="TCommand">命令的具体类型</typeparam>
|
||||
/// <param name="command">要发送的命令对象</param>
|
||||
public void SendCommand<TCommand>(TCommand command) where TCommand : ICommand
|
||||
{
|
||||
_context.SendCommand(command);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个带返回结果的命令请求
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行后的返回值类型</typeparam>
|
||||
/// <param name="command">要发送的命令对象</param>
|
||||
/// <returns>命令执行的结果</returns>
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
||||
{
|
||||
return _context.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 _context.SendQuery(query);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Management
|
||||
|
||||
/// <summary>
|
||||
/// 发布一个默认构造的新事件对象
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
public void SendEvent<TEvent>() where TEvent : new()
|
||||
{
|
||||
_context.SendEvent<TEvent>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发布一个具体的事件对象
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="e">要发布的事件实例</param>
|
||||
public void SendEvent<TEvent>(TEvent e)
|
||||
{
|
||||
_context.SendEvent(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅某个特定类型的事件
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="onEvent">当事件发生时触发的动作</param>
|
||||
/// <returns>可用于取消订阅的对象</returns>
|
||||
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> onEvent)
|
||||
{
|
||||
return _context.RegisterEvent(onEvent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消对某类型事件的监听
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="onEvent">之前绑定的事件处理器</param>
|
||||
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
|
||||
{
|
||||
_context.UnRegisterEvent(onEvent);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -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();
|
||||
@ -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
|
||||
|
||||
/// <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
|
||||
|
||||
public void SendCommand(ICommand command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SendEvent<TEvent>()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SendEvent<TEvent>(TEvent e)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 架构接口,定义了应用程序架构的核心功能,包括系统、模型、工具的注册和获取,
|
||||
/// 以及命令、查询、事件的发送和处理机制
|
||||
/// 架构接口,专注于生命周期管理,包括系统、模型、工具的注册和获取
|
||||
/// 业务操作通过 ArchitectureRuntime 提供
|
||||
/// </summary>
|
||||
public interface IArchitecture: IAsyncInitializable
|
||||
{
|
||||
@ -29,78 +30,46 @@ public interface IArchitecture: IAsyncInitializable
|
||||
/// </remarks>
|
||||
void Destroy();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 注册系统实例到架构中
|
||||
/// 注册系统实例到架构中
|
||||
/// </summary>
|
||||
/// <typeparam name="T">系统类型,必须实现ISystem接口</typeparam>
|
||||
/// <param name="system">要注册的系统实例</param>
|
||||
void RegisterSystem<T>(T system) where T : ISystem;
|
||||
|
||||
/// <summary>
|
||||
/// 注册模型实例到架构中
|
||||
/// 注册模型实例到架构中
|
||||
/// </summary>
|
||||
/// <typeparam name="T">模型类型,必须实现IModel接口</typeparam>
|
||||
/// <param name="model">要注册的模型实例</param>
|
||||
void RegisterModel<T>(T model) where T : IModel;
|
||||
|
||||
/// <summary>
|
||||
/// 注册工具实例到架构中
|
||||
/// 注册工具实例到架构中
|
||||
/// </summary>
|
||||
/// <typeparam name="T">工具类型,必须实现IUtility接口</typeparam>
|
||||
/// <param name="utility">要注册的工具实例</param>
|
||||
void RegisterUtility<T>(T utility) where T : IUtility;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 安装架构模块
|
||||
/// </summary>
|
||||
/// <param name="module">要安装的模块</param>
|
||||
void InstallModule(IArchitectureModule module);
|
||||
|
||||
/// <summary>
|
||||
/// 发送并执行指定的命令
|
||||
/// 注册生命周期钩子
|
||||
/// </summary>
|
||||
/// <typeparam name="T">命令类型,必须实现ICommand接口</typeparam>
|
||||
/// <param name="command">要执行的命令实例</param>
|
||||
void SendCommand<T>(T command) where T : ICommand;
|
||||
/// <param name="hook">生命周期钩子实例</param>
|
||||
void RegisterLifecycleHook(IArchitectureLifecycle hook);
|
||||
|
||||
/// <summary>
|
||||
/// 发送并执行带有返回值的命令
|
||||
/// 获取架构上下文
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
||||
/// <param name="command">要执行的命令实例</param>
|
||||
/// <returns>命令执行的结果</returns>
|
||||
TResult SendCommand<TResult>(ICommand<TResult> command);
|
||||
IArchitectureContext Context { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 发送并执行查询操作
|
||||
/// 获取架构运行时实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
/// <param name="query">要执行的查询实例</param>
|
||||
/// <returns>查询的结果</returns>
|
||||
TResult SendQuery<TResult>(IQuery<TResult> query);
|
||||
|
||||
/// <summary>
|
||||
/// 发送无参事件
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
|
||||
void SendEvent<T>() where T : new();
|
||||
|
||||
/// <summary>
|
||||
/// 发送指定的事件实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="e">要发送的事件实例</param>
|
||||
void SendEvent<T>(T e);
|
||||
|
||||
/// <summary>
|
||||
/// 注册事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="onEvent">事件触发时的回调方法</param>
|
||||
/// <returns>用于取消注册的句柄</returns>
|
||||
IUnRegister RegisterEvent<T>(Action<T> onEvent);
|
||||
|
||||
/// <summary>
|
||||
/// 取消注册事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="onEvent">要取消注册的事件回调方法</param>
|
||||
void UnRegisterEvent<T>(Action<T> onEvent);
|
||||
IArchitectureRuntime Runtime { get; }
|
||||
}
|
||||
@ -59,15 +59,15 @@ public interface IArchitectureContext
|
||||
/// <summary>
|
||||
/// 发送一个事件
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
void SendEvent<TEvent>();
|
||||
|
||||
/// <typeparam name="TEvent">事件类型,必须具有无参构造函数</typeparam>
|
||||
void SendEvent<TEvent>() where TEvent : new();
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个带参数的事件
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="e">事件参数</param>
|
||||
void SendEvent<TEvent>(TEvent e);
|
||||
void SendEvent<TEvent>(TEvent e) where TEvent : class;
|
||||
|
||||
/// <summary>
|
||||
/// 注册事件处理器
|
||||
@ -81,4 +81,11 @@ public interface IArchitectureContext
|
||||
/// 获取日志记录器
|
||||
/// </summary>
|
||||
ILogger Logger { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 取消注册事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="onEvent">要取消注册的事件回调方法</param>
|
||||
void UnRegisterEvent<TEvent>(Action<TEvent> onEvent);
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ public interface IArchitectureLifecycle
|
||||
/// 当架构进入指定阶段时触发的回调方法
|
||||
/// </summary>
|
||||
/// <param name="phase">当前的架构阶段</param>
|
||||
/// <param name="arch">相关的架构实例</param>
|
||||
void OnPhase(ArchitecturePhase phase, IArchitecture arch);
|
||||
/// <param name="architecture">相关的架构实例</param>
|
||||
void OnPhase(ArchitecturePhase phase, IArchitecture architecture);
|
||||
}
|
||||
|
||||
|
||||
66
GFramework.Core/architecture/IArchitectureRuntime.cs
Normal file
66
GFramework.Core/architecture/IArchitectureRuntime.cs
Normal file
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 架构运行时接口,提供统一的命令、查询、事件操作入口
|
||||
/// 负责委托 ArchitectureContext 的能力执行具体操作
|
||||
/// </summary>
|
||||
public interface IArchitectureRuntime
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送并执行指定的命令
|
||||
/// </summary>
|
||||
/// <typeparam name="T">命令类型,必须实现ICommand接口</typeparam>
|
||||
/// <param name="command">要执行的命令实例</param>
|
||||
void SendCommand<T>(T command) where T : ICommand;
|
||||
|
||||
/// <summary>
|
||||
/// 发送并执行带有返回值的命令
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
||||
/// <param name="command">要执行的命令实例</param>
|
||||
/// <returns>命令执行的结果</returns>
|
||||
TResult SendCommand<TResult>(ICommand<TResult> command);
|
||||
|
||||
/// <summary>
|
||||
/// 发送并执行查询操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
/// <param name="query">要执行的查询实例</param>
|
||||
/// <returns>查询的结果</returns>
|
||||
TResult SendQuery<TResult>(IQuery<TResult> query);
|
||||
|
||||
/// <summary>
|
||||
/// 发送无参事件
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
|
||||
void SendEvent<T>() where T : new();
|
||||
|
||||
/// <summary>
|
||||
/// 发送指定的事件实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="e">要发送的事件实例</param>
|
||||
void SendEvent<T>(T e);
|
||||
|
||||
/// <summary>
|
||||
/// 注册事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="onEvent">事件触发时的回调方法</param>
|
||||
/// <returns>用于取消注册的句柄</returns>
|
||||
IUnRegister RegisterEvent<T>(Action<T> onEvent);
|
||||
|
||||
/// <summary>
|
||||
/// 取消注册事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="onEvent">要取消注册的事件回调方法</param>
|
||||
void UnRegisterEvent<T>(Action<T> onEvent);
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.query;
|
||||
|
||||
@ -6,10 +6,8 @@ namespace GFramework.Core.query;
|
||||
/// 抽象查询类,提供查询操作的基础实现
|
||||
/// </summary>
|
||||
/// <typeparam name="T">查询结果的类型</typeparam>
|
||||
public abstract class AbstractQuery<T> : IQuery<T>
|
||||
public abstract class AbstractQuery<T> :ContextAwareBase, IQuery<T>
|
||||
{
|
||||
private IArchitecture _mArchitecture;
|
||||
|
||||
/// <summary>
|
||||
/// 执行查询操作
|
||||
/// </summary>
|
||||
@ -18,25 +16,7 @@ public abstract class AbstractQuery<T> : IQuery<T>
|
||||
{
|
||||
return OnDo();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构实例
|
||||
/// </summary>
|
||||
/// <returns>架构实例</returns>
|
||||
public IArchitecture GetArchitecture()
|
||||
{
|
||||
return _mArchitecture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置架构实例
|
||||
/// </summary>
|
||||
/// <param name="architecture">要设置的架构实例</param>
|
||||
public void SetArchitecture(IArchitecture architecture)
|
||||
{
|
||||
_mArchitecture = architecture;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 抽象方法,由子类实现具体的查询逻辑
|
||||
/// </summary>
|
||||
|
||||
@ -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;
|
||||
/// 查询接口,定义了执行查询操作的契约
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
public interface IQuery<out TResult>
|
||||
public interface IQuery<out TResult>:IContextAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行查询操作并返回结果
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user