mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
feat(ioc): 添加Microsoft DI容器适配器和CQRS运行时模块
- 移除过时的Cqrs抽象引用 - 添加MicrosoftDiContainer实现IIocContainer接口 - 提供线程安全的依赖注入容器功能 - 支持单例、瞬态、作用域生命周期管理 - 实现CQRS请求管道行为注册功能 - 添加CqrsRuntimeModule服务模块 - 提供CQRS运行时实现和处理器注册器 - 扩展IArchitectureContext接口支持CQRS契约
This commit is contained in:
parent
385893c791
commit
1973fb2a60
@ -18,7 +18,7 @@ namespace GFramework.Core.Abstractions.Architectures;
|
|||||||
/// <para>新的 <c>GFramework.Cqrs.Abstractions.Cqrs</c> 契约由内置 CQRS dispatcher 统一处理,支持 request pipeline、notification publish 与 stream request。</para>
|
/// <para>新的 <c>GFramework.Cqrs.Abstractions.Cqrs</c> 契约由内置 CQRS dispatcher 统一处理,支持 request pipeline、notification publish 与 stream request。</para>
|
||||||
/// <para>新功能优先使用 <see cref="SendRequestAsync{TResponse}(IRequest{TResponse},CancellationToken)" />、<see cref="SendAsync{TCommand}(TCommand,CancellationToken)" /> 与对应的 CQRS Command/Query 重载;迁移旧代码时可先保留旧入口,再逐步替换为 CQRS 请求模型。</para>
|
/// <para>新功能优先使用 <see cref="SendRequestAsync{TResponse}(IRequest{TResponse},CancellationToken)" />、<see cref="SendAsync{TCommand}(TCommand,CancellationToken)" /> 与对应的 CQRS Command/Query 重载;迁移旧代码时可先保留旧入口,再逐步替换为 CQRS 请求模型。</para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public interface IArchitectureContext
|
public interface IArchitectureContext : ICqrsContext
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的服务实例
|
/// 获取指定类型的服务实例
|
||||||
|
|||||||
@ -1,52 +1,16 @@
|
|||||||
using GFramework.Core.Abstractions.Architectures;
|
using System.ComponentModel;
|
||||||
using GFramework.Cqrs.Abstractions.Cqrs;
|
|
||||||
|
|
||||||
namespace GFramework.Core.Abstractions.Cqrs;
|
namespace GFramework.Core.Abstractions.Cqrs;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义架构上下文使用的 CQRS runtime seam。
|
/// 提供旧 <c>GFramework.Core.Abstractions.Cqrs</c> 命名空间下的 CQRS runtime 兼容别名。
|
||||||
/// 该抽象把请求分发、通知发布与流式处理从具体实现中解耦,
|
|
||||||
/// 使 <see cref="IArchitectureContext" /> 不再直接依赖某个固定的 runtime 类型。
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ICqrsRuntime
|
/// <remarks>
|
||||||
|
/// 正式 runtime seam 已迁移到 <see cref="GFramework.Cqrs.Abstractions.Cqrs.ICqrsRuntime" />,
|
||||||
|
/// 但当前仍保留该接口以避免立即打断历史公开路径与既有二进制引用。
|
||||||
|
/// 新代码应优先依赖 <c>GFramework.Cqrs.Abstractions.Cqrs</c> 下的正式契约。
|
||||||
|
/// </remarks>
|
||||||
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
|
public interface ICqrsRuntime : GFramework.Cqrs.Abstractions.Cqrs.ICqrsRuntime
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 发送请求并返回响应。
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResponse">响应类型。</typeparam>
|
|
||||||
/// <param name="context">当前架构上下文,用于上下文感知处理器注入与嵌套请求访问。</param>
|
|
||||||
/// <param name="request">要分发的请求。</param>
|
|
||||||
/// <param name="cancellationToken">取消令牌。</param>
|
|
||||||
/// <returns>请求响应。</returns>
|
|
||||||
ValueTask<TResponse> SendAsync<TResponse>(
|
|
||||||
IArchitectureContext context,
|
|
||||||
IRequest<TResponse> request,
|
|
||||||
CancellationToken cancellationToken = default);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发布通知到所有已注册处理器。
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TNotification">通知类型。</typeparam>
|
|
||||||
/// <param name="context">当前架构上下文,用于上下文感知处理器注入。</param>
|
|
||||||
/// <param name="notification">要发布的通知。</param>
|
|
||||||
/// <param name="cancellationToken">取消令牌。</param>
|
|
||||||
/// <returns>表示通知分发完成的值任务。</returns>
|
|
||||||
ValueTask PublishAsync<TNotification>(
|
|
||||||
IArchitectureContext context,
|
|
||||||
TNotification notification,
|
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
where TNotification : INotification;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 创建流式请求的异步响应序列。
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResponse">流元素类型。</typeparam>
|
|
||||||
/// <param name="context">当前架构上下文,用于上下文感知处理器注入。</param>
|
|
||||||
/// <param name="request">流式请求。</param>
|
|
||||||
/// <param name="cancellationToken">取消令牌。</param>
|
|
||||||
/// <returns>按需生成的异步响应序列。</returns>
|
|
||||||
IAsyncEnumerable<TResponse> CreateStream<TResponse>(
|
|
||||||
IArchitectureContext context,
|
|
||||||
IStreamRequest<TResponse> request,
|
|
||||||
CancellationToken cancellationToken = default);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using GFramework.Core.Abstractions.Architectures;
|
using GFramework.Core.Abstractions.Architectures;
|
||||||
using GFramework.Core.Abstractions.Command;
|
using GFramework.Core.Abstractions.Command;
|
||||||
using GFramework.Core.Abstractions.Cqrs;
|
|
||||||
using GFramework.Core.Abstractions.Enums;
|
using GFramework.Core.Abstractions.Enums;
|
||||||
using GFramework.Core.Abstractions.Environment;
|
using GFramework.Core.Abstractions.Environment;
|
||||||
using GFramework.Core.Abstractions.Ioc;
|
using GFramework.Core.Abstractions.Ioc;
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using GFramework.Core.Abstractions.Bases;
|
using GFramework.Core.Abstractions.Bases;
|
||||||
using GFramework.Core.Abstractions.Cqrs;
|
|
||||||
using GFramework.Core.Abstractions.Logging;
|
using GFramework.Core.Abstractions.Logging;
|
||||||
using GFramework.Core.Ioc;
|
using GFramework.Core.Ioc;
|
||||||
using GFramework.Core.Logging;
|
using GFramework.Core.Logging;
|
||||||
using GFramework.Core.Tests.Cqrs;
|
using GFramework.Core.Tests.Cqrs;
|
||||||
using GFramework.Core.Tests.Systems;
|
using GFramework.Core.Tests.Systems;
|
||||||
using GFramework.Cqrs.Abstractions.Cqrs;
|
using GFramework.Cqrs.Abstractions.Cqrs;
|
||||||
|
using LegacyICqrsRuntime = GFramework.Core.Abstractions.Cqrs.ICqrsRuntime;
|
||||||
|
|
||||||
namespace GFramework.Core.Tests.Ioc;
|
namespace GFramework.Core.Tests.Ioc;
|
||||||
|
|
||||||
@ -160,12 +160,16 @@ public class MicrosoftDiContainerTests
|
|||||||
public void RegisterHandlers_Should_Not_Duplicate_Cqrs_Infrastructure_When_It_Is_Already_Registered()
|
public void RegisterHandlers_Should_Not_Duplicate_Cqrs_Infrastructure_When_It_Is_Already_Registered()
|
||||||
{
|
{
|
||||||
Assert.That(_container.GetAll<ICqrsRuntime>(), Has.Count.EqualTo(1));
|
Assert.That(_container.GetAll<ICqrsRuntime>(), Has.Count.EqualTo(1));
|
||||||
|
Assert.That(_container.GetAll<LegacyICqrsRuntime>(), Has.Count.EqualTo(1));
|
||||||
Assert.That(_container.GetAll<ICqrsHandlerRegistrar>(), Has.Count.EqualTo(1));
|
Assert.That(_container.GetAll<ICqrsHandlerRegistrar>(), Has.Count.EqualTo(1));
|
||||||
|
Assert.That(_container.Get<ICqrsRuntime>(), Is.SameAs(_container.Get<LegacyICqrsRuntime>()));
|
||||||
|
|
||||||
CqrsTestRuntime.RegisterHandlers(_container);
|
CqrsTestRuntime.RegisterHandlers(_container);
|
||||||
|
|
||||||
Assert.That(_container.GetAll<ICqrsRuntime>(), Has.Count.EqualTo(1));
|
Assert.That(_container.GetAll<ICqrsRuntime>(), Has.Count.EqualTo(1));
|
||||||
|
Assert.That(_container.GetAll<LegacyICqrsRuntime>(), Has.Count.EqualTo(1));
|
||||||
Assert.That(_container.GetAll<ICqrsHandlerRegistrar>(), Has.Count.EqualTo(1));
|
Assert.That(_container.GetAll<ICqrsHandlerRegistrar>(), Has.Count.EqualTo(1));
|
||||||
|
Assert.That(_container.Get<ICqrsRuntime>(), Is.SameAs(_container.Get<LegacyICqrsRuntime>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using GFramework.Core.Abstractions.Architectures;
|
using GFramework.Core.Abstractions.Architectures;
|
||||||
using GFramework.Core.Abstractions.Command;
|
using GFramework.Core.Abstractions.Command;
|
||||||
using GFramework.Core.Abstractions.Cqrs;
|
|
||||||
using GFramework.Core.Abstractions.Environment;
|
using GFramework.Core.Abstractions.Environment;
|
||||||
using GFramework.Core.Abstractions.Events;
|
using GFramework.Core.Abstractions.Events;
|
||||||
using GFramework.Core.Abstractions.Ioc;
|
using GFramework.Core.Abstractions.Ioc;
|
||||||
@ -190,7 +189,7 @@ public class ArchitectureContext : IArchitectureContext
|
|||||||
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||||
/// <param name="query">要发送的查询对象</param>
|
/// <param name="query">要发送的查询对象</param>
|
||||||
/// <returns>查询结果</returns>
|
/// <returns>查询结果</returns>
|
||||||
public TResponse SendQuery<TResponse>(GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery<TResponse> query)
|
public TResponse SendQuery<TResponse>(Cqrs.Abstractions.Cqrs.Query.IQuery<TResponse> query)
|
||||||
{
|
{
|
||||||
return SendQueryAsync(query).AsTask().GetAwaiter().GetResult();
|
return SendQueryAsync(query).AsTask().GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
@ -216,8 +215,7 @@ public class ArchitectureContext : IArchitectureContext
|
|||||||
/// <param name="query">要发送的查询对象</param>
|
/// <param name="query">要发送的查询对象</param>
|
||||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||||
/// <returns>包含查询结果的ValueTask</returns>
|
/// <returns>包含查询结果的ValueTask</returns>
|
||||||
public async ValueTask<TResponse> SendQueryAsync<TResponse>(
|
public async ValueTask<TResponse> SendQueryAsync<TResponse>(Cqrs.Abstractions.Cqrs.Query.IQuery<TResponse> query,
|
||||||
GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery<TResponse> query,
|
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(query);
|
ArgumentNullException.ThrowIfNull(query);
|
||||||
@ -354,7 +352,7 @@ public class ArchitectureContext : IArchitectureContext
|
|||||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||||
/// <returns>包含命令执行结果的ValueTask</returns>
|
/// <returns>包含命令执行结果的ValueTask</returns>
|
||||||
public async ValueTask<TResponse> SendCommandAsync<TResponse>(
|
public async ValueTask<TResponse> SendCommandAsync<TResponse>(
|
||||||
GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand<TResponse> command,
|
Cqrs.Abstractions.Cqrs.Command.ICommand<TResponse> command,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
@ -393,7 +391,7 @@ public class ArchitectureContext : IArchitectureContext
|
|||||||
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
||||||
/// <param name="command">要发送的命令对象</param>
|
/// <param name="command">要发送的命令对象</param>
|
||||||
/// <returns>命令执行结果</returns>
|
/// <returns>命令执行结果</returns>
|
||||||
public TResponse SendCommand<TResponse>(GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand<TResponse> command)
|
public TResponse SendCommand<TResponse>(Cqrs.Abstractions.Cqrs.Command.ICommand<TResponse> command)
|
||||||
{
|
{
|
||||||
return SendCommandAsync(command).AsTask().GetAwaiter().GetResult();
|
return SendCommandAsync(command).AsTask().GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,6 @@ using GFramework.Core.Abstractions.Bases;
|
|||||||
using GFramework.Core.Abstractions.Ioc;
|
using GFramework.Core.Abstractions.Ioc;
|
||||||
using GFramework.Core.Abstractions.Logging;
|
using GFramework.Core.Abstractions.Logging;
|
||||||
using GFramework.Core.Abstractions.Systems;
|
using GFramework.Core.Abstractions.Systems;
|
||||||
using GFramework.Core.Logging;
|
|
||||||
using GFramework.Core.Rule;
|
using GFramework.Core.Rule;
|
||||||
using GFramework.Cqrs.Abstractions.Cqrs;
|
using GFramework.Cqrs.Abstractions.Cqrs;
|
||||||
|
|
||||||
@ -624,11 +623,14 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
|||||||
.Where(s => s.ServiceType == serviceType || serviceType.IsAssignableFrom(s.ServiceType)).ToList();
|
.Where(s => s.ServiceType == serviceType || serviceType.IsAssignableFrom(s.ServiceType)).ToList();
|
||||||
|
|
||||||
var result = new List<T>();
|
var result = new List<T>();
|
||||||
|
var seenInstances = new HashSet<object>(ReferenceEqualityComparer.Instance);
|
||||||
foreach (var descriptor in registeredServices)
|
foreach (var descriptor in registeredServices)
|
||||||
{
|
{
|
||||||
if (descriptor.ImplementationInstance is T instance)
|
if (descriptor.ImplementationInstance is T instance)
|
||||||
{
|
{
|
||||||
result.Add(instance);
|
// 同一实例可能同时以“正式接口 + 兼容别名接口”被注册;未冻结路径需去重以保持与冻结后的解析口径一致。
|
||||||
|
if (seenInstances.Add(instance))
|
||||||
|
result.Add(instance);
|
||||||
}
|
}
|
||||||
else if (descriptor.ImplementationFactory != null)
|
else if (descriptor.ImplementationFactory != null)
|
||||||
{
|
{
|
||||||
@ -672,11 +674,14 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
|||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
var result = new List<object>();
|
var result = new List<object>();
|
||||||
|
var seenInstances = new HashSet<object>(ReferenceEqualityComparer.Instance);
|
||||||
foreach (var descriptor in registeredServices)
|
foreach (var descriptor in registeredServices)
|
||||||
{
|
{
|
||||||
if (descriptor.ImplementationInstance != null)
|
if (descriptor.ImplementationInstance != null)
|
||||||
{
|
{
|
||||||
result.Add(descriptor.ImplementationInstance);
|
// 同一实例可能通过多个可赋值服务类型暴露;返回前按引用去重,避免兼容别名造成重复观察结果。
|
||||||
|
if (seenInstances.Add(descriptor.ImplementationInstance))
|
||||||
|
result.Add(descriptor.ImplementationInstance);
|
||||||
}
|
}
|
||||||
else if (descriptor.ImplementationFactory != null)
|
else if (descriptor.ImplementationFactory != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
using GFramework.Core.Abstractions.Architectures;
|
using GFramework.Core.Abstractions.Architectures;
|
||||||
using GFramework.Core.Abstractions.Cqrs;
|
|
||||||
using GFramework.Core.Abstractions.Ioc;
|
using GFramework.Core.Abstractions.Ioc;
|
||||||
using GFramework.Core.Abstractions.Logging;
|
using GFramework.Core.Abstractions.Logging;
|
||||||
using GFramework.Cqrs;
|
using GFramework.Cqrs;
|
||||||
using GFramework.Cqrs.Abstractions.Cqrs;
|
using GFramework.Cqrs.Abstractions.Cqrs;
|
||||||
|
using LegacyICqrsRuntime = GFramework.Core.Abstractions.Cqrs.ICqrsRuntime;
|
||||||
|
|
||||||
namespace GFramework.Core.Services.Modules;
|
namespace GFramework.Core.Services.Modules;
|
||||||
|
|
||||||
@ -39,8 +39,10 @@ public sealed class CqrsRuntimeModule : IServiceModule
|
|||||||
|
|
||||||
var dispatcherLogger = LoggerFactoryResolver.Provider.CreateLogger("CqrsDispatcher");
|
var dispatcherLogger = LoggerFactoryResolver.Provider.CreateLogger("CqrsDispatcher");
|
||||||
var registrarLogger = LoggerFactoryResolver.Provider.CreateLogger("DefaultCqrsHandlerRegistrar");
|
var registrarLogger = LoggerFactoryResolver.Provider.CreateLogger("DefaultCqrsHandlerRegistrar");
|
||||||
|
var runtime = CqrsRuntimeFactory.CreateRuntime(container, dispatcherLogger);
|
||||||
|
|
||||||
container.Register<ICqrsRuntime>(CqrsRuntimeFactory.CreateRuntime(container, dispatcherLogger));
|
container.Register(runtime);
|
||||||
|
container.Register<LegacyICqrsRuntime>((LegacyICqrsRuntime)runtime);
|
||||||
container.Register<ICqrsHandlerRegistrar>(
|
container.Register<ICqrsHandlerRegistrar>(
|
||||||
CqrsRuntimeFactory.CreateHandlerRegistrar(container, registrarLogger));
|
CqrsRuntimeFactory.CreateHandlerRegistrar(container, registrarLogger));
|
||||||
}
|
}
|
||||||
|
|||||||
13
GFramework.Cqrs.Abstractions/Cqrs/ICqrsContext.cs
Normal file
13
GFramework.Cqrs.Abstractions/Cqrs/ICqrsContext.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
namespace GFramework.Cqrs.Abstractions.Cqrs;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 定义 CQRS runtime 在分发期间携带的最小上下文标记。
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// 该接口当前刻意保持为轻量 marker seam,只用于让 <see cref="ICqrsRuntime" /> 从
|
||||||
|
/// <c>GFramework.Core.Abstractions</c> 的 <c>IArchitectureContext</c> 解耦。
|
||||||
|
/// 运行时实现仍可在需要时识别更具体的上下文类型,并对现有 <c>IContextAware</c> 处理器执行兼容注入。
|
||||||
|
/// </remarks>
|
||||||
|
public interface ICqrsContext
|
||||||
|
{
|
||||||
|
}
|
||||||
49
GFramework.Cqrs.Abstractions/Cqrs/ICqrsRuntime.cs
Normal file
49
GFramework.Cqrs.Abstractions/Cqrs/ICqrsRuntime.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
namespace GFramework.Cqrs.Abstractions.Cqrs;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 定义架构上下文使用的 CQRS runtime seam。
|
||||||
|
/// 该抽象把请求分发、通知发布与流式处理从具体实现中解耦,
|
||||||
|
/// 使 CQRS runtime 契约可独立归属到 <c>GFramework.Cqrs.Abstractions</c>。
|
||||||
|
/// </summary>
|
||||||
|
public interface ICqrsRuntime
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 发送请求并返回响应。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">响应类型。</typeparam>
|
||||||
|
/// <param name="context">当前 CQRS 分发上下文。</param>
|
||||||
|
/// <param name="request">要分发的请求。</param>
|
||||||
|
/// <param name="cancellationToken">取消令牌。</param>
|
||||||
|
/// <returns>请求响应。</returns>
|
||||||
|
ValueTask<TResponse> SendAsync<TResponse>(
|
||||||
|
ICqrsContext context,
|
||||||
|
IRequest<TResponse> request,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发布通知到所有已注册处理器。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TNotification">通知类型。</typeparam>
|
||||||
|
/// <param name="context">当前 CQRS 分发上下文。</param>
|
||||||
|
/// <param name="notification">要发布的通知。</param>
|
||||||
|
/// <param name="cancellationToken">取消令牌。</param>
|
||||||
|
/// <returns>表示通知分发完成的值任务。</returns>
|
||||||
|
ValueTask PublishAsync<TNotification>(
|
||||||
|
ICqrsContext context,
|
||||||
|
TNotification notification,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
where TNotification : INotification;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建流式请求的异步响应序列。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">流元素类型。</typeparam>
|
||||||
|
/// <param name="context">当前 CQRS 分发上下文。</param>
|
||||||
|
/// <param name="request">流式请求。</param>
|
||||||
|
/// <param name="cancellationToken">取消令牌。</param>
|
||||||
|
/// <returns>按需生成的异步响应序列。</returns>
|
||||||
|
IAsyncEnumerable<TResponse> CreateStream<TResponse>(
|
||||||
|
ICqrsContext context,
|
||||||
|
IStreamRequest<TResponse> request,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
@ -1,4 +1,3 @@
|
|||||||
using GFramework.Core.Abstractions.Cqrs;
|
|
||||||
using GFramework.Core.Abstractions.Ioc;
|
using GFramework.Core.Abstractions.Ioc;
|
||||||
using GFramework.Core.Abstractions.Logging;
|
using GFramework.Core.Abstractions.Logging;
|
||||||
using GFramework.Cqrs.Abstractions.Cqrs;
|
using GFramework.Cqrs.Abstractions.Cqrs;
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using GFramework.Core.Abstractions.Architectures;
|
using GFramework.Core.Abstractions.Architectures;
|
||||||
using GFramework.Core.Abstractions.Cqrs;
|
|
||||||
using GFramework.Core.Abstractions.Ioc;
|
using GFramework.Core.Abstractions.Ioc;
|
||||||
using GFramework.Core.Abstractions.Logging;
|
using GFramework.Core.Abstractions.Logging;
|
||||||
using GFramework.Core.Abstractions.Rule;
|
using GFramework.Core.Abstractions.Rule;
|
||||||
using GFramework.Cqrs.Abstractions.Cqrs;
|
using GFramework.Cqrs.Abstractions.Cqrs;
|
||||||
|
using ICqrsRuntime = GFramework.Core.Abstractions.Cqrs.ICqrsRuntime;
|
||||||
|
|
||||||
namespace GFramework.Cqrs.Internal;
|
namespace GFramework.Cqrs.Internal;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// GFramework 自有 CQRS 运行时分发器。
|
/// GFramework 自有 CQRS 运行时分发器。
|
||||||
/// 该类型负责解析请求/通知处理器,并在调用前为上下文感知对象注入当前架构上下文。
|
/// 该类型负责解析请求/通知处理器,并在调用前为上下文感知对象注入当前 CQRS 分发上下文。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal sealed class CqrsDispatcher(
|
internal sealed class CqrsDispatcher(
|
||||||
IIocContainer container,
|
IIocContainer container,
|
||||||
@ -38,11 +38,11 @@ internal sealed class CqrsDispatcher(
|
|||||||
/// 发布通知到所有已注册处理器。
|
/// 发布通知到所有已注册处理器。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TNotification">通知类型。</typeparam>
|
/// <typeparam name="TNotification">通知类型。</typeparam>
|
||||||
/// <param name="context">当前架构上下文,用于上下文感知处理器注入。</param>
|
/// <param name="context">当前 CQRS 分发上下文,用于上下文感知处理器注入。</param>
|
||||||
/// <param name="notification">通知对象。</param>
|
/// <param name="notification">通知对象。</param>
|
||||||
/// <param name="cancellationToken">取消令牌。</param>
|
/// <param name="cancellationToken">取消令牌。</param>
|
||||||
public async ValueTask PublishAsync<TNotification>(
|
public async ValueTask PublishAsync<TNotification>(
|
||||||
IArchitectureContext context,
|
ICqrsContext context,
|
||||||
TNotification notification,
|
TNotification notification,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
where TNotification : INotification
|
where TNotification : INotification
|
||||||
@ -75,12 +75,12 @@ internal sealed class CqrsDispatcher(
|
|||||||
/// 发送请求并返回结果。
|
/// 发送请求并返回结果。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TResponse">响应类型。</typeparam>
|
/// <typeparam name="TResponse">响应类型。</typeparam>
|
||||||
/// <param name="context">当前架构上下文,用于上下文感知处理器注入。</param>
|
/// <param name="context">当前 CQRS 分发上下文,用于上下文感知处理器注入。</param>
|
||||||
/// <param name="request">请求对象。</param>
|
/// <param name="request">请求对象。</param>
|
||||||
/// <param name="cancellationToken">取消令牌。</param>
|
/// <param name="cancellationToken">取消令牌。</param>
|
||||||
/// <returns>请求响应。</returns>
|
/// <returns>请求响应。</returns>
|
||||||
public async ValueTask<TResponse> SendAsync<TResponse>(
|
public async ValueTask<TResponse> SendAsync<TResponse>(
|
||||||
IArchitectureContext context,
|
ICqrsContext context,
|
||||||
IRequest<TResponse> request,
|
IRequest<TResponse> request,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
@ -122,12 +122,12 @@ internal sealed class CqrsDispatcher(
|
|||||||
/// 创建流式请求并返回异步响应序列。
|
/// 创建流式请求并返回异步响应序列。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TResponse">响应元素类型。</typeparam>
|
/// <typeparam name="TResponse">响应元素类型。</typeparam>
|
||||||
/// <param name="context">当前架构上下文,用于上下文感知处理器注入。</param>
|
/// <param name="context">当前 CQRS 分发上下文,用于上下文感知处理器注入。</param>
|
||||||
/// <param name="request">流式请求对象。</param>
|
/// <param name="request">流式请求对象。</param>
|
||||||
/// <param name="cancellationToken">取消令牌。</param>
|
/// <param name="cancellationToken">取消令牌。</param>
|
||||||
/// <returns>异步响应序列。</returns>
|
/// <returns>异步响应序列。</returns>
|
||||||
public IAsyncEnumerable<TResponse> CreateStream<TResponse>(
|
public IAsyncEnumerable<TResponse> CreateStream<TResponse>(
|
||||||
IArchitectureContext context,
|
ICqrsContext context,
|
||||||
IStreamRequest<TResponse> request,
|
IStreamRequest<TResponse> request,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
@ -150,14 +150,20 @@ internal sealed class CqrsDispatcher(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 为上下文感知处理器注入当前架构上下文。
|
/// 为上下文感知处理器注入当前 CQRS 分发上下文。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="handler">处理器实例。</param>
|
/// <param name="handler">处理器实例。</param>
|
||||||
/// <param name="context">当前架构上下文。</param>
|
/// <param name="context">当前 CQRS 分发上下文。</param>
|
||||||
private static void PrepareHandler(object handler, IArchitectureContext context)
|
private static void PrepareHandler(object handler, ICqrsContext context)
|
||||||
{
|
{
|
||||||
if (handler is IContextAware contextAware)
|
if (handler is IContextAware contextAware)
|
||||||
contextAware.SetContext(context);
|
{
|
||||||
|
if (context is not IArchitectureContext architectureContext)
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
"The current CQRS context does not implement IArchitectureContext, so it cannot be injected into IContextAware handlers.");
|
||||||
|
|
||||||
|
contextAware.SetContext(architectureContext);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using GFramework.Core.Abstractions.Cqrs;
|
|
||||||
using GFramework.Core.Abstractions.Ioc;
|
using GFramework.Core.Abstractions.Ioc;
|
||||||
using GFramework.Core.Abstractions.Logging;
|
using GFramework.Core.Abstractions.Logging;
|
||||||
using GFramework.Core.Ioc;
|
using GFramework.Core.Ioc;
|
||||||
using GFramework.Cqrs;
|
using GFramework.Cqrs;
|
||||||
using GFramework.Cqrs.Abstractions.Cqrs;
|
using GFramework.Cqrs.Abstractions.Cqrs;
|
||||||
using GFramework.Cqrs.Command;
|
using GFramework.Cqrs.Command;
|
||||||
|
using LegacyICqrsRuntime = GFramework.Core.Abstractions.Cqrs.ICqrsRuntime;
|
||||||
|
|
||||||
namespace GFramework.Tests.Common;
|
namespace GFramework.Tests.Common;
|
||||||
|
|
||||||
@ -60,7 +60,12 @@ public static class CqrsTestRuntime
|
|||||||
{
|
{
|
||||||
var runtimeLogger = LoggerFactoryResolver.Provider.CreateLogger("CqrsDispatcher");
|
var runtimeLogger = LoggerFactoryResolver.Provider.CreateLogger("CqrsDispatcher");
|
||||||
var runtime = CqrsRuntimeFactory.CreateRuntime(container, runtimeLogger);
|
var runtime = CqrsRuntimeFactory.CreateRuntime(container, runtimeLogger);
|
||||||
container.Register<ICqrsRuntime>(runtime);
|
container.Register(runtime);
|
||||||
|
container.Register<LegacyICqrsRuntime>((LegacyICqrsRuntime)runtime);
|
||||||
|
}
|
||||||
|
else if (container.Get<LegacyICqrsRuntime>() is null)
|
||||||
|
{
|
||||||
|
container.Register<LegacyICqrsRuntime>((LegacyICqrsRuntime)container.GetRequired<ICqrsRuntime>());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (container.Get<ICqrsHandlerRegistrar>() is null)
|
if (container.Get<ICqrsHandlerRegistrar>() is null)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user