using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using GFramework.Core.Abstractions.Architectures; using GFramework.Core.Abstractions.Command; using GFramework.Core.Abstractions.Environment; using GFramework.Core.Abstractions.Events; using GFramework.Core.Abstractions.Model; using GFramework.Core.Abstractions.Query; using GFramework.Core.Abstractions.Systems; using GFramework.Core.Abstractions.Utility; using GFramework.Core.Environment; using GFramework.Core.Events; using GFramework.Core.Ioc; using GFramework.Cqrs; using GFramework.Cqrs.Abstractions.Cqrs; using ICommand = GFramework.Core.Abstractions.Command.ICommand; namespace GFramework.Core.Tests.Architectures; /// /// 为 提供最小实现的架构上下文测试桩。 /// /// /// 该类型仅用于验证 的上下文传递行为,因此仅保留当前测试切片需要的容器、 /// 环境与接口实现。所有不在本测试范围内的 CQRS 调用均明确抛出 , /// 以避免误把测试桩当作真实运行时上下文使用。 /// public class TestArchitectureContextV3 : IArchitectureContext { private readonly MicrosoftDiContainer _container = new(); private readonly DefaultEnvironment _environment = new(); private readonly EventBus _eventBus = new(); /// /// 获取或初始化用于区分测试上下文实例的标识。 /// public int Id { get; init; } /// /// 获取指定类型的服务实例。 /// /// 服务类型。 /// 已注册的服务实例。 /// 未注册或存在多个同类型实例时抛出。 public TService GetService() where TService : class { return _container.GetRequired(); } /// /// 获取指定类型的所有服务实例。 /// /// 服务类型。 /// 所有已注册的服务实例。 public IReadOnlyList GetServices() where TService : class { return _container.GetAll(); } /// /// 获取指定类型的模型实例。 /// /// 模型类型。 /// 已注册的模型实例。 /// 未注册或存在多个同类型实例时抛出。 public TModel GetModel() where TModel : class, IModel { return _container.GetRequired(); } /// /// 获取指定类型的所有模型实例。 /// /// 模型类型。 /// 所有已注册的模型实例。 public IReadOnlyList GetModels() where TModel : class, IModel { return _container.GetAll(); } /// /// 获取指定类型的系统实例。 /// /// 系统类型。 /// 已注册的系统实例。 /// 未注册或存在多个同类型实例时抛出。 public TSystem GetSystem() where TSystem : class, ISystem { return _container.GetRequired(); } /// /// 获取指定类型的所有系统实例。 /// /// 系统类型。 /// 所有已注册的系统实例。 public IReadOnlyList GetSystems() where TSystem : class, ISystem { return _container.GetAll(); } /// /// 获取指定类型的工具实例。 /// /// 工具类型。 /// 已注册的工具实例。 /// 未注册或存在多个同类型实例时抛出。 public TUtility GetUtility() where TUtility : class, IUtility { return _container.GetRequired(); } /// /// 获取指定类型的所有工具实例。 /// /// 工具类型。 /// 所有已注册的工具实例。 public IReadOnlyList GetUtilities() where TUtility : class, IUtility { return _container.GetAll(); } /// /// 获取指定类型的所有服务实例,并按优先级排序。 /// /// 服务类型。 /// 按优先级排序后的服务实例。 public IReadOnlyList GetServicesByPriority() where TService : class { return _container.GetAllByPriority(); } /// /// 获取指定类型的所有系统实例,并按优先级排序。 /// /// 系统类型。 /// 按优先级排序后的系统实例。 public IReadOnlyList GetSystemsByPriority() where TSystem : class, ISystem { return _container.GetAllByPriority(); } /// /// 获取指定类型的所有模型实例,并按优先级排序。 /// /// 模型类型。 /// 按优先级排序后的模型实例。 public IReadOnlyList GetModelsByPriority() where TModel : class, IModel { return _container.GetAllByPriority(); } /// /// 获取指定类型的所有工具实例,并按优先级排序。 /// /// 工具类型。 /// 按优先级排序后的工具实例。 public IReadOnlyList GetUtilitiesByPriority() where TUtility : class, IUtility { return _container.GetAllByPriority(); } /// /// 发送无参数事件。 /// /// 事件类型。 public void SendEvent() where TEvent : new() { _eventBus.Send(); } /// /// 发送带参数事件。 /// /// 事件类型。 /// 事件实例。 /// public void SendEvent(TEvent e) where TEvent : class { ArgumentNullException.ThrowIfNull(e); _eventBus.Send(e); } /// /// 注册事件处理器。 /// /// 事件类型。 /// 事件处理回调。 /// 用于注销回调的句柄。 /// public IUnRegister RegisterEvent(Action handler) { ArgumentNullException.ThrowIfNull(handler); return _eventBus.Register(handler); } /// /// 取消事件处理器注册。 /// /// 事件类型。 /// 要取消的事件回调。 /// public void UnRegisterEvent(Action onEvent) { ArgumentNullException.ThrowIfNull(onEvent); _eventBus.UnRegister(onEvent); } /// /// 异步发送新版 CQRS 请求。 /// /// 响应类型。 /// 要发送的请求。 /// 取消令牌。 /// 请求响应任务。 /// 该测试桩不支持此成员。 public ValueTask SendRequestAsync( IRequest request, CancellationToken cancellationToken = default) { throw new NotSupportedException(); } /// /// 同步发送新版 CQRS 请求。 /// /// 响应类型。 /// 要发送的请求。 /// 请求响应。 /// 该测试桩不支持此成员。 public TResponse SendRequest(IRequest request) { throw new NotSupportedException(); } /// /// 异步发送新版 CQRS 命令并返回响应。 /// /// 命令响应类型。 /// 要发送的命令。 /// 取消令牌。 /// 命令响应任务。 /// 该测试桩不支持此成员。 public ValueTask SendCommandAsync( GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand command, CancellationToken cancellationToken = default) { throw new NotSupportedException(); } /// /// 同步发送新版 CQRS 命令并返回响应。 /// /// 命令响应类型。 /// 要发送的命令。 /// 命令响应。 /// 该测试桩不支持此成员。 public TResponse SendCommand(GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand command) { throw new NotSupportedException(); } /// /// 异步发送新版 CQRS 查询并返回结果。 /// /// 查询结果类型。 /// 要发送的查询。 /// 取消令牌。 /// 查询结果任务。 /// 该测试桩不支持此成员。 public ValueTask SendQueryAsync( GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery query, CancellationToken cancellationToken = default) { throw new NotSupportedException(); } /// /// 同步发送新版 CQRS 查询并返回结果。 /// /// 查询结果类型。 /// 要发送的查询。 /// 查询结果。 /// 该测试桩不支持此成员。 public TResponse SendQuery(GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery query) { throw new NotSupportedException(); } /// /// 发布通知消息。 /// /// 通知类型。 /// 通知实例。 /// 取消令牌。 /// 通知发布任务。 /// 该测试桩不支持此成员。 public ValueTask PublishAsync( TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification { throw new NotSupportedException(); } /// /// 创建流式请求结果。 /// /// 流元素类型。 /// 流式请求。 /// 取消令牌。 /// 流式响应序列。 /// 该测试桩不支持此成员。 public IAsyncEnumerable CreateStream( IStreamRequest request, CancellationToken cancellationToken = default) { throw new NotSupportedException(); } /// /// 异步发送无返回值请求命令。 /// /// 命令类型。 /// 要发送的命令。 /// 取消令牌。 /// 命令发送任务。 /// 该测试桩不支持此成员。 public ValueTask SendAsync(TCommand command, CancellationToken cancellationToken = default) where TCommand : IRequest { throw new NotSupportedException(); } /// /// 异步发送带返回值请求。 /// /// 响应类型。 /// 要发送的请求。 /// 取消令牌。 /// 请求响应任务。 /// 该测试桩不支持此成员。 public ValueTask SendAsync( IRequest command, CancellationToken cancellationToken = default) { throw new NotSupportedException(); } /// /// 发送旧版无返回值命令。 /// /// 要发送的命令。 /// 该测试桩不支持旧版命令执行入口。 public void SendCommand(ICommand command) { throw new NotSupportedException(); } /// /// 发送旧版带返回值命令。 /// /// 命令响应类型。 /// 要发送的命令。 /// 此方法始终抛出异常,不返回结果。 /// 该测试桩不支持旧版命令执行入口。 public TResult SendCommand(GFramework.Core.Abstractions.Command.ICommand command) { throw new NotSupportedException(); } /// /// 异步发送旧版无返回值命令。 /// /// 要发送的命令。 /// 已失败的任务。 public Task SendCommandAsync(IAsyncCommand command) { return Task.FromException(new NotSupportedException()); } /// /// 异步发送旧版带返回值命令。 /// /// 命令响应类型。 /// 要发送的命令。 /// 已失败的任务。 public Task SendCommandAsync(IAsyncCommand command) { return Task.FromException(new NotSupportedException()); } /// /// 发送旧版查询请求。 /// /// 查询结果类型。 /// 要发送的查询。 /// 此方法始终抛出异常,不返回结果。 /// 该测试桩不支持旧版查询执行入口。 public TResult SendQuery(GFramework.Core.Abstractions.Query.IQuery query) { throw new NotSupportedException(); } /// /// 异步发送旧版查询请求。 /// /// 查询结果类型。 /// 要发送的查询。 /// 已失败的任务。 public Task SendQueryAsync(IAsyncQuery query) { return Task.FromException(new NotSupportedException()); } /// /// 获取当前测试上下文绑定的环境实例。 /// /// 默认测试环境实例。 public IEnvironment GetEnvironment() { return _environment; } }