From e7285b3426cbacebaa7434eb38fad6d8ffc13b2d Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sat, 17 Jan 2026 16:03:00 +0800
Subject: [PATCH] =?UTF-8?q?feat(core):=20=E6=B7=BB=E5=8A=A0=E5=BC=82?=
=?UTF-8?q?=E6=AD=A5=E5=91=BD=E4=BB=A4=E6=94=AF=E6=8C=81=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 在 ArchitectureContext 中新增 SendCommandAsync 方法支持异步命令执行
- 在 CommandBus 中实现 SendAsync 方法处理异步命令的发送和执行
- 在 ContextAwareExtensions 中扩展 SendCommandAsync 扩展方法
- 更新 IArchitectureContext 接口定义异步命令方法契约
- 更新 ICommandBus 接口定义异步命令执行方法
- 新增 AbstractAsyncCommand 抽象类提供异步命令基类实现
- 定义 IAsyncCommand 接口规范异步命令的行为 contract
---
.../architecture/IArchitectureContext.cs | 17 +++++-
.../command/IAsyncCommand.cs | 14 +++++
.../command/ICommandBus.cs | 18 ++++++-
.../architecture/ArchitectureContext.cs | 24 ++++++++-
.../command/AbstractAsyncCommand.cs | 26 ++++++++++
GFramework.Core/command/CommandBus.cs | 29 ++++++++++-
.../extensions/ContextAwareExtensions.cs | 52 ++++++++++++++++++-
7 files changed, 175 insertions(+), 5 deletions(-)
create mode 100644 GFramework.Core.Abstractions/command/IAsyncCommand.cs
create mode 100644 GFramework.Core/command/AbstractAsyncCommand.cs
diff --git a/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs b/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs
index d02c877..87c43bc 100644
--- a/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs
+++ b/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs
@@ -1,4 +1,5 @@
-using System;
+using System;
+using System.Threading.Tasks;
using GFramework.Core.Abstractions.command;
using GFramework.Core.Abstractions.environment;
using GFramework.Core.Abstractions.events;
@@ -49,6 +50,20 @@ public interface IArchitectureContext
/// 命令执行结果
TResult SendCommand(ICommand command);
+ ///
+ /// 发送并异步执行一个命令
+ ///
+ /// 要发送的命令
+ Task SendCommandAsync(IAsyncCommand command);
+
+ ///
+ /// 发送并异步执行一个带返回值的命令
+ ///
+ /// 命令执行结果类型
+ /// 要发送的命令
+ /// 命令执行结果
+ Task SendCommandAsync(IAsyncCommand command);
+
///
/// 发送一个查询请求
///
diff --git a/GFramework.Core.Abstractions/command/IAsyncCommand.cs b/GFramework.Core.Abstractions/command/IAsyncCommand.cs
new file mode 100644
index 0000000..72366bb
--- /dev/null
+++ b/GFramework.Core.Abstractions/command/IAsyncCommand.cs
@@ -0,0 +1,14 @@
+using System.Threading.Tasks;
+using GFramework.Core.Abstractions.rule;
+
+namespace GFramework.Core.Abstractions.command;
+
+public interface IAsyncCommand : IContextAware
+{
+ Task ExecuteAsync();
+}
+
+public interface IAsyncCommand : IContextAware
+{
+ Task ExecuteAsync();
+}
\ No newline at end of file
diff --git a/GFramework.Core.Abstractions/command/ICommandBus.cs b/GFramework.Core.Abstractions/command/ICommandBus.cs
index d843005..15a3568 100644
--- a/GFramework.Core.Abstractions/command/ICommandBus.cs
+++ b/GFramework.Core.Abstractions/command/ICommandBus.cs
@@ -1,4 +1,6 @@
-namespace GFramework.Core.Abstractions.command;
+using System.Threading.Tasks;
+
+namespace GFramework.Core.Abstractions.command;
///
/// 定义命令总线接口,用于执行各种命令
@@ -18,4 +20,18 @@ public interface ICommandBus
/// 要执行的带返回值的命令对象
/// 命令执行的结果
public TResult Send(ICommand command);
+
+ ///
+ /// 发送并异步执行一个命令
+ ///
+ /// 要执行的命令对象
+ Task SendAsync(IAsyncCommand command);
+
+ ///
+ /// 发送并异步执行一个带返回值的命令
+ ///
+ /// 命令执行结果的类型
+ /// 要执行的带返回值的命令对象
+ /// 命令执行的结果
+ Task SendAsync(IAsyncCommand command);
}
\ No newline at end of file
diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs
index a394717..c4031b6 100644
--- a/GFramework.Core/architecture/ArchitectureContext.cs
+++ b/GFramework.Core/architecture/ArchitectureContext.cs
@@ -1,4 +1,4 @@
-using GFramework.Core.Abstractions.architecture;
+using GFramework.Core.Abstractions.architecture;
using GFramework.Core.Abstractions.command;
using GFramework.Core.Abstractions.environment;
using GFramework.Core.Abstractions.events;
@@ -104,6 +104,28 @@ public class ArchitectureContext(
return _commandBus.Send(command);
}
+ ///
+ /// 发送并异步执行一个命令请求
+ ///
+ /// 要发送的命令
+ public async Task SendCommandAsync(IAsyncCommand command)
+ {
+ ArgumentNullException.ThrowIfNull(command);
+ await _commandBus.SendAsync(command);
+ }
+
+ ///
+ /// 发送并异步执行一个带返回值的命令请求
+ ///
+ /// 命令执行结果类型
+ /// 要发送的命令
+ /// 命令执行结果
+ public async Task SendCommandAsync(IAsyncCommand command)
+ {
+ ArgumentNullException.ThrowIfNull(command);
+ return await _commandBus.SendAsync(command);
+ }
+
#endregion
#region Event Management
diff --git a/GFramework.Core/command/AbstractAsyncCommand.cs b/GFramework.Core/command/AbstractAsyncCommand.cs
new file mode 100644
index 0000000..367b13f
--- /dev/null
+++ b/GFramework.Core/command/AbstractAsyncCommand.cs
@@ -0,0 +1,26 @@
+using GFramework.Core.Abstractions.command;
+using GFramework.Core.rule;
+
+namespace GFramework.Core.command;
+
+public abstract class AbstractAsyncCommand(TInput input) : ContextAwareBase, IAsyncCommand
+ where TInput : ICommandInput
+{
+ async Task IAsyncCommand.ExecuteAsync()
+ {
+ await OnExecuteAsync(input);
+ }
+
+ protected abstract Task OnExecuteAsync(TInput input);
+}
+
+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/CommandBus.cs b/GFramework.Core/command/CommandBus.cs
index b0edb12..fd6c9b9 100644
--- a/GFramework.Core/command/CommandBus.cs
+++ b/GFramework.Core/command/CommandBus.cs
@@ -1,4 +1,5 @@
-using GFramework.Core.Abstractions.command;
+using GFramework.Core.Abstractions.command;
+using IAsyncCommand = GFramework.Core.Abstractions.command.IAsyncCommand;
namespace GFramework.Core.command;
@@ -32,4 +33,30 @@ public sealed class CommandBus : ICommandBus
return command.Execute();
}
+
+ ///
+ /// 发送并异步执行无返回值的命令
+ ///
+ /// 要执行的命令对象,不能为空
+ /// 当command参数为null时抛出
+ public async Task SendAsync(IAsyncCommand command)
+ {
+ ArgumentNullException.ThrowIfNull(command);
+
+ await command.ExecuteAsync();
+ }
+
+ ///
+ /// 发送并异步执行有返回值的命令
+ ///
+ /// 命令执行结果的类型
+ /// 要执行的命令对象,不能为空
+ /// 命令执行的结果
+ /// 当command参数为null时抛出
+ public async Task SendAsync(IAsyncCommand command)
+ {
+ ArgumentNullException.ThrowIfNull(command);
+
+ return await command.ExecuteAsync();
+ }
}
\ No newline at end of file
diff --git a/GFramework.Core/extensions/ContextAwareExtensions.cs b/GFramework.Core/extensions/ContextAwareExtensions.cs
index 916a41e..485835d 100644
--- a/GFramework.Core/extensions/ContextAwareExtensions.cs
+++ b/GFramework.Core/extensions/ContextAwareExtensions.cs
@@ -1,4 +1,4 @@
-using GFramework.Core.Abstractions.command;
+using GFramework.Core.Abstractions.command;
using GFramework.Core.Abstractions.environment;
using GFramework.Core.Abstractions.events;
using GFramework.Core.Abstractions.model;
@@ -101,6 +101,56 @@ public static class ContextAwareExtensions
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(command);
+ var Context = contextAware.GetContext();
+ return Context.SendCommand(command);
+ }
+
+ ///
+ /// 发送并异步执行一个无返回值的命令
+ ///
+ /// 实现 IContextAware 接口的对象
+ /// 要发送的命令
+ /// 当 contextAware 或 command 为 null 时抛出
+ public static async Task SendCommandAsync(this IContextAware contextAware, IAsyncCommand command)
+ {
+ ArgumentNullException.ThrowIfNull(contextAware);
+ ArgumentNullException.ThrowIfNull(command);
+
+ var Context = contextAware.GetContext();
+ await Context.SendCommandAsync(command);
+ }
+
+ ///
+ /// 发送并异步执行一个带返回值的命令
+ ///
+ /// 命令执行结果类型
+ /// 实现 IContextAware 接口的对象
+ /// 要发送的命令
+ /// 命令执行结果
+ /// 当 contextAware 或 command 为 null 时抛出
+ public static async Task SendCommandAsync(this IContextAware contextAware,
+ IAsyncCommand command)
+ {
+ ArgumentNullException.ThrowIfNull(contextAware);
+ ArgumentNullException.ThrowIfNull(command);
+
+ var Context = contextAware.GetContext();
+ return await Context.SendCommandAsync(command);
+ }
+
+ ///
+ /// 发送一个事件
+ ///
+ /// 命令执行结果类型
+ /// 实现 IContextAware 接口的对象
+ /// 要发送的命令
+ /// 命令执行结果
+ /// 当 contextAware 或 command 为 null 时抛出
+ public static TResult SendCommand(this IContextAware contextAware, ICommand command)
+ {
+ ArgumentNullException.ThrowIfNull(contextAware);
+ ArgumentNullException.ThrowIfNull(command);
+
var context = contextAware.GetContext();
return context.SendCommand(command);
}