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);
}