mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(core): 将命令和查询总线重构为执行器模式
- 将 CommandBus 重命名为 CommandExecutor 并更新相关接口 - 将 QueryBus 重命名为 QueryExecutor 并更新相关接口 - 将 AsyncQueryBus 重命名为 AsyncQueryExecutor 并更新相关接口 - 更新 ArchitectureContext 中的服务引用和错误消息 - 修改 ArchitectureServices 中的私有字段和公共属性名称 - 更新所有测试文件中的实例变量和服务引用 - 修改测试类名称以匹配新的执行器命名 - 更新状态机系统测试中的容器注册项
This commit is contained in:
parent
7481011780
commit
c8be4b317d
@ -26,15 +26,15 @@ public interface IArchitectureServices : IContextAware
|
||||
/// <summary>
|
||||
/// 获取命令总线
|
||||
/// </summary>
|
||||
public ICommandBus CommandBus { get; }
|
||||
public ICommandExecutor CommandExecutor { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询总线
|
||||
/// </summary>
|
||||
public IQueryBus QueryBus { get; }
|
||||
public IQueryExecutor QueryExecutor { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取异步查询总线
|
||||
/// </summary>
|
||||
public IAsyncQueryBus AsyncQueryBus { get; }
|
||||
public IAsyncQueryExecutor AsyncQueryExecutor { get; }
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
namespace GFramework.Core.Abstractions.command;
|
||||
|
||||
/// <summary>
|
||||
/// 定义命令总线接口,用于执行各种命令
|
||||
/// </summary>
|
||||
public interface ICommandBus
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送并执行一个命令
|
||||
/// </summary>
|
||||
/// <param name="command">要执行的命令对象</param>
|
||||
public void Send(ICommand command);
|
||||
|
||||
/// <summary>
|
||||
/// 发送并执行一个带返回值的命令
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
||||
/// <param name="command">要执行的带返回值的命令对象</param>
|
||||
/// <returns>命令执行的结果</returns>
|
||||
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);
|
||||
}
|
||||
36
GFramework.Core.Abstractions/command/ICommandExecutor.cs
Normal file
36
GFramework.Core.Abstractions/command/ICommandExecutor.cs
Normal file
@ -0,0 +1,36 @@
|
||||
namespace GFramework.Core.Abstractions.command;
|
||||
|
||||
/// <summary>
|
||||
/// 定义命令执行器接口,提供同步和异步方式发送并执行命令的方法。
|
||||
/// </summary>
|
||||
public interface ICommandExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送并执行一个命令。
|
||||
/// </summary>
|
||||
/// <param name="command">要执行的命令对象,实现 ICommand 接口。</param>
|
||||
public void Send(ICommand command);
|
||||
|
||||
/// <summary>
|
||||
/// 发送并执行一个带返回值的命令。
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行结果的类型。</typeparam>
|
||||
/// <param name="command">要执行的带返回值的命令对象,实现 ICommand<TResult> 接口。</param>
|
||||
/// <returns>命令执行的结果,类型为 TResult。</returns>
|
||||
public TResult Send<TResult>(ICommand<TResult> command);
|
||||
|
||||
/// <summary>
|
||||
/// 发送并异步执行一个命令。
|
||||
/// </summary>
|
||||
/// <param name="command">要执行的命令对象,实现 IAsyncCommand 接口。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
Task SendAsync(IAsyncCommand command);
|
||||
|
||||
/// <summary>
|
||||
/// 发送并异步执行一个带返回值的命令。
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行结果的类型。</typeparam>
|
||||
/// <param name="command">要执行的带返回值的命令对象,实现 IAsyncCommand<TResult> 接口。</param>
|
||||
/// <returns>表示异步操作的任务,其结果为命令执行的结果,类型为 TResult。</returns>
|
||||
Task<TResult> SendAsync<TResult>(IAsyncCommand<TResult> command);
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
namespace GFramework.Core.Abstractions.query;
|
||||
|
||||
/// <summary>
|
||||
/// 异步查询总线接口,用于处理异步查询请求
|
||||
/// </summary>
|
||||
public interface IAsyncQueryBus
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步发送查询请求并返回结果
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
/// <param name="query">要执行的异步查询对象</param>
|
||||
/// <returns>表示异步操作的任务,任务结果为查询结果</returns>
|
||||
Task<TResult> SendAsync<TResult>(IAsyncQuery<TResult> query);
|
||||
}
|
||||
16
GFramework.Core.Abstractions/query/IAsyncQueryExecutor.cs
Normal file
16
GFramework.Core.Abstractions/query/IAsyncQueryExecutor.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace GFramework.Core.Abstractions.query;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个异步查询执行器接口,用于发送异步查询请求并获取结果。
|
||||
/// </summary>
|
||||
public interface IAsyncQueryExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步发送查询请求并返回结果。
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型。</typeparam>
|
||||
/// <param name="query">要执行的异步查询对象,必须实现 IAsyncQuery<TResult> 接口。</param>
|
||||
/// <returns>表示异步操作的任务,任务完成时返回查询结果。</returns>
|
||||
Task<TResult> SendAsync<TResult>(IAsyncQuery<TResult> query);
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
namespace GFramework.Core.Abstractions.query;
|
||||
|
||||
/// <summary>
|
||||
/// 查询总线接口,用于发送和处理查询请求
|
||||
/// </summary>
|
||||
public interface IQueryBus
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送查询请求并返回结果
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
/// <param name="query">要发送的查询对象</param>
|
||||
/// <returns>查询结果</returns>
|
||||
public TResult Send<TResult>(IQuery<TResult> query);
|
||||
}
|
||||
16
GFramework.Core.Abstractions/query/IQueryExecutor.cs
Normal file
16
GFramework.Core.Abstractions/query/IQueryExecutor.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace GFramework.Core.Abstractions.query;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个查询执行器接口,用于发送查询请求并获取结果。
|
||||
/// </summary>
|
||||
public interface IQueryExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送查询请求并返回结果。
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型。</typeparam>
|
||||
/// <param name="query">要发送的查询对象,必须实现 IQuery<TResult> 接口。</param>
|
||||
/// <returns>查询的结果,类型为 TResult。</returns>
|
||||
TResult Send<TResult>(IQuery<TResult> query);
|
||||
}
|
||||
@ -59,9 +59,9 @@ public class ArchitectureContextTests
|
||||
|
||||
// 创建服务实例
|
||||
_eventBus = new EventBus();
|
||||
_commandBus = new CommandBus();
|
||||
_queryBus = new QueryBus();
|
||||
_asyncQueryBus = new AsyncQueryBus();
|
||||
_commandBus = new CommandExecutor();
|
||||
_queryBus = new QueryExecutor();
|
||||
_asyncQueryBus = new AsyncQueryExecutor();
|
||||
_environment = new DefaultEnvironment();
|
||||
|
||||
// 将服务注册到容器
|
||||
@ -77,9 +77,9 @@ public class ArchitectureContextTests
|
||||
private ArchitectureContext? _context;
|
||||
private IocContainer? _container;
|
||||
private EventBus? _eventBus;
|
||||
private CommandBus? _commandBus;
|
||||
private QueryBus? _queryBus;
|
||||
private AsyncQueryBus? _asyncQueryBus;
|
||||
private CommandExecutor? _commandBus;
|
||||
private QueryExecutor? _queryBus;
|
||||
private AsyncQueryExecutor? _asyncQueryBus;
|
||||
private DefaultEnvironment? _environment;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -21,7 +21,7 @@ namespace GFramework.Core.Tests.architecture;
|
||||
/// ArchitectureServices类的单元测试
|
||||
/// 测试内容包括:
|
||||
/// - 服务容器初始化
|
||||
/// - 所有服务实例创建(Container, EventBus, CommandBus, QueryBus)
|
||||
/// - 所有服务实例创建(Container, EventBus, CommandExecutor, QueryExecutor)
|
||||
/// - SetContext方法 - 设置上下文
|
||||
/// - SetContext方法 - 重复设置上下文
|
||||
/// - GetContext方法 - 获取已设置上下文
|
||||
@ -51,8 +51,8 @@ public class ArchitectureServicesTests
|
||||
{
|
||||
Assert.That(_services!.Container, Is.Not.Null);
|
||||
Assert.That(_services.EventBus, Is.Not.Null);
|
||||
Assert.That(_services.CommandBus, Is.Not.Null);
|
||||
Assert.That(_services.QueryBus, Is.Not.Null);
|
||||
Assert.That(_services.CommandExecutor, Is.Not.Null);
|
||||
Assert.That(_services.QueryExecutor, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -78,8 +78,8 @@ public class ArchitectureServicesTests
|
||||
[Test]
|
||||
public void CommandBus_Should_Be_Instance_Of_CommandBus()
|
||||
{
|
||||
Assert.That(_services!.CommandBus, Is.InstanceOf<ICommandBus>());
|
||||
Assert.That(_services.CommandBus, Is.InstanceOf<CommandBus>());
|
||||
Assert.That(_services!.CommandExecutor, Is.InstanceOf<ICommandExecutor>());
|
||||
Assert.That(_services.CommandExecutor, Is.InstanceOf<CommandExecutor>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -88,8 +88,8 @@ public class ArchitectureServicesTests
|
||||
[Test]
|
||||
public void QueryBus_Should_Be_Instance_Of_QueryBus()
|
||||
{
|
||||
Assert.That(_services!.QueryBus, Is.InstanceOf<IQueryBus>());
|
||||
Assert.That(_services.QueryBus, Is.InstanceOf<QueryBus>());
|
||||
Assert.That(_services!.QueryExecutor, Is.InstanceOf<IQueryExecutor>());
|
||||
Assert.That(_services.QueryExecutor, Is.InstanceOf<QueryExecutor>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -198,7 +198,7 @@ public class ArchitectureServicesTests
|
||||
var services1 = new ArchitectureServices();
|
||||
var services2 = new ArchitectureServices();
|
||||
|
||||
Assert.That(services1.CommandBus, Is.Not.SameAs(services2.CommandBus));
|
||||
Assert.That(services1.CommandExecutor, Is.Not.SameAs(services2.CommandExecutor));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -210,7 +210,7 @@ public class ArchitectureServicesTests
|
||||
var services1 = new ArchitectureServices();
|
||||
var services2 = new ArchitectureServices();
|
||||
|
||||
Assert.That(services1.QueryBus, Is.Not.SameAs(services2.QueryBus));
|
||||
Assert.That(services1.QueryExecutor, Is.Not.SameAs(services2.QueryExecutor));
|
||||
}
|
||||
}
|
||||
|
||||
@ -224,8 +224,8 @@ public class TestArchitectureContextV3 : IArchitectureContext
|
||||
|
||||
public IIocContainer Container => _container;
|
||||
public IEventBus EventBus => new EventBus();
|
||||
public ICommandBus CommandBus => new CommandBus();
|
||||
public IQueryBus QueryBus => new QueryBus();
|
||||
public ICommandExecutor CommandExecutor => new CommandExecutor();
|
||||
public IQueryExecutor QueryExecutor => new QueryExecutor();
|
||||
|
||||
public TService? GetService<TService>() where TService : class
|
||||
{
|
||||
|
||||
@ -244,12 +244,12 @@ public class TestArchitectureContext : IArchitectureContext
|
||||
/// <summary>
|
||||
/// 获取命令总线
|
||||
/// </summary>
|
||||
public ICommandBus CommandBus => new CommandBus();
|
||||
public ICommandExecutor CommandExecutor => new CommandExecutor();
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询总线
|
||||
/// </summary>
|
||||
public IQueryBus QueryBus => new QueryBus();
|
||||
public IQueryExecutor QueryExecutor => new QueryExecutor();
|
||||
|
||||
/// <summary>
|
||||
/// 获取环境对象
|
||||
|
||||
@ -34,10 +34,10 @@ public class AbstractAsyncCommandTests
|
||||
{
|
||||
_container = new IocContainer();
|
||||
_container.RegisterPlurality(new EventBus());
|
||||
_container.RegisterPlurality(new CommandBus());
|
||||
_container.RegisterPlurality(new QueryBus());
|
||||
_container.RegisterPlurality(new CommandExecutor());
|
||||
_container.RegisterPlurality(new QueryExecutor());
|
||||
_container.RegisterPlurality(new DefaultEnvironment());
|
||||
_container.RegisterPlurality(new AsyncQueryBus());
|
||||
_container.RegisterPlurality(new AsyncQueryExecutor());
|
||||
_context = new ArchitectureContext(_container);
|
||||
}
|
||||
|
||||
|
||||
@ -17,15 +17,15 @@ namespace GFramework.Core.Tests.command;
|
||||
/// - SendAsync方法(带返回值)处理null异步命令
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class CommandBusTests
|
||||
public class CommandExecutorTests
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_commandBus = new CommandBus();
|
||||
_commandExecutor = new CommandExecutor();
|
||||
}
|
||||
|
||||
private CommandBus _commandBus = null!;
|
||||
private CommandExecutor _commandExecutor = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 测试Send方法执行命令
|
||||
@ -36,7 +36,7 @@ public class CommandBusTests
|
||||
var input = new TestCommandInput { Value = 42 };
|
||||
var command = new TestCommand(input);
|
||||
|
||||
Assert.DoesNotThrow(() => _commandBus.Send(command));
|
||||
Assert.DoesNotThrow(() => _commandExecutor.Send(command));
|
||||
Assert.That(command.Executed, Is.True);
|
||||
Assert.That(command.ExecutedValue, Is.EqualTo(42));
|
||||
}
|
||||
@ -47,7 +47,7 @@ public class CommandBusTests
|
||||
[Test]
|
||||
public void Send_WithNullCommand_Should_ThrowArgumentNullException()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _commandBus.Send(null!));
|
||||
Assert.Throws<ArgumentNullException>(() => _commandExecutor.Send(null!));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -59,7 +59,7 @@ public class CommandBusTests
|
||||
var input = new TestCommandInput { Value = 100 };
|
||||
var command = new TestCommandWithResult(input);
|
||||
|
||||
var result = _commandBus.Send(command);
|
||||
var result = _commandExecutor.Send(command);
|
||||
|
||||
Assert.That(command.Executed, Is.True);
|
||||
Assert.That(result, Is.EqualTo(200));
|
||||
@ -71,7 +71,7 @@ public class CommandBusTests
|
||||
[Test]
|
||||
public void Send_WithResult_AndNullCommand_Should_ThrowArgumentNullException()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _commandBus.Send<int>(null!));
|
||||
Assert.Throws<ArgumentNullException>(() => _commandExecutor.Send<int>(null!));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -83,7 +83,7 @@ public class CommandBusTests
|
||||
var input = new TestCommandInput { Value = 42 };
|
||||
var command = new TestAsyncCommand(input);
|
||||
|
||||
await _commandBus.SendAsync(command);
|
||||
await _commandExecutor.SendAsync(command);
|
||||
|
||||
Assert.That(command.Executed, Is.True);
|
||||
Assert.That(command.ExecutedValue, Is.EqualTo(42));
|
||||
@ -95,7 +95,7 @@ public class CommandBusTests
|
||||
[Test]
|
||||
public void SendAsync_WithNullCommand_Should_ThrowArgumentNullException()
|
||||
{
|
||||
Assert.ThrowsAsync<ArgumentNullException>(async () => await _commandBus.SendAsync(null!));
|
||||
Assert.ThrowsAsync<ArgumentNullException>(async () => await _commandExecutor.SendAsync(null!));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -107,7 +107,7 @@ public class CommandBusTests
|
||||
var input = new TestCommandInput { Value = 100 };
|
||||
var command = new TestAsyncCommandWithResult(input);
|
||||
|
||||
var result = await _commandBus.SendAsync(command);
|
||||
var result = await _commandExecutor.SendAsync(command);
|
||||
|
||||
Assert.That(command.Executed, Is.True);
|
||||
Assert.That(result, Is.EqualTo(200));
|
||||
@ -119,7 +119,7 @@ public class CommandBusTests
|
||||
[Test]
|
||||
public void SendAsync_WithResult_AndNullCommand_Should_ThrowArgumentNullException()
|
||||
{
|
||||
Assert.ThrowsAsync<ArgumentNullException>(async () => await _commandBus.SendAsync<int>(null!));
|
||||
Assert.ThrowsAsync<ArgumentNullException>(async () => await _commandExecutor.SendAsync<int>(null!));
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,10 +32,10 @@ public class AbstractAsyncQueryTests
|
||||
{
|
||||
_container = new IocContainer();
|
||||
_container.RegisterPlurality(new EventBus());
|
||||
_container.RegisterPlurality(new CommandBus());
|
||||
_container.RegisterPlurality(new QueryBus());
|
||||
_container.RegisterPlurality(new CommandExecutor());
|
||||
_container.RegisterPlurality(new QueryExecutor());
|
||||
_container.RegisterPlurality(new DefaultEnvironment());
|
||||
_container.RegisterPlurality(new AsyncQueryBus());
|
||||
_container.RegisterPlurality(new AsyncQueryExecutor());
|
||||
_context = new ArchitectureContext(_container);
|
||||
}
|
||||
|
||||
|
||||
@ -15,15 +15,15 @@ namespace GFramework.Core.Tests.query;
|
||||
/// - 异步查询的上下文传递
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class AsyncQueryBusTests
|
||||
public class AsyncQueryExecutorTests
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_asyncQueryBus = new AsyncQueryBus();
|
||||
_asyncQueryExecutor = new AsyncQueryExecutor();
|
||||
}
|
||||
|
||||
private AsyncQueryBus _asyncQueryBus = null!;
|
||||
private AsyncQueryExecutor _asyncQueryExecutor = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 测试SendAsync方法正确返回查询结果
|
||||
@ -34,7 +34,7 @@ public class AsyncQueryBusTests
|
||||
var input = new TestAsyncQueryInput { Value = 10 };
|
||||
var query = new TestAsyncQuery(input);
|
||||
|
||||
var result = await _asyncQueryBus.SendAsync(query);
|
||||
var result = await _asyncQueryExecutor.SendAsync(query);
|
||||
|
||||
Assert.That(result, Is.EqualTo(20));
|
||||
}
|
||||
@ -45,7 +45,7 @@ public class AsyncQueryBusTests
|
||||
[Test]
|
||||
public void SendAsync_WithNullQuery_Should_ThrowArgumentNullException()
|
||||
{
|
||||
Assert.ThrowsAsync<ArgumentNullException>(async () => await _asyncQueryBus.SendAsync<int>(null!));
|
||||
Assert.ThrowsAsync<ArgumentNullException>(async () => await _asyncQueryExecutor.SendAsync<int>(null!));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -57,7 +57,7 @@ public class AsyncQueryBusTests
|
||||
var input = new TestAsyncQueryInput { Value = 5 };
|
||||
var query = new TestAsyncStringQuery(input);
|
||||
|
||||
var result = await _asyncQueryBus.SendAsync(query);
|
||||
var result = await _asyncQueryExecutor.SendAsync(query);
|
||||
|
||||
Assert.That(result, Is.EqualTo("Result: 10"));
|
||||
}
|
||||
@ -71,7 +71,7 @@ public class AsyncQueryBusTests
|
||||
var input = new TestAsyncQueryInput { Value = 42 };
|
||||
var query = new TestAsyncBooleanQuery(input);
|
||||
|
||||
var result = await _asyncQueryBus.SendAsync(query);
|
||||
var result = await _asyncQueryExecutor.SendAsync(query);
|
||||
|
||||
Assert.That(result, Is.True);
|
||||
}
|
||||
@ -85,7 +85,7 @@ public class AsyncQueryBusTests
|
||||
var input = new TestAsyncQueryInput { Value = 100 };
|
||||
var query = new TestAsyncComplexQuery(input);
|
||||
|
||||
var result = await _asyncQueryBus.SendAsync(query);
|
||||
var result = await _asyncQueryExecutor.SendAsync(query);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Value, Is.EqualTo(200));
|
||||
@ -101,7 +101,7 @@ public class AsyncQueryBusTests
|
||||
var input = new TestAsyncQueryInput { Value = 0 };
|
||||
var query = new TestAsyncQueryWithException(input);
|
||||
|
||||
Assert.ThrowsAsync<InvalidOperationException>(async () => await _asyncQueryBus.SendAsync(query));
|
||||
Assert.ThrowsAsync<InvalidOperationException>(async () => await _asyncQueryExecutor.SendAsync(query));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -113,8 +113,8 @@ public class AsyncQueryBusTests
|
||||
var input = new TestAsyncQueryInput { Value = 10 };
|
||||
var query = new TestAsyncQuery(input);
|
||||
|
||||
var result1 = await _asyncQueryBus.SendAsync(query);
|
||||
var result2 = await _asyncQueryBus.SendAsync(query);
|
||||
var result1 = await _asyncQueryExecutor.SendAsync(query);
|
||||
var result2 = await _asyncQueryExecutor.SendAsync(query);
|
||||
|
||||
Assert.That(result1, Is.EqualTo(20));
|
||||
Assert.That(result2, Is.EqualTo(20));
|
||||
@ -131,8 +131,8 @@ public class AsyncQueryBusTests
|
||||
var query1 = new TestAsyncQuery(input1);
|
||||
var query2 = new TestAsyncQuery(input2);
|
||||
|
||||
var result1 = await _asyncQueryBus.SendAsync(query1);
|
||||
var result2 = await _asyncQueryBus.SendAsync(query2);
|
||||
var result1 = await _asyncQueryExecutor.SendAsync(query1);
|
||||
var result2 = await _asyncQueryExecutor.SendAsync(query2);
|
||||
|
||||
Assert.That(result1, Is.EqualTo(20));
|
||||
Assert.That(result2, Is.EqualTo(40));
|
||||
@ -8,7 +8,7 @@ namespace GFramework.Core.Tests.query;
|
||||
/// 查询总线测试类,用于测试QueryBus的功能和异常处理
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class QueryBusTests
|
||||
public class QueryExecutorTests
|
||||
{
|
||||
/// <summary>
|
||||
/// 测试设置方法,在每个测试方法执行前初始化查询总线实例
|
||||
@ -16,10 +16,10 @@ public class QueryBusTests
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_queryBus = new QueryBus();
|
||||
_queryExecutor = new QueryExecutor();
|
||||
}
|
||||
|
||||
private QueryBus _queryBus = null!;
|
||||
private QueryExecutor _queryExecutor = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 测试Send方法是否能正确返回查询结果
|
||||
@ -31,7 +31,7 @@ public class QueryBusTests
|
||||
var input = new TestQueryInput { Value = 10 };
|
||||
var query = new TestQuery(input);
|
||||
|
||||
var result = _queryBus.Send(query);
|
||||
var result = _queryExecutor.Send(query);
|
||||
|
||||
Assert.That(result, Is.EqualTo(20));
|
||||
}
|
||||
@ -43,7 +43,7 @@ public class QueryBusTests
|
||||
[Test]
|
||||
public void Send_WithNullQuery_Should_ThrowArgumentNullException()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _queryBus.Send<int>(null!));
|
||||
Assert.Throws<ArgumentNullException>(() => _queryExecutor.Send<int>(null!));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -56,7 +56,7 @@ public class QueryBusTests
|
||||
var input = new TestQueryInput { Value = 5 };
|
||||
var query = new TestStringQuery(input);
|
||||
|
||||
var result = _queryBus.Send(query);
|
||||
var result = _queryExecutor.Send(query);
|
||||
|
||||
Assert.That(result, Is.EqualTo("Result: 10"));
|
||||
}
|
||||
@ -50,10 +50,10 @@ public class StateMachineSystemTests
|
||||
LoggerFactoryResolver.Provider.CreateLogger(nameof(StateMachineSystemTests)));
|
||||
|
||||
container.RegisterPlurality(_eventBus);
|
||||
container.RegisterPlurality(new CommandBus());
|
||||
container.RegisterPlurality(new QueryBus());
|
||||
container.RegisterPlurality(new CommandExecutor());
|
||||
container.RegisterPlurality(new QueryExecutor());
|
||||
container.RegisterPlurality(new DefaultEnvironment());
|
||||
container.RegisterPlurality(new AsyncQueryBus());
|
||||
container.RegisterPlurality(new AsyncQueryExecutor());
|
||||
|
||||
_context = new ArchitectureContext(container);
|
||||
|
||||
|
||||
@ -32,10 +32,10 @@ public class AbstractContextUtilityTests
|
||||
{
|
||||
_container = new IocContainer();
|
||||
_container.RegisterPlurality(new EventBus());
|
||||
_container.RegisterPlurality(new CommandBus());
|
||||
_container.RegisterPlurality(new QueryBus());
|
||||
_container.RegisterPlurality(new CommandExecutor());
|
||||
_container.RegisterPlurality(new QueryExecutor());
|
||||
_container.RegisterPlurality(new DefaultEnvironment());
|
||||
_container.RegisterPlurality(new AsyncQueryBus());
|
||||
_container.RegisterPlurality(new AsyncQueryExecutor());
|
||||
_context = new ArchitectureContext(_container);
|
||||
}
|
||||
|
||||
|
||||
@ -61,8 +61,8 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
||||
{
|
||||
if (query == null) throw new ArgumentNullException(nameof(query));
|
||||
var queryBus = GetOrCache<IQueryBus>();
|
||||
if (queryBus == null) throw new InvalidOperationException("IQueryBus not registered");
|
||||
var queryBus = GetOrCache<IQueryExecutor>();
|
||||
if (queryBus == null) throw new InvalidOperationException("IQueryExecutor not registered");
|
||||
return queryBus.Send(query);
|
||||
}
|
||||
|
||||
@ -75,8 +75,8 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||
public async Task<TResult> SendQueryAsync<TResult>(IAsyncQuery<TResult> query)
|
||||
{
|
||||
if (query == null) throw new ArgumentNullException(nameof(query));
|
||||
var asyncQueryBus = GetOrCache<IAsyncQueryBus>();
|
||||
if (asyncQueryBus == null) throw new InvalidOperationException("IAsyncQueryBus not registered");
|
||||
var asyncQueryBus = GetOrCache<IAsyncQueryExecutor>();
|
||||
if (asyncQueryBus == null) throw new InvalidOperationException("IAsyncQueryExecutor not registered");
|
||||
return await asyncQueryBus.SendAsync(query);
|
||||
}
|
||||
|
||||
@ -125,7 +125,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||
public void SendCommand(ICommand command)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
var commandBus = GetOrCache<ICommandBus>();
|
||||
var commandBus = GetOrCache<ICommandExecutor>();
|
||||
commandBus?.Send(command);
|
||||
}
|
||||
|
||||
@ -138,8 +138,8 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
var commandBus = GetOrCache<ICommandBus>();
|
||||
if (commandBus == null) throw new InvalidOperationException("ICommandBus not registered");
|
||||
var commandBus = GetOrCache<ICommandExecutor>();
|
||||
if (commandBus == null) throw new InvalidOperationException("ICommandExecutor not registered");
|
||||
return commandBus.Send(command);
|
||||
}
|
||||
|
||||
@ -150,8 +150,8 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||
public async Task SendCommandAsync(IAsyncCommand command)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
var commandBus = GetOrCache<ICommandBus>();
|
||||
if (commandBus == null) throw new InvalidOperationException("ICommandBus not registered");
|
||||
var commandBus = GetOrCache<ICommandExecutor>();
|
||||
if (commandBus == null) throw new InvalidOperationException("ICommandExecutor not registered");
|
||||
await commandBus.SendAsync(command);
|
||||
}
|
||||
|
||||
@ -164,8 +164,8 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||
public async Task<TResult> SendCommandAsync<TResult>(IAsyncCommand<TResult> command)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
var commandBus = GetOrCache<ICommandBus>();
|
||||
if (commandBus == null) throw new InvalidOperationException("ICommandBus not registered");
|
||||
var commandBus = GetOrCache<ICommandExecutor>();
|
||||
if (commandBus == null) throw new InvalidOperationException("ICommandExecutor not registered");
|
||||
return await commandBus.SendAsync(command);
|
||||
}
|
||||
|
||||
|
||||
@ -7,7 +7,6 @@ using GFramework.Core.command;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.ioc;
|
||||
using GFramework.Core.query;
|
||||
using IAsyncQueryBus = GFramework.Core.Abstractions.query.IAsyncQueryBus;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
@ -19,12 +18,12 @@ public class ArchitectureServices : IArchitectureServices
|
||||
/// <summary>
|
||||
/// 异步查询总线实例
|
||||
/// </summary>
|
||||
private readonly IAsyncQueryBus _asyncQueryBus;
|
||||
private readonly IAsyncQueryExecutor _asyncQueryExecutor;
|
||||
|
||||
/// <summary>
|
||||
/// 命令总线实例
|
||||
/// 命令执行器实例
|
||||
/// </summary>
|
||||
private readonly ICommandBus _commandBus;
|
||||
private readonly ICommandExecutor _commandExecutor;
|
||||
|
||||
/// <summary>
|
||||
/// 事件总线实例
|
||||
@ -32,14 +31,19 @@ public class ArchitectureServices : IArchitectureServices
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
/// <summary>
|
||||
/// 查询总线实例
|
||||
/// 同步查询执行器实例
|
||||
/// </summary>
|
||||
private readonly IQueryBus _queryBus;
|
||||
private readonly IQueryExecutor _queryExecutor;
|
||||
|
||||
/// <summary>
|
||||
/// 架构上下文对象
|
||||
/// </summary>
|
||||
private IArchitectureContext _context = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数,初始化架构服务
|
||||
/// 初始化依赖注入容器,并创建事件总线、命令执行器、查询执行器和异步查询执行器的实例,
|
||||
/// 然后将这些服务注册到容器中。
|
||||
/// </summary>
|
||||
public ArchitectureServices()
|
||||
{
|
||||
@ -47,15 +51,15 @@ public class ArchitectureServices : IArchitectureServices
|
||||
|
||||
// 创建服务实例
|
||||
_eventBus = new EventBus();
|
||||
_commandBus = new CommandBus();
|
||||
_queryBus = new QueryBus();
|
||||
_asyncQueryBus = new AsyncQueryBus();
|
||||
_commandExecutor = new CommandExecutor();
|
||||
_queryExecutor = new QueryExecutor();
|
||||
_asyncQueryExecutor = new AsyncQueryExecutor();
|
||||
|
||||
// 将服务注册到容器
|
||||
Container.RegisterPlurality(_eventBus);
|
||||
Container.RegisterPlurality(_commandBus);
|
||||
Container.RegisterPlurality(_queryBus);
|
||||
Container.RegisterPlurality(_asyncQueryBus);
|
||||
Container.RegisterPlurality(_commandExecutor);
|
||||
Container.RegisterPlurality(_queryExecutor);
|
||||
Container.RegisterPlurality(_asyncQueryExecutor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -64,24 +68,24 @@ public class ArchitectureServices : IArchitectureServices
|
||||
public IIocContainer Container { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取类型事件系统
|
||||
/// 获取事件总线实例
|
||||
/// </summary>
|
||||
public IEventBus EventBus => _eventBus;
|
||||
|
||||
/// <summary>
|
||||
/// 获取命令总线
|
||||
/// 获取命令执行器实例
|
||||
/// </summary>
|
||||
public ICommandBus CommandBus => _commandBus;
|
||||
public ICommandExecutor CommandExecutor => _commandExecutor;
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询总线
|
||||
/// 获取同步查询执行器实例
|
||||
/// </summary>
|
||||
public IQueryBus QueryBus => _queryBus;
|
||||
public IQueryExecutor QueryExecutor => _queryExecutor;
|
||||
|
||||
/// <summary>
|
||||
/// 获取异步查询总线
|
||||
/// 获取异步查询执行器实例
|
||||
/// </summary>
|
||||
public IAsyncQueryBus AsyncQueryBus => _asyncQueryBus;
|
||||
public IAsyncQueryExecutor AsyncQueryExecutor => _asyncQueryExecutor;
|
||||
|
||||
/// <summary>
|
||||
/// 设置架构上下文
|
||||
@ -101,4 +105,4 @@ public class ArchitectureServices : IArchitectureServices
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,9 +4,10 @@ using IAsyncCommand = GFramework.Core.Abstractions.command.IAsyncCommand;
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
/// <summary>
|
||||
/// 命令总线实现类,用于发送和执行命令
|
||||
/// 表示一个命令执行器,用于执行命令操作。
|
||||
/// 该类实现了 ICommandExecutor 接口,提供命令执行的核心功能。
|
||||
/// </summary>
|
||||
public sealed class CommandBus : ICommandBus
|
||||
public sealed class CommandExecutor : ICommandExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送并执行无返回值的命令
|
||||
@ -5,7 +5,7 @@ namespace GFramework.Core.query;
|
||||
/// <summary>
|
||||
/// 异步查询总线实现,用于处理异步查询请求
|
||||
/// </summary>
|
||||
public sealed class AsyncQueryBus : IAsyncQueryBus
|
||||
public sealed class AsyncQueryExecutor : IAsyncQueryExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步发送查询请求并返回结果
|
||||
@ -1,23 +0,0 @@
|
||||
using GFramework.Core.Abstractions.query;
|
||||
|
||||
namespace GFramework.Core.query;
|
||||
|
||||
/// <summary>
|
||||
/// 查询总线实现,负责执行查询并返回结果
|
||||
/// </summary>
|
||||
public sealed class QueryBus : IQueryBus
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行指定的查询并返回结果
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
/// <param name="query">要执行的查询对象</param>
|
||||
/// <returns>查询执行结果</returns>
|
||||
public TResult Send<TResult>(IQuery<TResult> query)
|
||||
{
|
||||
// 验证查询参数不为null
|
||||
ArgumentNullException.ThrowIfNull(query);
|
||||
|
||||
return query.Do();
|
||||
}
|
||||
}
|
||||
27
GFramework.Core/query/QueryExecutor.cs
Normal file
27
GFramework.Core/query/QueryExecutor.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using GFramework.Core.Abstractions.query;
|
||||
|
||||
namespace GFramework.Core.query;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// QueryExecutor 类负责执行查询操作,实现 IQueryExecutor 接口。
|
||||
/// 该类是密封的,防止被继承。
|
||||
/// </summary>
|
||||
public sealed class QueryExecutor : IQueryExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行指定的查询并返回结果。
|
||||
/// 该方法通过调用查询对象的 Do 方法来获取结果。
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型。</typeparam>
|
||||
/// <param name="query">要执行的查询对象,必须实现 IQuery<TResult> 接口。</param>
|
||||
/// <returns>查询执行的结果,类型为 TResult。</returns>
|
||||
public TResult Send<TResult>(IQuery<TResult> query)
|
||||
{
|
||||
// 验证查询参数不为 null,如果为 null 则抛出 ArgumentNullException 异常
|
||||
ArgumentNullException.ThrowIfNull(query);
|
||||
|
||||
// 调用查询对象的 Do 方法执行查询并返回结果
|
||||
return query.Do();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user