mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
refactor(command): 重构命令和查询抽象基类以支持输入参数分离
- 将 AbstractAsyncCommand 拆分为 AbstractAsyncCommand 和 AbstractAsyncCommandWithInput - 将 AbstractAsyncCommand<TInput, TResult> 移至新的 AbstractAsyncCommandWithResult 类 - 将 AbstractCommand 拆分为 AbstractCommand 和 AbstractCommandWithInput - 将 AbstractCommand<TInput, TResult> 移至新的 AbstractCommandWithResult 类 - 将 AbstractAsyncQuery 简化为不带输入参数的版本 - 将 AbstractQuery 简化为不带输入参数的版本 - 创建新的 AbstractAsyncQueryWithResult 类处理带输入参数的异步查询 - 创建新的 AbstractQueryWithResult 类处理带输入参数的同步查询
This commit is contained in:
parent
7e5036ae76
commit
21c5d1bc68
@ -4,50 +4,25 @@ using GFramework.Core.rule;
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
/// <summary>
|
||||
/// 抽象异步命令基类,用于处理无返回值的异步命令操作
|
||||
/// 异步命令的抽象基类,实现了IAsyncCommand接口
|
||||
/// 提供异步命令执行的基础框架和上下文感知功能
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">命令输入类型,必须实现ICommandInput接口</typeparam>
|
||||
public abstract class AbstractAsyncCommand<TInput>(TInput input) : ContextAwareBase, IAsyncCommand
|
||||
where TInput : ICommandInput
|
||||
public abstract class AbstractAsyncCommand : ContextAwareBase, IAsyncCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行异步命令的实现方法
|
||||
/// 该方法通过调用受保护的抽象方法OnExecuteAsync来执行具体的命令逻辑
|
||||
/// </summary>
|
||||
/// <returns>表示异步操作的任务</returns>
|
||||
async Task IAsyncCommand.ExecuteAsync()
|
||||
{
|
||||
await OnExecuteAsync(input);
|
||||
await OnExecuteAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑
|
||||
/// 子类必须实现的异步执行方法
|
||||
/// 包含具体的命令执行逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">命令输入参数</param>
|
||||
/// <returns>表示异步操作的任务</returns>
|
||||
protected abstract Task OnExecuteAsync(TInput input);
|
||||
protected abstract Task OnExecuteAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 抽象异步命令基类,用于处理有返回值的异步命令操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">命令输入类型,必须实现ICommandInput接口</typeparam>
|
||||
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||
public abstract class AbstractAsyncCommand<TInput, TResult>(TInput input) : ContextAwareBase, IAsyncCommand<TResult>
|
||||
where TInput : ICommandInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行异步命令并返回结果的实现方法
|
||||
/// </summary>
|
||||
/// <returns>表示异步操作且包含结果的任务</returns>
|
||||
async Task<TResult> IAsyncCommand<TResult>.ExecuteAsync()
|
||||
{
|
||||
return await OnExecuteAsync(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑并返回结果
|
||||
/// </summary>
|
||||
/// <param name="input">命令输入参数</param>
|
||||
/// <returns>表示异步操作且包含结果的任务</returns>
|
||||
protected abstract Task<TResult> OnExecuteAsync(TInput input);
|
||||
}
|
||||
28
GFramework.Core/command/AbstractAsyncCommandWithInput.cs
Normal file
28
GFramework.Core/command/AbstractAsyncCommandWithInput.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using GFramework.Core.Abstractions.command;
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
/// <summary>
|
||||
/// 抽象异步命令基类,用于处理无返回值的异步命令操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">命令输入类型,必须实现ICommandInput接口</typeparam>
|
||||
public abstract class AbstractAsyncCommand<TInput>(TInput input) : ContextAwareBase, IAsyncCommand
|
||||
where TInput : ICommandInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行异步命令的实现方法
|
||||
/// </summary>
|
||||
/// <returns>表示异步操作的任务</returns>
|
||||
async Task IAsyncCommand.ExecuteAsync()
|
||||
{
|
||||
await OnExecuteAsync(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">命令输入参数</param>
|
||||
/// <returns>表示异步操作的任务</returns>
|
||||
protected abstract Task OnExecuteAsync(TInput input);
|
||||
}
|
||||
29
GFramework.Core/command/AbstractAsyncCommandWithResult.cs
Normal file
29
GFramework.Core/command/AbstractAsyncCommandWithResult.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using GFramework.Core.Abstractions.command;
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
/// <summary>
|
||||
/// 抽象异步命令基类,用于处理有返回值的异步命令操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">命令输入类型,必须实现ICommandInput接口</typeparam>
|
||||
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||
public abstract class AbstractAsyncCommand<TInput, TResult>(TInput input) : ContextAwareBase, IAsyncCommand<TResult>
|
||||
where TInput : ICommandInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行异步命令并返回结果的实现方法
|
||||
/// </summary>
|
||||
/// <returns>表示异步操作且包含结果的任务</returns>
|
||||
async Task<TResult> IAsyncCommand<TResult>.ExecuteAsync()
|
||||
{
|
||||
return await OnExecuteAsync(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑并返回结果
|
||||
/// </summary>
|
||||
/// <param name="input">命令输入参数</param>
|
||||
/// <returns>表示异步操作且包含结果的任务</returns>
|
||||
protected abstract Task<TResult> OnExecuteAsync(TInput input);
|
||||
}
|
||||
@ -6,48 +6,18 @@ namespace GFramework.Core.command;
|
||||
/// <summary>
|
||||
/// 抽象命令类,实现 ICommand 接口,为具体命令提供基础架构支持
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">命令输入参数类型,必须实现 ICommandInput 接口</typeparam>
|
||||
/// <param name="input">命令执行所需的输入参数</param>
|
||||
public abstract class AbstractCommand<TInput>(TInput input) : ContextAwareBase, ICommand
|
||||
where TInput : ICommandInput
|
||||
public abstract class AbstractCommand : ContextAwareBase, ICommand
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行命令的入口方法,实现 ICommand 接口的 Execute 方法
|
||||
/// </summary>
|
||||
void ICommand.Execute()
|
||||
{
|
||||
OnExecute(input);
|
||||
OnExecute();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 命令执行的抽象方法,由派生类实现具体的命令逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">命令执行所需的输入参数</param>
|
||||
protected abstract void OnExecute(TInput input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 带返回值的抽象命令类,实现 ICommand{TResult} 接口,为需要返回结果的命令提供基础架构支持
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">命令输入参数类型,必须实现 ICommandInput 接口</typeparam>
|
||||
/// <typeparam name="TResult">命令执行后返回的结果类型</typeparam>
|
||||
/// <param name="input">命令执行所需的输入参数</param>
|
||||
public abstract class AbstractCommand<TInput, TResult>(TInput input) : ContextAwareBase, ICommand<TResult>
|
||||
where TInput : ICommandInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行命令的入口方法,实现 ICommand{TResult} 接口的 Execute 方法
|
||||
/// </summary>
|
||||
/// <returns>命令执行后的结果</returns>
|
||||
TResult ICommand<TResult>.Execute()
|
||||
{
|
||||
return OnExecute(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 命令执行的抽象方法,由派生类实现具体的命令逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">命令执行所需的输入参数</param>
|
||||
/// <returns>命令执行后的结果</returns>
|
||||
protected abstract TResult OnExecute(TInput input);
|
||||
protected abstract void OnExecute();
|
||||
}
|
||||
27
GFramework.Core/command/AbstractCommandWithInput.cs
Normal file
27
GFramework.Core/command/AbstractCommandWithInput.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using GFramework.Core.Abstractions.command;
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
/// <summary>
|
||||
/// 抽象命令类,实现 ICommand 接口,为具体命令提供基础架构支持
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">命令输入参数类型,必须实现 ICommandInput 接口</typeparam>
|
||||
/// <param name="input">命令执行所需的输入参数</param>
|
||||
public abstract class AbstractCommand<TInput>(TInput input) : ContextAwareBase, ICommand
|
||||
where TInput : ICommandInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行命令的入口方法,实现 ICommand 接口的 Execute 方法
|
||||
/// </summary>
|
||||
void ICommand.Execute()
|
||||
{
|
||||
OnExecute(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 命令执行的抽象方法,由派生类实现具体的命令逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">命令执行所需的输入参数</param>
|
||||
protected abstract void OnExecute(TInput input);
|
||||
}
|
||||
30
GFramework.Core/command/AbstractCommandWithResult.cs
Normal file
30
GFramework.Core/command/AbstractCommandWithResult.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using GFramework.Core.Abstractions.command;
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
/// <summary>
|
||||
/// 带返回值的抽象命令类,实现 ICommand{TResult} 接口,为需要返回结果的命令提供基础架构支持
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">命令输入参数类型,必须实现 ICommandInput 接口</typeparam>
|
||||
/// <typeparam name="TResult">命令执行后返回的结果类型</typeparam>
|
||||
/// <param name="input">命令执行所需的输入参数</param>
|
||||
public abstract class AbstractCommand<TInput, TResult>(TInput input) : ContextAwareBase, ICommand<TResult>
|
||||
where TInput : ICommandInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行命令的入口方法,实现 ICommand{TResult} 接口的 Execute 方法
|
||||
/// </summary>
|
||||
/// <returns>命令执行后的结果</returns>
|
||||
TResult ICommand<TResult>.Execute()
|
||||
{
|
||||
return OnExecute(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 命令执行的抽象方法,由派生类实现具体的命令逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">命令执行所需的输入参数</param>
|
||||
/// <returns>命令执行后的结果</returns>
|
||||
protected abstract TResult OnExecute(TInput input);
|
||||
}
|
||||
@ -4,29 +4,23 @@ using GFramework.Core.rule;
|
||||
namespace GFramework.Core.query;
|
||||
|
||||
/// <summary>
|
||||
/// 抽象异步查询基类,用于处理输入类型为TInput、结果类型为TResult的异步查询操作
|
||||
/// 异步查询抽象基类,提供异步查询的基本框架和执行机制
|
||||
/// 继承自ContextAwareBase并实现IAsyncQuery<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
|
||||
{
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
public abstract class AbstractAsyncQuery<TResult> : ContextAwareBase, IAsyncQuery<TResult> {
|
||||
/// <summary>
|
||||
/// 执行异步查询操作
|
||||
/// </summary>
|
||||
/// <returns>返回查询结果的异步任务</returns>
|
||||
public Task<TResult> DoAsync()
|
||||
{
|
||||
return OnDoAsync(input);
|
||||
return OnDoAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 抽象方法,用于实现具体的异步查询逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">查询输入参数</param>
|
||||
/// <returns>返回查询结果的异步任务</returns>
|
||||
protected abstract Task<TResult> OnDoAsync(TInput input);
|
||||
}
|
||||
protected abstract Task<TResult> OnDoAsync();
|
||||
}
|
||||
|
||||
32
GFramework.Core/query/AbstractAsyncQueryWithResult.cs
Normal file
32
GFramework.Core/query/AbstractAsyncQueryWithResult.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);
|
||||
}
|
||||
@ -6,10 +6,9 @@ namespace GFramework.Core.query;
|
||||
/// <summary>
|
||||
/// 抽象查询类,提供查询操作的基础实现
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">查询输入参数的类型,必须实现IQueryInput接口</typeparam>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
public abstract class AbstractQuery<TInput, TResult>(TInput input) : ContextAwareBase, IQuery<TResult>
|
||||
where TInput : IQueryInput
|
||||
public abstract class AbstractQuery<TResult> : ContextAwareBase, IQuery<TResult>
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行查询操作
|
||||
@ -17,13 +16,13 @@ public abstract class AbstractQuery<TInput, TResult>(TInput input) : ContextAwar
|
||||
/// <returns>查询结果,类型为TResult</returns>
|
||||
public TResult Do()
|
||||
{
|
||||
return OnDo(input);
|
||||
// 调用抽象方法执行具体的查询逻辑
|
||||
return OnDo();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 抽象方法,用于实现具体的查询逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">查询输入参数</param>
|
||||
/// <returns>查询结果,类型为TResult</returns>
|
||||
protected abstract TResult OnDo(TInput input);
|
||||
}
|
||||
protected abstract TResult OnDo();
|
||||
}
|
||||
|
||||
29
GFramework.Core/query/AbstractQueryWithResult.cs
Normal file
29
GFramework.Core/query/AbstractQueryWithResult.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using GFramework.Core.Abstractions.query;
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.query;
|
||||
|
||||
/// <summary>
|
||||
/// 抽象查询类,提供查询操作的基础实现
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">查询输入参数的类型,必须实现IQueryInput接口</typeparam>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
public abstract class AbstractQuery<TInput, TResult>(TInput input) : ContextAwareBase, IQuery<TResult>
|
||||
where TInput : IQueryInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行查询操作
|
||||
/// </summary>
|
||||
/// <returns>查询结果,类型为TResult</returns>
|
||||
public TResult Do()
|
||||
{
|
||||
return OnDo(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 抽象方法,用于实现具体的查询逻辑
|
||||
/// </summary>
|
||||
/// <param name="input">查询输入参数</param>
|
||||
/// <returns>查询结果,类型为TResult</returns>
|
||||
protected abstract TResult OnDo(TInput input);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user