From 21c5d1bc6880d9df9e6cca97c7f671c0bbc2d207 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Mon, 26 Jan 2026 13:03:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor(command):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=92=8C=E6=9F=A5=E8=AF=A2=E6=8A=BD=E8=B1=A1?= =?UTF-8?q?=E5=9F=BA=E7=B1=BB=E4=BB=A5=E6=94=AF=E6=8C=81=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 AbstractAsyncCommand 拆分为 AbstractAsyncCommand 和 AbstractAsyncCommandWithInput - 将 AbstractAsyncCommand 移至新的 AbstractAsyncCommandWithResult 类 - 将 AbstractCommand 拆分为 AbstractCommand 和 AbstractCommandWithInput - 将 AbstractCommand 移至新的 AbstractCommandWithResult 类 - 将 AbstractAsyncQuery 简化为不带输入参数的版本 - 将 AbstractQuery 简化为不带输入参数的版本 - 创建新的 AbstractAsyncQueryWithResult 类处理带输入参数的异步查询 - 创建新的 AbstractQueryWithResult 类处理带输入参数的同步查询 --- .../command/AbstractAsyncCommand.cs | 41 ++++--------------- .../command/AbstractAsyncCommandWithInput.cs | 28 +++++++++++++ .../command/AbstractAsyncCommandWithResult.cs | 29 +++++++++++++ GFramework.Core/command/AbstractCommand.cs | 36 ++-------------- .../command/AbstractCommandWithInput.cs | 27 ++++++++++++ .../command/AbstractCommandWithResult.cs | 30 ++++++++++++++ GFramework.Core/query/AbstractAsyncQuery.cs | 20 ++++----- .../query/AbstractAsyncQueryWithResult.cs | 32 +++++++++++++++ GFramework.Core/query/AbstractQuery.cs | 13 +++--- .../query/AbstractQueryWithResult.cs | 29 +++++++++++++ 10 files changed, 199 insertions(+), 86 deletions(-) create mode 100644 GFramework.Core/command/AbstractAsyncCommandWithInput.cs create mode 100644 GFramework.Core/command/AbstractAsyncCommandWithResult.cs create mode 100644 GFramework.Core/command/AbstractCommandWithInput.cs create mode 100644 GFramework.Core/command/AbstractCommandWithResult.cs create mode 100644 GFramework.Core/query/AbstractAsyncQueryWithResult.cs create mode 100644 GFramework.Core/query/AbstractQueryWithResult.cs diff --git a/GFramework.Core/command/AbstractAsyncCommand.cs b/GFramework.Core/command/AbstractAsyncCommand.cs index 80768d4..74876d7 100644 --- a/GFramework.Core/command/AbstractAsyncCommand.cs +++ b/GFramework.Core/command/AbstractAsyncCommand.cs @@ -4,50 +4,25 @@ using GFramework.Core.rule; namespace GFramework.Core.command; /// -/// 抽象异步命令基类,用于处理无返回值的异步命令操作 +/// 异步命令的抽象基类,实现了IAsyncCommand接口 +/// 提供异步命令执行的基础框架和上下文感知功能 /// -/// 命令输入类型,必须实现ICommandInput接口 -public abstract class AbstractAsyncCommand(TInput input) : ContextAwareBase, IAsyncCommand - where TInput : ICommandInput +public abstract class AbstractAsyncCommand : ContextAwareBase, IAsyncCommand { /// /// 执行异步命令的实现方法 + /// 该方法通过调用受保护的抽象方法OnExecuteAsync来执行具体的命令逻辑 /// /// 表示异步操作的任务 async Task IAsyncCommand.ExecuteAsync() { - await OnExecuteAsync(input); + await OnExecuteAsync(); } /// - /// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑 + /// 子类必须实现的异步执行方法 + /// 包含具体的命令执行逻辑 /// - /// 命令输入参数 /// 表示异步操作的任务 - protected abstract Task OnExecuteAsync(TInput input); + protected abstract Task OnExecuteAsync(); } - -/// -/// 抽象异步命令基类,用于处理有返回值的异步命令操作 -/// -/// 命令输入类型,必须实现ICommandInput接口 -/// 命令执行结果类型 -public abstract class AbstractAsyncCommand(TInput input) : ContextAwareBase, IAsyncCommand - where TInput : ICommandInput -{ - /// - /// 执行异步命令并返回结果的实现方法 - /// - /// 表示异步操作且包含结果的任务 - async Task IAsyncCommand.ExecuteAsync() - { - return await OnExecuteAsync(input); - } - - /// - /// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑并返回结果 - /// - /// 命令输入参数 - /// 表示异步操作且包含结果的任务 - protected abstract Task OnExecuteAsync(TInput input); -} \ No newline at end of file diff --git a/GFramework.Core/command/AbstractAsyncCommandWithInput.cs b/GFramework.Core/command/AbstractAsyncCommandWithInput.cs new file mode 100644 index 0000000..c7cbabc --- /dev/null +++ b/GFramework.Core/command/AbstractAsyncCommandWithInput.cs @@ -0,0 +1,28 @@ +using GFramework.Core.Abstractions.command; +using GFramework.Core.rule; + +namespace GFramework.Core.command; + +/// +/// 抽象异步命令基类,用于处理无返回值的异步命令操作 +/// +/// 命令输入类型,必须实现ICommandInput接口 +public abstract class AbstractAsyncCommand(TInput input) : ContextAwareBase, IAsyncCommand + where TInput : ICommandInput +{ + /// + /// 执行异步命令的实现方法 + /// + /// 表示异步操作的任务 + async Task IAsyncCommand.ExecuteAsync() + { + await OnExecuteAsync(input); + } + + /// + /// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑 + /// + /// 命令输入参数 + /// 表示异步操作的任务 + protected abstract Task OnExecuteAsync(TInput input); +} \ No newline at end of file diff --git a/GFramework.Core/command/AbstractAsyncCommandWithResult.cs b/GFramework.Core/command/AbstractAsyncCommandWithResult.cs new file mode 100644 index 0000000..d599b2e --- /dev/null +++ b/GFramework.Core/command/AbstractAsyncCommandWithResult.cs @@ -0,0 +1,29 @@ +using GFramework.Core.Abstractions.command; +using GFramework.Core.rule; + +namespace GFramework.Core.command; + +/// +/// 抽象异步命令基类,用于处理有返回值的异步命令操作 +/// +/// 命令输入类型,必须实现ICommandInput接口 +/// 命令执行结果类型 +public abstract class AbstractAsyncCommand(TInput input) : ContextAwareBase, IAsyncCommand + where TInput : ICommandInput +{ + /// + /// 执行异步命令并返回结果的实现方法 + /// + /// 表示异步操作且包含结果的任务 + async Task IAsyncCommand.ExecuteAsync() + { + return await OnExecuteAsync(input); + } + + /// + /// 定义异步执行逻辑的抽象方法,由派生类实现具体业务逻辑并返回结果 + /// + /// 命令输入参数 + /// 表示异步操作且包含结果的任务 + protected abstract Task OnExecuteAsync(TInput input); +} \ No newline at end of file diff --git a/GFramework.Core/command/AbstractCommand.cs b/GFramework.Core/command/AbstractCommand.cs index a5fa48b..45c1488 100644 --- a/GFramework.Core/command/AbstractCommand.cs +++ b/GFramework.Core/command/AbstractCommand.cs @@ -6,48 +6,18 @@ namespace GFramework.Core.command; /// /// 抽象命令类,实现 ICommand 接口,为具体命令提供基础架构支持 /// -/// 命令输入参数类型,必须实现 ICommandInput 接口 -/// 命令执行所需的输入参数 -public abstract class AbstractCommand(TInput input) : ContextAwareBase, ICommand - where TInput : ICommandInput +public abstract class AbstractCommand : ContextAwareBase, ICommand { /// /// 执行命令的入口方法,实现 ICommand 接口的 Execute 方法 /// void ICommand.Execute() { - OnExecute(input); + OnExecute(); } /// /// 命令执行的抽象方法,由派生类实现具体的命令逻辑 /// - /// 命令执行所需的输入参数 - protected abstract void OnExecute(TInput input); -} - -/// -/// 带返回值的抽象命令类,实现 ICommand{TResult} 接口,为需要返回结果的命令提供基础架构支持 -/// -/// 命令输入参数类型,必须实现 ICommandInput 接口 -/// 命令执行后返回的结果类型 -/// 命令执行所需的输入参数 -public abstract class AbstractCommand(TInput input) : ContextAwareBase, ICommand - where TInput : ICommandInput -{ - /// - /// 执行命令的入口方法,实现 ICommand{TResult} 接口的 Execute 方法 - /// - /// 命令执行后的结果 - TResult ICommand.Execute() - { - return OnExecute(input); - } - - /// - /// 命令执行的抽象方法,由派生类实现具体的命令逻辑 - /// - /// 命令执行所需的输入参数 - /// 命令执行后的结果 - protected abstract TResult OnExecute(TInput input); + protected abstract void OnExecute(); } \ No newline at end of file diff --git a/GFramework.Core/command/AbstractCommandWithInput.cs b/GFramework.Core/command/AbstractCommandWithInput.cs new file mode 100644 index 0000000..01de19b --- /dev/null +++ b/GFramework.Core/command/AbstractCommandWithInput.cs @@ -0,0 +1,27 @@ +using GFramework.Core.Abstractions.command; +using GFramework.Core.rule; + +namespace GFramework.Core.command; + +/// +/// 抽象命令类,实现 ICommand 接口,为具体命令提供基础架构支持 +/// +/// 命令输入参数类型,必须实现 ICommandInput 接口 +/// 命令执行所需的输入参数 +public abstract class AbstractCommand(TInput input) : ContextAwareBase, ICommand + where TInput : ICommandInput +{ + /// + /// 执行命令的入口方法,实现 ICommand 接口的 Execute 方法 + /// + void ICommand.Execute() + { + OnExecute(input); + } + + /// + /// 命令执行的抽象方法,由派生类实现具体的命令逻辑 + /// + /// 命令执行所需的输入参数 + protected abstract void OnExecute(TInput input); +} \ No newline at end of file diff --git a/GFramework.Core/command/AbstractCommandWithResult.cs b/GFramework.Core/command/AbstractCommandWithResult.cs new file mode 100644 index 0000000..da49177 --- /dev/null +++ b/GFramework.Core/command/AbstractCommandWithResult.cs @@ -0,0 +1,30 @@ +using GFramework.Core.Abstractions.command; +using GFramework.Core.rule; + +namespace GFramework.Core.command; + +/// +/// 带返回值的抽象命令类,实现 ICommand{TResult} 接口,为需要返回结果的命令提供基础架构支持 +/// +/// 命令输入参数类型,必须实现 ICommandInput 接口 +/// 命令执行后返回的结果类型 +/// 命令执行所需的输入参数 +public abstract class AbstractCommand(TInput input) : ContextAwareBase, ICommand + where TInput : ICommandInput +{ + /// + /// 执行命令的入口方法,实现 ICommand{TResult} 接口的 Execute 方法 + /// + /// 命令执行后的结果 + TResult ICommand.Execute() + { + return OnExecute(input); + } + + /// + /// 命令执行的抽象方法,由派生类实现具体的命令逻辑 + /// + /// 命令执行所需的输入参数 + /// 命令执行后的结果 + protected abstract TResult OnExecute(TInput input); +} \ No newline at end of file diff --git a/GFramework.Core/query/AbstractAsyncQuery.cs b/GFramework.Core/query/AbstractAsyncQuery.cs index 0e04c57..a239d7d 100644 --- a/GFramework.Core/query/AbstractAsyncQuery.cs +++ b/GFramework.Core/query/AbstractAsyncQuery.cs @@ -4,29 +4,23 @@ using GFramework.Core.rule; namespace GFramework.Core.query; /// -/// 抽象异步查询基类,用于处理输入类型为TInput、结果类型为TResult的异步查询操作 +/// 异步查询抽象基类,提供异步查询的基本框架和执行机制 +/// 继承自ContextAwareBase并实现IAsyncQuery<TResult>接口 /// -/// 查询输入类型,必须实现IQueryInput接口 -/// 查询结果类型 -/// 查询输入参数 -public abstract class AbstractAsyncQuery( - TInput input -) : ContextAwareBase, IAsyncQuery - where TInput : IQueryInput -{ +/// 查询结果的类型 +public abstract class AbstractAsyncQuery : ContextAwareBase, IAsyncQuery { /// /// 执行异步查询操作 /// /// 返回查询结果的异步任务 public Task DoAsync() { - return OnDoAsync(input); + return OnDoAsync(); } /// /// 抽象方法,用于实现具体的异步查询逻辑 /// - /// 查询输入参数 /// 返回查询结果的异步任务 - protected abstract Task OnDoAsync(TInput input); -} \ No newline at end of file + protected abstract Task OnDoAsync(); +} diff --git a/GFramework.Core/query/AbstractAsyncQueryWithResult.cs b/GFramework.Core/query/AbstractAsyncQueryWithResult.cs new file mode 100644 index 0000000..0e04c57 --- /dev/null +++ b/GFramework.Core/query/AbstractAsyncQueryWithResult.cs @@ -0,0 +1,32 @@ +using GFramework.Core.Abstractions.query; +using GFramework.Core.rule; + +namespace GFramework.Core.query; + +/// +/// 抽象异步查询基类,用于处理输入类型为TInput、结果类型为TResult的异步查询操作 +/// +/// 查询输入类型,必须实现IQueryInput接口 +/// 查询结果类型 +/// 查询输入参数 +public abstract class AbstractAsyncQuery( + TInput input +) : ContextAwareBase, IAsyncQuery + where TInput : IQueryInput +{ + /// + /// 执行异步查询操作 + /// + /// 返回查询结果的异步任务 + public Task DoAsync() + { + return OnDoAsync(input); + } + + /// + /// 抽象方法,用于实现具体的异步查询逻辑 + /// + /// 查询输入参数 + /// 返回查询结果的异步任务 + protected abstract Task OnDoAsync(TInput input); +} \ No newline at end of file diff --git a/GFramework.Core/query/AbstractQuery.cs b/GFramework.Core/query/AbstractQuery.cs index fba9bba..274f03c 100644 --- a/GFramework.Core/query/AbstractQuery.cs +++ b/GFramework.Core/query/AbstractQuery.cs @@ -6,10 +6,9 @@ namespace GFramework.Core.query; /// /// 抽象查询类,提供查询操作的基础实现 /// -/// 查询输入参数的类型,必须实现IQueryInput接口 /// 查询结果的类型 -public abstract class AbstractQuery(TInput input) : ContextAwareBase, IQuery - where TInput : IQueryInput +public abstract class AbstractQuery : ContextAwareBase, IQuery + { /// /// 执行查询操作 @@ -17,13 +16,13 @@ public abstract class AbstractQuery(TInput input) : ContextAwar /// 查询结果,类型为TResult public TResult Do() { - return OnDo(input); + // 调用抽象方法执行具体的查询逻辑 + return OnDo(); } /// /// 抽象方法,用于实现具体的查询逻辑 /// - /// 查询输入参数 /// 查询结果,类型为TResult - protected abstract TResult OnDo(TInput input); -} \ No newline at end of file + protected abstract TResult OnDo(); +} diff --git a/GFramework.Core/query/AbstractQueryWithResult.cs b/GFramework.Core/query/AbstractQueryWithResult.cs new file mode 100644 index 0000000..1f30940 --- /dev/null +++ b/GFramework.Core/query/AbstractQueryWithResult.cs @@ -0,0 +1,29 @@ +using GFramework.Core.Abstractions.query; +using GFramework.Core.rule; + +namespace GFramework.Core.query; + +/// +/// 抽象查询类,提供查询操作的基础实现 +/// +/// 查询输入参数的类型,必须实现IQueryInput接口 +/// 查询结果的类型 +public abstract class AbstractQuery(TInput input) : ContextAwareBase, IQuery + where TInput : IQueryInput +{ + /// + /// 执行查询操作 + /// + /// 查询结果,类型为TResult + public TResult Do() + { + return OnDo(input); + } + + /// + /// 抽象方法,用于实现具体的查询逻辑 + /// + /// 查询输入参数 + /// 查询结果,类型为TResult + protected abstract TResult OnDo(TInput input); +} \ No newline at end of file