mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-26 06:16:43 +08:00
feat(core): 添加异步查询系统并优化命令系统实现
- 新增 AbstractAsyncQuery 基类支持异步查询操作 - 实现 AsyncQueryBus 和 IAsyncQueryBus 查询总线功能 - 添加 IAsyncQuery 接口定义异步查询契约 - 重构 CommandBus 的 SendAsync 方法移除不必要的 await - 为 AbstractAsyncCommand 添加完整 XML 文档注释 - 更新 TEST_COVERAGE_PLAN.md 反映测试覆盖率提升至 91.5%
This commit is contained in:
parent
1fb1334d88
commit
42ddb80248
16
GFramework.Core.Abstractions/query/IAsyncQuery.cs
Normal file
16
GFramework.Core.Abstractions/query/IAsyncQuery.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace GFramework.Core.Abstractions.query;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步查询接口,定义了执行异步查询操作的方法
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||||
|
public interface IAsyncQuery<TResult>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 执行异步查询操作
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>返回查询结果的Task,结果类型为TResult</returns>
|
||||||
|
Task<TResult> DoAsync();
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -3,24 +3,51 @@ using GFramework.Core.rule;
|
|||||||
|
|
||||||
namespace GFramework.Core.command;
|
namespace GFramework.Core.command;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 抽象异步命令基类,用于处理无返回值的异步命令操作
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TInput">命令输入类型,必须实现ICommandInput接口</typeparam>
|
||||||
public abstract class AbstractAsyncCommand<TInput>(TInput input) : ContextAwareBase, IAsyncCommand
|
public abstract class AbstractAsyncCommand<TInput>(TInput input) : ContextAwareBase, IAsyncCommand
|
||||||
where TInput : ICommandInput
|
where TInput : ICommandInput
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 执行异步命令的实现方法
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>表示异步操作的任务</returns>
|
||||||
async Task IAsyncCommand.ExecuteAsync()
|
async Task IAsyncCommand.ExecuteAsync()
|
||||||
{
|
{
|
||||||
await OnExecuteAsync(input);
|
await OnExecuteAsync(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">命令输入参数</param>
|
||||||
|
/// <returns>表示异步操作的任务</returns>
|
||||||
protected abstract Task OnExecuteAsync(TInput input);
|
protected abstract Task OnExecuteAsync(TInput input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 抽象异步命令基类,用于处理有返回值的异步命令操作
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TInput">命令输入类型,必须实现ICommandInput接口</typeparam>
|
||||||
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||||
public abstract class AbstractAsyncCommand<TInput, TResult>(TInput input) : ContextAwareBase, IAsyncCommand<TResult>
|
public abstract class AbstractAsyncCommand<TInput, TResult>(TInput input) : ContextAwareBase, IAsyncCommand<TResult>
|
||||||
where TInput : ICommandInput
|
where TInput : ICommandInput
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 执行异步命令并返回结果的实现方法
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>表示异步操作且包含结果的任务</returns>
|
||||||
async Task<TResult> IAsyncCommand<TResult>.ExecuteAsync()
|
async Task<TResult> IAsyncCommand<TResult>.ExecuteAsync()
|
||||||
{
|
{
|
||||||
return await OnExecuteAsync(input);
|
return await OnExecuteAsync(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑并返回结果
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">命令输入参数</param>
|
||||||
|
/// <returns>表示异步操作且包含结果的任务</returns>
|
||||||
protected abstract Task<TResult> OnExecuteAsync(TInput input);
|
protected abstract Task<TResult> OnExecuteAsync(TInput input);
|
||||||
}
|
}
|
||||||
@ -39,11 +39,11 @@ public sealed class CommandBus : ICommandBus
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="command">要执行的命令对象,不能为空</param>
|
/// <param name="command">要执行的命令对象,不能为空</param>
|
||||||
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
|
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
|
||||||
public async Task SendAsync(IAsyncCommand command)
|
public Task SendAsync(IAsyncCommand command)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
await command.ExecuteAsync();
|
return command.ExecuteAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -53,10 +53,10 @@ public sealed class CommandBus : ICommandBus
|
|||||||
/// <param name="command">要执行的命令对象,不能为空</param>
|
/// <param name="command">要执行的命令对象,不能为空</param>
|
||||||
/// <returns>命令执行的结果</returns>
|
/// <returns>命令执行的结果</returns>
|
||||||
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
|
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
|
||||||
public async Task<TResult> SendAsync<TResult>(IAsyncCommand<TResult> command)
|
public Task<TResult> SendAsync<TResult>(IAsyncCommand<TResult> command)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
return await command.ExecuteAsync();
|
return command.ExecuteAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
32
GFramework.Core/query/AbstractAsyncQuery.cs
Normal file
32
GFramework.Core/query/AbstractAsyncQuery.cs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
using GFramework.Core.Abstractions.query;
|
||||||
|
using GFramework.Core.rule;
|
||||||
|
|
||||||
|
namespace GFramework.Core.query;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 抽象异步查询基类,用于处理输入类型为TInput、结果类型为TResult的异步查询操作
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TInput">查询输入类型,必须实现IQueryInput接口</typeparam>
|
||||||
|
/// <typeparam name="TResult">查询结果类型</typeparam>
|
||||||
|
/// <param name="input">查询输入参数</param>
|
||||||
|
public abstract class AbstractAsyncQuery<TInput, TResult>(
|
||||||
|
TInput input
|
||||||
|
) : ContextAwareBase, IAsyncQuery<TResult>
|
||||||
|
where TInput : IQueryInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 执行异步查询操作
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>返回查询结果的异步任务</returns>
|
||||||
|
public Task<TResult> DoAsync()
|
||||||
|
{
|
||||||
|
return OnDoAsync(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 抽象方法,用于实现具体的异步查询逻辑
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">查询输入参数</param>
|
||||||
|
/// <returns>返回查询结果的异步任务</returns>
|
||||||
|
protected abstract Task<TResult> OnDoAsync(TInput input);
|
||||||
|
}
|
||||||
22
GFramework.Core/query/AsyncQueryBus.cs
Normal file
22
GFramework.Core/query/AsyncQueryBus.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using GFramework.Core.Abstractions.query;
|
||||||
|
|
||||||
|
namespace GFramework.Core.query;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步查询总线实现,用于处理异步查询请求
|
||||||
|
/// </summary>
|
||||||
|
public sealed class AsyncQueryBus : IAsyncQueryBus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 异步发送查询请求并返回结果
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">查询结果类型</typeparam>
|
||||||
|
/// <param name="query">要执行的异步查询对象</param>
|
||||||
|
/// <returns>包含查询结果的异步任务</returns>
|
||||||
|
public Task<TResult> SendAsync<TResult>(IAsyncQuery<TResult> query)
|
||||||
|
{
|
||||||
|
// 验证查询参数不为空
|
||||||
|
ArgumentNullException.ThrowIfNull(query);
|
||||||
|
return query.DoAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
17
GFramework.Core/query/IAsyncQueryBus.cs
Normal file
17
GFramework.Core/query/IAsyncQueryBus.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using GFramework.Core.Abstractions.query;
|
||||||
|
|
||||||
|
namespace GFramework.Core.query;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步查询总线接口,用于处理异步查询请求
|
||||||
|
/// </summary>
|
||||||
|
public interface IAsyncQueryBus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 异步发送查询请求并返回结果
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||||
|
/// <param name="query">要执行的异步查询对象</param>
|
||||||
|
/// <returns>表示异步操作的任务,任务结果为查询结果</returns>
|
||||||
|
Task<TResult> SendAsync<TResult>(IAsyncQuery<TResult> query);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user