mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 04:59:01 +08:00
refactor(architecture): 重构架构上下文中的命令和查询方法
- 移除旧的 Mediator 扩展方法区域标记 - 重新组织命令发送方法,将异步和同步版本分离 - 更新 SendCommand 和 SendQuery 方法的实现逻辑 - 为查询操作添加新的同步和异步发送方法 - 调整命令执行器方法的参数类型和返回值 - 优化方法注释文档以提高可读性
This commit is contained in:
parent
e6a114fe7b
commit
d0e7a9fb9b
@ -102,68 +102,6 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
return SendRequestAsync(request).AsTask().GetAwaiter().GetResult();
|
return SendRequestAsync(request).AsTask().GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// [Mediator] 异步发送命令并返回结果
|
|
||||||
/// 通过Mediator模式发送命令请求,支持取消操作
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
|
||||||
/// <param name="command">要发送的命令对象</param>
|
|
||||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
|
||||||
/// <returns>包含命令执行结果的ValueTask</returns>
|
|
||||||
public async ValueTask<TResponse> SendCommandAsync<TResponse>(Mediator.ICommand<TResponse> command,
|
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
|
||||||
|
|
||||||
var sender = Sender;
|
|
||||||
if (sender == null)
|
|
||||||
throw new InvalidOperationException("Sender not registered.");
|
|
||||||
|
|
||||||
return await sender.Send(command, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性)
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
|
||||||
/// <param name="command">要发送的命令对象</param>
|
|
||||||
/// <returns>命令执行结果</returns>
|
|
||||||
public TResponse SendCommand<TResponse>(Mediator.ICommand<TResponse> command)
|
|
||||||
{
|
|
||||||
return SendCommandAsync(command).AsTask().GetAwaiter().GetResult();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// [Mediator] 异步发送查询并返回结果
|
|
||||||
/// 通过Mediator模式发送查询请求,支持取消操作
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
|
||||||
/// <param name="command">要发送的查询对象</param>
|
|
||||||
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
|
||||||
/// <returns>包含查询结果的ValueTask</returns>
|
|
||||||
public async ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command,
|
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
|
||||||
|
|
||||||
var sender = Sender;
|
|
||||||
if (sender == null)
|
|
||||||
throw new InvalidOperationException("Sender not registered.");
|
|
||||||
|
|
||||||
return await sender.Send(command, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性)
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
|
||||||
/// <param name="command">要发送的查询对象</param>
|
|
||||||
/// <returns>查询结果</returns>
|
|
||||||
public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command)
|
|
||||||
{
|
|
||||||
return SendQueryAsync(command).AsTask().GetAwaiter().GetResult();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// [Mediator] 发布通知(一对多)
|
/// [Mediator] 发布通知(一对多)
|
||||||
/// 用于事件驱动场景,多个处理器可以同时处理同一个通知
|
/// 用于事件驱动场景,多个处理器可以同时处理同一个通知
|
||||||
@ -205,10 +143,6 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
return mediator.CreateStream(request, cancellationToken);
|
return mediator.CreateStream(request, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Mediator Extension Methods (便捷方法)
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// [扩展] 发送命令(无返回值)
|
/// [扩展] 发送命令(无返回值)
|
||||||
/// 语法糖,等同于 SendRequestAsync<Unit>
|
/// 语法糖,等同于 SendRequestAsync<Unit>
|
||||||
@ -273,6 +207,17 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
return queryBus.Send(query);
|
return queryBus.Send(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性)
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||||
|
/// <param name="command">要发送的查询对象</param>
|
||||||
|
/// <returns>查询结果</returns>
|
||||||
|
public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command)
|
||||||
|
{
|
||||||
|
return SendQueryAsync(command).AsTask().GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步发送一个查询请求
|
/// 异步发送一个查询请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -287,6 +232,26 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
return await asyncQueryBus.SendAsync(query);
|
return await asyncQueryBus.SendAsync(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [Mediator] 异步发送查询并返回结果
|
||||||
|
/// 通过Mediator模式发送查询请求,支持取消操作
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">查询响应类型</typeparam>
|
||||||
|
/// <param name="command">要发送的查询对象</param>
|
||||||
|
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||||
|
/// <returns>包含查询结果的ValueTask</returns>
|
||||||
|
public async ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
var sender = Sender;
|
||||||
|
if (sender == null)
|
||||||
|
throw new InvalidOperationException("Sender not registered.");
|
||||||
|
|
||||||
|
return await sender.Send(command, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Component Retrieval
|
#region Component Retrieval
|
||||||
@ -326,28 +291,23 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
#region Command Execution
|
#region Command Execution
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送一个命令请求
|
/// [Mediator] 异步发送命令并返回结果
|
||||||
|
/// 通过Mediator模式发送命令请求,支持取消操作
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="command">要发送的命令</param>
|
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
||||||
public void SendCommand(ICommand command)
|
/// <param name="command">要发送的命令对象</param>
|
||||||
|
/// <param name="cancellationToken">取消令牌,用于取消操作</param>
|
||||||
|
/// <returns>包含命令执行结果的ValueTask</returns>
|
||||||
|
public async ValueTask<TResponse> SendCommandAsync<TResponse>(Mediator.ICommand<TResponse> command,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
var commandBus = GetOrCache<ICommandExecutor>();
|
|
||||||
commandBus?.Send(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
var sender = Sender;
|
||||||
/// 发送一个带返回值的命令请求
|
if (sender == null)
|
||||||
/// </summary>
|
throw new InvalidOperationException("Sender not registered.");
|
||||||
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
|
||||||
/// <param name="command">要发送的命令</param>
|
return await sender.Send(command, cancellationToken);
|
||||||
/// <returns>命令执行结果</returns>
|
|
||||||
public TResult SendCommand<TResult>(Abstractions.command.ICommand<TResult> command)
|
|
||||||
{
|
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
|
||||||
var commandBus = GetOrCache<ICommandExecutor>();
|
|
||||||
if (commandBus == null) throw new InvalidOperationException("ICommandExecutor not registered");
|
|
||||||
return commandBus.Send(command);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -376,6 +336,42 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
return await commandBus.SendAsync(command);
|
return await commandBus.SendAsync(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [Mediator] 发送命令的同步版本(不推荐,仅用于兼容性)
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResponse">命令响应类型</typeparam>
|
||||||
|
/// <param name="command">要发送的命令对象</param>
|
||||||
|
/// <returns>命令执行结果</returns>
|
||||||
|
public TResponse SendCommand<TResponse>(Mediator.ICommand<TResponse> command)
|
||||||
|
{
|
||||||
|
return SendCommandAsync(command).AsTask().GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送一个命令请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
public void SendCommand(ICommand command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
var commandBus = GetOrCache<ICommandExecutor>();
|
||||||
|
commandBus?.Send(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送一个带返回值的命令请求
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||||
|
/// <param name="command">要发送的命令</param>
|
||||||
|
/// <returns>命令执行结果</returns>
|
||||||
|
public TResult SendCommand<TResult>(Abstractions.command.ICommand<TResult> command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
var commandBus = GetOrCache<ICommandExecutor>();
|
||||||
|
if (commandBus == null) throw new InvalidOperationException("ICommandExecutor not registered");
|
||||||
|
return commandBus.Send(command);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Event Management
|
#region Event Management
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user