Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ Architecture 负责统一生命周期编排,核心阶段包括:

### CQRS

命令与查询分离,支持同步与异步执行。Mediator 模式通过源码生成器集成,以减少模板代码并保持调用路径清晰。
命令与查询分离,支持同步与异步执行。当前版本内建自有 CQRS runtime、行为管道和 handler 自动注册;公开 API 里仍保留少量历史
`Mediator` 命名以兼容旧调用点。

### EventBus

Expand Down
17 changes: 13 additions & 4 deletions GFramework.Core.Abstractions/Architectures/IArchitecture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using GFramework.Core.Abstractions.Model;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
using Microsoft.Extensions.DependencyInjection;

namespace GFramework.Core.Abstractions.Architectures;

Expand Down Expand Up @@ -73,12 +72,22 @@ public interface IArchitecture : IAsyncInitializable, IAsyncDestroyable, IInitia
void RegisterUtility<T>(Action<T>? onCreated = null) where T : class, IUtility;

/// <summary>
/// 注册中介行为管道
/// 用于配置Mediator框架的行为拦截和处理逻辑。
/// 注册 CQRS 请求管道行为。
/// 既支持实现 <c>IPipelineBehavior&lt;,&gt;</c> 的开放泛型行为类型,
/// 也支持绑定到单一请求/响应对的封闭行为类型。
/// </summary>
/// <typeparam name="TBehavior">行为类型,必须是引用类型</typeparam>
void RegisterCqrsPipelineBehavior<TBehavior>()
where TBehavior : class;

/// <summary>
/// 注册 CQRS 请求管道行为。
/// 该成员保留旧名称以兼容历史调用点,内部行为与 <see cref="RegisterCqrsPipelineBehavior{TBehavior}" /> 一致。
/// 既支持实现 <c>IPipelineBehavior&lt;,&gt;</c> 的开放泛型行为类型,
/// 也支持绑定到单一请求/响应对的封闭行为类型。
/// </summary>
/// <typeparam name="TBehavior">行为类型,必须是引用类型</typeparam>
[Obsolete("Use RegisterCqrsPipelineBehavior<TBehavior>() instead.")]
void RegisterMediatorBehavior<TBehavior>()
where TBehavior : class;

Expand All @@ -101,4 +110,4 @@ void RegisterMediatorBehavior<TBehavior>()
/// </summary>
/// <returns>表示异步等待操作的任务</returns>
Task WaitUntilReadyAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Mediator.Abstractions" Version="3.0.2"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.5"/>
</ItemGroup>
</Project>
15 changes: 11 additions & 4 deletions GFramework.Core.Abstractions/Ioc/IIocContainer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using GFramework.Core.Abstractions.Rule;
using GFramework.Core.Abstractions.Systems;
using Microsoft.Extensions.DependencyInjection;

namespace GFramework.Core.Abstractions.Ioc;

Expand Down Expand Up @@ -90,10 +89,18 @@ void RegisterScoped<TService, TImpl>()
void RegisterFactory<TService>(Func<IServiceProvider, TService> factory) where TService : class;

/// <summary>
/// 注册中介行为管道
/// 用于配置Mediator框架的行为拦截和处理逻辑
/// 注册 CQRS 请求管道行为。
/// </summary>
/// <typeparam name="TBehavior">行为类型,必须是引用类型</typeparam>
void RegisterCqrsPipelineBehavior<TBehavior>()
where TBehavior : class;

/// <summary>
/// 注册 CQRS 请求管道行为。
/// 该成员保留旧名称以兼容历史调用点,内部行为与 <see cref="RegisterCqrsPipelineBehavior{TBehavior}" /> 一致。
/// </summary>
/// <typeparam name="TBehavior">行为类型,必须是引用类型</typeparam>
[Obsolete("Use RegisterCqrsPipelineBehavior<TBehavior>() instead.")]
void RegisterMediatorBehavior<TBehavior>()
where TBehavior : class;

Expand Down Expand Up @@ -227,4 +234,4 @@ void RegisterMediatorBehavior<TBehavior>()
IServiceScope CreateScope();

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public async Task InstallModule_Should_Invoke_Module_Install_With_Current_Archit
/// 验证注册的 CQRS 行为会参与请求管道执行。
/// </summary>
[Test]
public async Task RegisterMediatorBehavior_Should_Apply_Pipeline_Behavior_To_Request()
public async Task RegisterCqrsPipelineBehavior_Should_Apply_Pipeline_Behavior_To_Request()
{
var architecture = new ModuleTestArchitecture(target =>
target.RegisterMediatorBehavior<TrackingPipelineBehavior<ModuleBehaviorRequest, string>>());
target.RegisterCqrsPipelineBehavior<TrackingPipelineBehavior<ModuleBehaviorRequest, string>>());
Comment thread
coderabbitai[bot] marked this conversation as resolved.

await architecture.InitializeAsync();

Expand Down
31 changes: 15 additions & 16 deletions GFramework.Core.Tests/Architectures/ArchitectureServicesTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Abstractions.Command;
using GFramework.Core.Abstractions.Cqrs;
using GFramework.Core.Abstractions.Environment;
using GFramework.Core.Abstractions.Events;
using GFramework.Core.Abstractions.Ioc;
Expand All @@ -13,7 +14,6 @@
using GFramework.Core.Events;
using GFramework.Core.Ioc;
using GFramework.Core.Query;
using Mediator;
using ICommand = GFramework.Core.Abstractions.Command.ICommand;

namespace GFramework.Core.Tests.Architectures;
Expand All @@ -34,16 +34,17 @@ namespace GFramework.Core.Tests.Architectures;
[TestFixture]
public class ArchitectureServicesTests
{
private TestArchitectureContextV3? _context;

private ArchitectureServices? _services;

[SetUp]
public void SetUp()
{
_services = new ArchitectureServices();
_context = new TestArchitectureContextV3();
}

private ArchitectureServices? _services;
private TestArchitectureContextV3? _context;

private void RegisterBuiltInServices()
{
_services!.ModuleManager.RegisterBuiltInModules(_services.Container);
Expand Down Expand Up @@ -347,61 +348,59 @@ public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
{
}

public ValueTask<TResponse> SendRequestAsync<TResponse>(global::GFramework.Core.Abstractions.Cqrs.IRequest<TResponse> request,
public ValueTask<TResponse> SendRequestAsync<TResponse>(IRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public TResponse SendRequest<TResponse>(global::GFramework.Core.Abstractions.Cqrs.IRequest<TResponse> request)
public TResponse SendRequest<TResponse>(IRequest<TResponse> request)
{
throw new NotImplementedException();
}

public ValueTask<TResponse> SendCommandAsync<TResponse>(
global::GFramework.Core.Abstractions.Cqrs.Command.ICommand<TResponse> command,
public ValueTask<TResponse> SendCommandAsync<TResponse>(Abstractions.Cqrs.Command.ICommand<TResponse> command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public TResponse SendCommand<TResponse>(global::GFramework.Core.Abstractions.Cqrs.Command.ICommand<TResponse> command)
public TResponse SendCommand<TResponse>(Abstractions.Cqrs.Command.ICommand<TResponse> command)
{
throw new NotImplementedException();
}

public ValueTask<TResponse> SendQueryAsync<TResponse>(
global::GFramework.Core.Abstractions.Cqrs.Query.IQuery<TResponse> query,
public ValueTask<TResponse> SendQueryAsync<TResponse>(Abstractions.Cqrs.Query.IQuery<TResponse> query,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public TResponse SendQuery<TResponse>(global::GFramework.Core.Abstractions.Cqrs.Query.IQuery<TResponse> query)
public TResponse SendQuery<TResponse>(Abstractions.Cqrs.Query.IQuery<TResponse> query)
{
throw new NotImplementedException();
}

public ValueTask PublishAsync<TNotification>(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : global::GFramework.Core.Abstractions.Cqrs.INotification
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}

public IAsyncEnumerable<TResponse> CreateStream<TResponse>(
global::GFramework.Core.Abstractions.Cqrs.IStreamRequest<TResponse> request,
IStreamRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public ValueTask SendAsync<TCommand>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : global::GFramework.Core.Abstractions.Cqrs.IRequest<global::GFramework.Core.Abstractions.Cqrs.Unit>
where TCommand : IRequest<Unit>
{
throw new NotImplementedException();
}

public ValueTask<TResponse> SendAsync<TResponse>(global::GFramework.Core.Abstractions.Cqrs.IRequest<TResponse> command,
public ValueTask<TResponse> SendAsync<TResponse>(IRequest<TResponse> command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
Expand Down
28 changes: 13 additions & 15 deletions GFramework.Core.Tests/Architectures/GameContextTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Abstractions.Command;
using GFramework.Core.Abstractions.Cqrs;
using GFramework.Core.Abstractions.Environment;
using GFramework.Core.Abstractions.Events;
using GFramework.Core.Abstractions.Ioc;
Expand All @@ -13,7 +14,6 @@
using GFramework.Core.Events;
using GFramework.Core.Ioc;
using GFramework.Core.Query;
using Mediator;
using ICommand = GFramework.Core.Abstractions.Command.ICommand;

namespace GFramework.Core.Tests.Architectures;
Expand Down Expand Up @@ -394,61 +394,59 @@ public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
{
}

public ValueTask<TResponse> SendRequestAsync<TResponse>(global::GFramework.Core.Abstractions.Cqrs.IRequest<TResponse> request,
public ValueTask<TResponse> SendRequestAsync<TResponse>(IRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public TResponse SendRequest<TResponse>(global::GFramework.Core.Abstractions.Cqrs.IRequest<TResponse> request)
public TResponse SendRequest<TResponse>(IRequest<TResponse> request)
{
throw new NotImplementedException();
}

public ValueTask<TResponse> SendCommandAsync<TResponse>(
global::GFramework.Core.Abstractions.Cqrs.Command.ICommand<TResponse> command,
public ValueTask<TResponse> SendCommandAsync<TResponse>(Abstractions.Cqrs.Command.ICommand<TResponse> command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public TResponse SendCommand<TResponse>(global::GFramework.Core.Abstractions.Cqrs.Command.ICommand<TResponse> command)
public TResponse SendCommand<TResponse>(Abstractions.Cqrs.Command.ICommand<TResponse> command)
{
throw new NotImplementedException();
}

public ValueTask<TResponse> SendQueryAsync<TResponse>(
global::GFramework.Core.Abstractions.Cqrs.Query.IQuery<TResponse> query,
public ValueTask<TResponse> SendQueryAsync<TResponse>(Abstractions.Cqrs.Query.IQuery<TResponse> query,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public TResponse SendQuery<TResponse>(global::GFramework.Core.Abstractions.Cqrs.Query.IQuery<TResponse> query)
public TResponse SendQuery<TResponse>(Abstractions.Cqrs.Query.IQuery<TResponse> query)
{
throw new NotImplementedException();
}

public ValueTask PublishAsync<TNotification>(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : global::GFramework.Core.Abstractions.Cqrs.INotification
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}

public IAsyncEnumerable<TResponse> CreateStream<TResponse>(
global::GFramework.Core.Abstractions.Cqrs.IStreamRequest<TResponse> request,
IStreamRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public ValueTask SendAsync<TCommand>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : global::GFramework.Core.Abstractions.Cqrs.IRequest<global::GFramework.Core.Abstractions.Cqrs.Unit>
where TCommand : IRequest<Unit>
{
throw new NotImplementedException();
}

public ValueTask<TResponse> SendAsync<TResponse>(global::GFramework.Core.Abstractions.Cqrs.IRequest<TResponse> command,
public ValueTask<TResponse> SendAsync<TResponse>(IRequest<TResponse> command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
Expand All @@ -468,7 +466,7 @@ public void SendCommand(ICommand command)
/// <typeparam name="TResult">返回值类型</typeparam>
/// <param name="command">命令对象</param>
/// <returns>命令执行结果</returns>
public TResult SendCommand<TResult>(Abstractions.Command.ICommand<TResult> command)
public TResult SendCommand<TResult>(ICommand<TResult> command)
{
return default!;
}
Expand All @@ -489,7 +487,7 @@ public Task<TResult> SendCommandAsync<TResult>(IAsyncCommand<TResult> command)
/// <typeparam name="TResult">查询结果类型</typeparam>
/// <param name="query">查询对象</param>
/// <returns>查询结果</returns>
public TResult SendQuery<TResult>(Abstractions.Query.IQuery<TResult> query)
public TResult SendQuery<TResult>(IQuery<TResult> query)
{
return default!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
using GFramework.Core.Architectures;
using Microsoft.Extensions.DependencyInjection;

namespace GFramework.Core.Tests.Architectures;

Expand Down Expand Up @@ -181,11 +180,17 @@ T IArchitecture.RegisterUtility<T>(T utility)
throw new NotImplementedException();
}

public void RegisterMediatorBehavior<TBehavior>() where TBehavior : class
public void RegisterCqrsPipelineBehavior<TBehavior>() where TBehavior : class
{
throw new NotImplementedException();
}

[Obsolete("Use RegisterCqrsPipelineBehavior<TBehavior>() instead.")]
public void RegisterMediatorBehavior<TBehavior>() where TBehavior : class
{
RegisterCqrsPipelineBehavior<TBehavior>();
}

public IArchitectureModule InstallModule(IArchitectureModule module)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -306,11 +311,17 @@ T IArchitecture.RegisterUtility<T>(T utility)
throw new NotImplementedException();
}

public void RegisterMediatorBehavior<TBehavior>() where TBehavior : class
public void RegisterCqrsPipelineBehavior<TBehavior>() where TBehavior : class
{
throw new NotImplementedException();
}

[Obsolete("Use RegisterCqrsPipelineBehavior<TBehavior>() instead.")]
public void RegisterMediatorBehavior<TBehavior>() where TBehavior : class
{
RegisterCqrsPipelineBehavior<TBehavior>();
}

public IArchitectureModule InstallModule(IArchitectureModule module)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -363,4 +374,4 @@ public void Destroy()
public void RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
}
}
}
Loading
Loading