mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-24 03:41:33 +08:00
- 在Architecture类中添加AsyncQueryBus属性 - 在ArchitectureContext中添加异步查询执行方法SendQueryAsync - 在ArchitectureServices中添加AsyncQueryBus服务实例 - 扩展IArchitectureContext接口以包含异步查询方法 - 扩展IArchitectureServices接口以包含异步查询总线 - 更新ArchitectureContext构造函数以接受异步查询总线参数 - 为ArchitectureContextTests添加异步查询总线相关测试用例 - 更新测试中的构造函数调用以包含新的异步查询总线参数
201 lines
6.6 KiB
C#
201 lines
6.6 KiB
C#
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;
|
|
using IAsyncQueryBus = GFramework.Core.Abstractions.query.IAsyncQueryBus;
|
|
|
|
namespace GFramework.Core.architecture;
|
|
|
|
/// <summary>
|
|
/// 架构上下文类,提供对系统、模型、工具等组件的访问以及命令、查询、事件的执行管理
|
|
/// </summary>
|
|
public class ArchitectureContext(
|
|
IIocContainer container,
|
|
IEventBus eventBus,
|
|
ICommandBus commandBus,
|
|
IQueryBus queryBus,
|
|
IEnvironment environment,
|
|
IAsyncQueryBus asyncQueryBus)
|
|
: IArchitectureContext
|
|
{
|
|
private readonly IAsyncQueryBus _asyncQueryBus =
|
|
asyncQueryBus ?? throw new ArgumentNullException(nameof(asyncQueryBus));
|
|
|
|
private readonly ICommandBus _commandBus = commandBus ?? throw new ArgumentNullException(nameof(commandBus));
|
|
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
|
|
|
|
private readonly IEnvironment _environment = environment ?? throw new ArgumentNullException(nameof(environment));
|
|
|
|
private readonly IEventBus _eventBus =
|
|
eventBus ?? throw new ArgumentNullException(nameof(eventBus));
|
|
|
|
private readonly IQueryBus _queryBus = queryBus ?? throw new ArgumentNullException(nameof(queryBus));
|
|
|
|
#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)) : _queryBus.Send(query);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步发送一个查询请求
|
|
/// </summary>
|
|
/// <typeparam name="TResult">查询结果类型</typeparam>
|
|
/// <param name="query">要发送的异步查询</param>
|
|
/// <returns>查询结果</returns>
|
|
public Task<TResult> SendQueryAsync<TResult>(IAsyncQuery<TResult> query)
|
|
{
|
|
return query == null ? throw new ArgumentNullException(nameof(query)) : _asyncQueryBus.SendAsync(query);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#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);
|
|
_commandBus.Send(command);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送一个带返回值的命令请求
|
|
/// </summary>
|
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
|
/// <param name="command">要发送的命令</param>
|
|
/// <returns>命令执行结果</returns>
|
|
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
return _commandBus.Send(command);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送并异步执行一个命令请求
|
|
/// </summary>
|
|
/// <param name="command">要发送的命令</param>
|
|
public async Task SendCommandAsync(IAsyncCommand command)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
await _commandBus.SendAsync(command);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送并异步执行一个带返回值的命令请求
|
|
/// </summary>
|
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
|
/// <param name="command">要发送的命令</param>
|
|
/// <returns>命令执行结果</returns>
|
|
public async Task<TResult> SendCommandAsync<TResult>(IAsyncCommand<TResult> command)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
return await _commandBus.SendAsync(command);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Event Management
|
|
|
|
/// <summary>
|
|
/// 发送一个默认构造的新事件
|
|
/// </summary>
|
|
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
public void SendEvent<TEvent>() where TEvent : new()
|
|
{
|
|
_eventBus.Send<TEvent>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送一个具体的事件实例
|
|
/// </summary>
|
|
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
/// <param name="e">事件参数</param>
|
|
public void SendEvent<TEvent>(TEvent e) where TEvent : class
|
|
{
|
|
ArgumentNullException.ThrowIfNull(e);
|
|
_eventBus.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)) : _eventBus.Register(handler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取消对某类型事件的监听
|
|
/// </summary>
|
|
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
/// <param name="onEvent">之前绑定的事件处理器</param>
|
|
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(onEvent);
|
|
_eventBus.UnRegister(onEvent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前环境对象
|
|
/// </summary>
|
|
/// <returns>环境对象实例</returns>
|
|
public IEnvironment GetEnvironment()
|
|
{
|
|
return _environment;
|
|
}
|
|
|
|
#endregion
|
|
} |