mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-24 20:34:29 +08:00
feat(core): 添加异步命令支持功能
- 在 ArchitectureContext 中新增 SendCommandAsync 方法支持异步命令执行 - 在 CommandBus 中实现 SendAsync 方法处理异步命令的发送和执行 - 在 ContextAwareExtensions 中扩展 SendCommandAsync 扩展方法 - 更新 IArchitectureContext 接口定义异步命令方法契约 - 更新 ICommandBus 接口定义异步命令执行方法 - 新增 AbstractAsyncCommand 抽象类提供异步命令基类实现 - 定义 IAsyncCommand 接口规范异步命令的行为 contract
This commit is contained in:
parent
6ef22e5d10
commit
e7285b3426
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using GFramework.Core.Abstractions.command;
|
using GFramework.Core.Abstractions.command;
|
||||||
using GFramework.Core.Abstractions.environment;
|
using GFramework.Core.Abstractions.environment;
|
||||||
using GFramework.Core.Abstractions.events;
|
using GFramework.Core.Abstractions.events;
|
||||||
@ -49,6 +50,20 @@ public interface IArchitectureContext
|
|||||||
/// <returns>命令执行结果</returns>
|
/// <returns>命令执行结果</returns>
|
||||||
TResult SendCommand<TResult>(ICommand<TResult> command);
|
TResult SendCommand<TResult>(ICommand<TResult> command);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行一个命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
Task SendCommandAsync(IAsyncCommand command);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行一个带返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
/// <returns>命令执行结果</returns>
|
||||||
|
Task<TResult> SendCommandAsync<TResult>(IAsyncCommand<TResult> command);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送一个查询请求
|
/// 发送一个查询请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
14
GFramework.Core.Abstractions/command/IAsyncCommand.cs
Normal file
14
GFramework.Core.Abstractions/command/IAsyncCommand.cs
Normal file
@ -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<TResult> : IContextAware
|
||||||
|
{
|
||||||
|
Task<TResult> ExecuteAsync();
|
||||||
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
namespace GFramework.Core.Abstractions.command;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace GFramework.Core.Abstractions.command;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义命令总线接口,用于执行各种命令
|
/// 定义命令总线接口,用于执行各种命令
|
||||||
@ -18,4 +20,18 @@ public interface ICommandBus
|
|||||||
/// <param name="command">要执行的带返回值的命令对象</param>
|
/// <param name="command">要执行的带返回值的命令对象</param>
|
||||||
/// <returns>命令执行的结果</returns>
|
/// <returns>命令执行的结果</returns>
|
||||||
public TResult Send<TResult>(ICommand<TResult> command);
|
public TResult Send<TResult>(ICommand<TResult> command);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行一个命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">要执行的命令对象</param>
|
||||||
|
Task SendAsync(IAsyncCommand command);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行一个带返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
||||||
|
/// <param name="command">要执行的带返回值的命令对象</param>
|
||||||
|
/// <returns>命令执行的结果</returns>
|
||||||
|
Task<TResult> SendAsync<TResult>(IAsyncCommand<TResult> command);
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
using GFramework.Core.Abstractions.architecture;
|
using GFramework.Core.Abstractions.architecture;
|
||||||
using GFramework.Core.Abstractions.command;
|
using GFramework.Core.Abstractions.command;
|
||||||
using GFramework.Core.Abstractions.environment;
|
using GFramework.Core.Abstractions.environment;
|
||||||
using GFramework.Core.Abstractions.events;
|
using GFramework.Core.Abstractions.events;
|
||||||
@ -104,6 +104,28 @@ public class ArchitectureContext(
|
|||||||
return _commandBus.Send(command);
|
return _commandBus.Send(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行一个命令请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
public async Task SendCommandAsync(IAsyncCommand command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
await _commandBus.SendAsync(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行一个带返回值的命令请求
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
/// <returns>命令执行结果</returns>
|
||||||
|
public async Task<TResult> SendCommandAsync<TResult>(IAsyncCommand<TResult> command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
return await _commandBus.SendAsync(command);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Event Management
|
#region Event Management
|
||||||
|
|||||||
26
GFramework.Core/command/AbstractAsyncCommand.cs
Normal file
26
GFramework.Core/command/AbstractAsyncCommand.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using GFramework.Core.Abstractions.command;
|
||||||
|
using GFramework.Core.rule;
|
||||||
|
|
||||||
|
namespace GFramework.Core.command;
|
||||||
|
|
||||||
|
public abstract class AbstractAsyncCommand<TInput>(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, TResult>(TInput input) : ContextAwareBase, IAsyncCommand<TResult>
|
||||||
|
where TInput : ICommandInput
|
||||||
|
{
|
||||||
|
async Task<TResult> IAsyncCommand<TResult>.ExecuteAsync()
|
||||||
|
{
|
||||||
|
return await OnExecuteAsync(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Task<TResult> OnExecuteAsync(TInput input);
|
||||||
|
}
|
||||||
@ -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;
|
namespace GFramework.Core.command;
|
||||||
|
|
||||||
@ -32,4 +33,30 @@ public sealed class CommandBus : ICommandBus
|
|||||||
|
|
||||||
return command.Execute();
|
return command.Execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行无返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">要执行的命令对象,不能为空</param>
|
||||||
|
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
|
||||||
|
public async Task SendAsync(IAsyncCommand command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
await command.ExecuteAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行有返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
||||||
|
/// <param name="command">要执行的命令对象,不能为空</param>
|
||||||
|
/// <returns>命令执行的结果</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
|
||||||
|
public async Task<TResult> SendAsync<TResult>(IAsyncCommand<TResult> command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
return await command.ExecuteAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
using GFramework.Core.Abstractions.command;
|
using GFramework.Core.Abstractions.command;
|
||||||
using GFramework.Core.Abstractions.environment;
|
using GFramework.Core.Abstractions.environment;
|
||||||
using GFramework.Core.Abstractions.events;
|
using GFramework.Core.Abstractions.events;
|
||||||
using GFramework.Core.Abstractions.model;
|
using GFramework.Core.Abstractions.model;
|
||||||
@ -101,6 +101,56 @@ public static class ContextAwareExtensions
|
|||||||
ArgumentNullException.ThrowIfNull(contextAware);
|
ArgumentNullException.ThrowIfNull(contextAware);
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
var Context = contextAware.GetContext();
|
||||||
|
return Context.SendCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行一个无返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
|
||||||
|
public static async Task SendCommandAsync(this IContextAware contextAware, IAsyncCommand command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(contextAware);
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
var Context = contextAware.GetContext();
|
||||||
|
await Context.SendCommandAsync(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并异步执行一个带返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||||
|
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
/// <returns>命令执行结果</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
|
||||||
|
public static async Task<TResult> SendCommandAsync<TResult>(this IContextAware contextAware,
|
||||||
|
IAsyncCommand<TResult> command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(contextAware);
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
var Context = contextAware.GetContext();
|
||||||
|
return await Context.SendCommandAsync(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送一个事件
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||||
|
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
/// <returns>命令执行结果</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">当 contextAware 或 command 为 null 时抛出</exception>
|
||||||
|
public static TResult SendCommand<TResult>(this IContextAware contextAware, ICommand<TResult> command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(contextAware);
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
var context = contextAware.GetContext();
|
var context = contextAware.GetContext();
|
||||||
return context.SendCommand(command);
|
return context.SendCommand(command);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user