refactor(architecture): 移除查询和事件发布语法糖方法

- 删除 ArchitectureContext 中的 QueryAsync 和 PublishEventAsync 扩展方法
- 删除 ContextAwareExtensions 中对应的扩展方法实现
- 从 IArchitectureContext 接口中移除相关方法定义
- 更新测试代码中的参数命名从 command 到 query
- 移除相关的单元测试用例
- 修正 Godot 扩展中对 PublishEventAsync 的调用为 PublishAsync
This commit is contained in:
GeWuYou 2026-02-17 16:29:08 +08:00 committed by gewuyou
parent 984829c368
commit d76751c636
7 changed files with 40 additions and 152 deletions

View File

@ -104,9 +104,9 @@ public interface IArchitectureContext
/// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性) /// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性)
/// </summary> /// </summary>
/// <typeparam name="TResponse">查询响应类型</typeparam> /// <typeparam name="TResponse">查询响应类型</typeparam>
/// <param name="command">要发送的查询对象</param> /// <param name="query">要发送的查询对象</param>
/// <returns>查询结果</returns> /// <returns>查询结果</returns>
TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command); TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> query);
/// <summary> /// <summary>
/// 异步发送一个查询请求 /// 异步发送一个查询请求
@ -121,10 +121,10 @@ public interface IArchitectureContext
/// 通过Mediator模式发送查询请求支持取消操作 /// 通过Mediator模式发送查询请求支持取消操作
/// </summary> /// </summary>
/// <typeparam name="TResponse">查询响应类型</typeparam> /// <typeparam name="TResponse">查询响应类型</typeparam>
/// <param name="command">要发送的查询对象</param> /// <param name="query">要发送的查询对象</param>
/// <param name="cancellationToken">取消令牌,用于取消操作</param> /// <param name="cancellationToken">取消令牌,用于取消操作</param>
/// <returns>包含查询结果的ValueTask</returns> /// <returns>包含查询结果的ValueTask</returns>
ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command, ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> query,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
/// <summary> /// <summary>
@ -199,20 +199,6 @@ public interface IArchitectureContext
IRequest<TResponse> command, IRequest<TResponse> command,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
/// <summary>
/// 发送查询
/// </summary>
ValueTask<TResponse> QueryAsync<TResponse>(
IRequest<TResponse> query,
CancellationToken cancellationToken = default);
/// <summary>
/// 发布事件通知
/// </summary>
ValueTask PublishEventAsync<TNotification>(
TNotification notification,
CancellationToken cancellationToken = default)
where TNotification : INotification;
/// <summary> /// <summary>
/// 获取环境对象 /// 获取环境对象

View File

@ -288,13 +288,13 @@ public class TestArchitectureContextV3 : IArchitectureContext
throw new NotImplementedException(); throw new NotImplementedException();
} }
public ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command, public ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> query,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command) public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> query)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -323,18 +323,6 @@ public class TestArchitectureContextV3 : IArchitectureContext
throw new NotImplementedException(); throw new NotImplementedException();
} }
public ValueTask<TResponse> QueryAsync<TResponse>(IRequest<TResponse> query,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public ValueTask PublishEventAsync<TNotification>(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}
public void SendCommand(ICommand command) public void SendCommand(ICommand command)
{ {
} }
@ -368,6 +356,18 @@ public class TestArchitectureContextV3 : IArchitectureContext
{ {
return _environment; return _environment;
} }
public ValueTask<TResponse> QueryAsync<TResponse>(IRequest<TResponse> query,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public ValueTask PublishEventAsync<TNotification>(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}
} }
#endregion #endregion

View File

@ -357,13 +357,13 @@ public class TestArchitectureContext : IArchitectureContext
throw new NotImplementedException(); throw new NotImplementedException();
} }
public ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command, public ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> query,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command) public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> query)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -392,18 +392,6 @@ public class TestArchitectureContext : IArchitectureContext
throw new NotImplementedException(); throw new NotImplementedException();
} }
public ValueTask<TResponse> QueryAsync<TResponse>(IRequest<TResponse> query,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public ValueTask PublishEventAsync<TNotification>(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}
/// <summary> /// <summary>
/// 发送命令 /// 发送命令
/// </summary> /// </summary>
@ -463,4 +451,16 @@ public class TestArchitectureContext : IArchitectureContext
{ {
return Environment; return Environment;
} }
public ValueTask<TResponse> QueryAsync<TResponse>(IRequest<TResponse> query,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public ValueTask PublishEventAsync<TNotification>(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}
} }

View File

@ -150,27 +150,6 @@ public class MediatorComprehensiveTests
Assert.That(result, Is.EqualTo(42)); Assert.That(result, Is.EqualTo(42));
} }
[Test]
public async Task QueryAsync_Should_ReturnResult_When_Query_IsValid()
{
var testQuery = new TestQuery { QueryResult = "test result" };
var result = await _context!.QueryAsync(testQuery);
Assert.That(result, Is.EqualTo("test result"));
}
[Test]
public async Task PublishEventAsync_Should_PublishNotification_When_Notification_IsValid()
{
TestNotificationHandler.LastReceivedMessage = null;
var testNotification = new TestNotification { Message = "test event" };
await _context!.PublishEventAsync(testNotification);
await Task.Delay(100);
Assert.That(TestNotificationHandler.LastReceivedMessage, Is.EqualTo("test event"));
}
[Test] [Test]
public void GetService_Should_Use_Cache() public void GetService_Should_Use_Cache()
{ {
@ -290,21 +269,6 @@ public class MediatorComprehensiveTests
Assert.That(sharedData.Value, Is.EqualTo(30)); // 10 + 20 Assert.That(sharedData.Value, Is.EqualTo(30)); // 10 + 20
} }
[Test]
public async Task Query_Caching_With_Mediator()
{
var cache = new Dictionary<string, string>();
var query1 = new TestCachingQuery { Key = "test1", Cache = cache };
var query2 = new TestCachingQuery { Key = "test1", Cache = cache }; // 相同key
var result1 = await _context!.QueryAsync(query1);
var result2 = await _context.QueryAsync(query2);
// 验证缓存生效相同key返回相同结果
Assert.That(result1, Is.EqualTo(result2));
Assert.That(cache.Count, Is.EqualTo(1)); // 只缓存了一次
}
[Test] [Test]
public async Task Notification_Ordering_Should_Be_Preserved() public async Task Notification_Ordering_Should_Be_Preserved()
{ {

View File

@ -166,29 +166,6 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
return await SendRequestAsync(command, cancellationToken); return await SendRequestAsync(command, cancellationToken);
} }
/// <summary>
/// [扩展] 发送查询
/// 语法糖,等同于 SendRequestAsync语义更清晰
/// </summary>
public async ValueTask<TResponse> QueryAsync<TResponse>(
IRequest<TResponse> query,
CancellationToken cancellationToken = default)
{
return await SendRequestAsync(query, cancellationToken);
}
/// <summary>
/// [扩展] 发布事件通知
/// 语法糖,等同于 PublishAsync
/// </summary>
public async ValueTask PublishEventAsync<TNotification>(
TNotification notification,
CancellationToken cancellationToken = default)
where TNotification : INotification
{
await PublishAsync(notification, cancellationToken);
}
#endregion #endregion
#region Query Execution #region Query Execution
@ -211,11 +188,11 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
/// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性) /// [Mediator] 发送查询的同步版本(不推荐,仅用于兼容性)
/// </summary> /// </summary>
/// <typeparam name="TResponse">查询响应类型</typeparam> /// <typeparam name="TResponse">查询响应类型</typeparam>
/// <param name="command">要发送的查询对象</param> /// <param name="query">要发送的查询对象</param>
/// <returns>查询结果</returns> /// <returns>查询结果</returns>
public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> command) public TResponse SendQuery<TResponse>(Mediator.IQuery<TResponse> query)
{ {
return SendQueryAsync(command).AsTask().GetAwaiter().GetResult(); return SendQueryAsync(query).AsTask().GetAwaiter().GetResult();
} }
/// <summary> /// <summary>
@ -237,19 +214,19 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
/// 通过Mediator模式发送查询请求支持取消操作 /// 通过Mediator模式发送查询请求支持取消操作
/// </summary> /// </summary>
/// <typeparam name="TResponse">查询响应类型</typeparam> /// <typeparam name="TResponse">查询响应类型</typeparam>
/// <param name="command">要发送的查询对象</param> /// <param name="query">要发送的查询对象</param>
/// <param name="cancellationToken">取消令牌,用于取消操作</param> /// <param name="cancellationToken">取消令牌,用于取消操作</param>
/// <returns>包含查询结果的ValueTask</returns> /// <returns>包含查询结果的ValueTask</returns>
public async ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> command, public async ValueTask<TResponse> SendQueryAsync<TResponse>(Mediator.IQuery<TResponse> query,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
ArgumentNullException.ThrowIfNull(command); ArgumentNullException.ThrowIfNull(query);
var sender = Sender; var sender = Sender;
if (sender == null) if (sender == null)
throw new InvalidOperationException("Sender not registered."); throw new InvalidOperationException("Sender not registered.");
return await sender.Send(command, cancellationToken); return await sender.Send(query, cancellationToken);
} }
#endregion #endregion

View File

@ -456,43 +456,4 @@ public static class ContextAwareExtensions
var context = contextAware.GetContext(); var context = contextAware.GetContext();
return context.SendAsync(command, cancellationToken); return context.SendAsync(command, cancellationToken);
} }
/// <summary>
/// 发送查询
/// </summary>
/// <typeparam name="TResponse">响应类型</typeparam>
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
/// <param name="query">要发送的查询</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>查询结果</returns>
/// <exception cref="ArgumentNullException">当 contextAware 或 query 为 null 时抛出</exception>
public static ValueTask<TResponse> QueryAsync<TResponse>(this IContextAware contextAware,
IRequest<TResponse> query, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(query);
var context = contextAware.GetContext();
return context.QueryAsync(query, cancellationToken);
}
/// <summary>
/// 发布事件通知
/// </summary>
/// <typeparam name="TNotification">通知类型</typeparam>
/// <param name="contextAware">实现 IContextAware 接口的对象</param>
/// <param name="notification">要发布的通知</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>异步任务</returns>
/// <exception cref="ArgumentNullException">当 contextAware 或 notification 为 null 时抛出</exception>
public static ValueTask PublishEventAsync<TNotification>(this IContextAware contextAware,
TNotification notification, CancellationToken cancellationToken = default)
where TNotification : INotification
{
ArgumentNullException.ThrowIfNull(contextAware);
ArgumentNullException.ThrowIfNull(notification);
var context = contextAware.GetContext();
return context.PublishEventAsync(notification, cancellationToken);
}
} }

View File

@ -99,7 +99,7 @@ public static class ContextAwareCoroutineExtensions
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
return contextAware return contextAware
.PublishEventAsync(notification, cancellationToken) .PublishAsync(notification, cancellationToken)
.AsTask() .AsTask()
.ToCoroutineEnumerator() .ToCoroutineEnumerator()
.RunCoroutine(segment, tag); .RunCoroutine(segment, tag);