refactor(architecture): 重构架构基类和模型实现

- 移除 Architecture 类中关于业务操作方法的注释说明
- 更新 AsyncTestModel 继承 AbstractModel 并移除上下文设置相关方法
- 添加 ITestModel 接口定义用于测试模型
- 在 SyncArchitectureTests 中添加事件接收和取消注册功能测试
- 创建 TestEvent 和 EmptyEvent 用于事件系统测试
- 修改 TestModel 继承 AbstractModel 并实现 ITestModel 接口
- 添加 TestQuery 类用于测试查询操作功能
- [skip ci]
This commit is contained in:
GeWuYou 2026-01-06 12:06:08 +08:00
parent e23a53c3d4
commit 4f01828543
7 changed files with 125 additions and 37 deletions

View File

@ -0,0 +1,8 @@
namespace GFramework.Core.Tests.events;
public sealed class TestEvent
{
public int ReceivedValue { get; init; }
}
public sealed class EmptyEvent;

View File

@ -1,16 +1,14 @@
using GFramework.Core.Abstractions.architecture;
using GFramework.Core.Abstractions.enums;
using GFramework.Core.Abstractions.model;
using GFramework.Core.model;
namespace GFramework.Core.Tests.model;
/// <summary>
/// 异步测试模型类实现了IModel和IAsyncInitializable接口
/// </summary>
public sealed class AsyncTestModel : IModel, IAsyncInitializable
public sealed class AsyncTestModel : AbstractModel, IAsyncInitializable
{
private IArchitectureContext _context = null!;
/// <summary>
/// 获取模型是否已初始化的标志
/// </summary>
@ -36,24 +34,6 @@ public sealed class AsyncTestModel : IModel, IAsyncInitializable
throw new InvalidOperationException("Sync Init should not be called");
}
/// <summary>
/// 设置架构上下文
/// </summary>
/// <param name="context">架构上下文对象</param>
public void SetContext(IArchitectureContext context)
{
_context = context;
}
/// <summary>
/// 获取架构上下文
/// </summary>
/// <returns>架构上下文对象</returns>
public IArchitectureContext GetContext()
{
return _context;
}
/// <summary>
/// 处理架构阶段事件
/// </summary>
@ -61,4 +41,8 @@ public sealed class AsyncTestModel : IModel, IAsyncInitializable
public void OnArchitecturePhase(ArchitecturePhase phase)
{
}
protected override void OnInit()
{
}
}

View File

@ -0,0 +1,8 @@
using GFramework.Core.Abstractions.model;
namespace GFramework.Core.Tests.model;
public interface ITestModel : IModel
{
int GetCurrentXp { get; }
}

View File

@ -1,15 +1,14 @@
using GFramework.Core.Abstractions.architecture;
using GFramework.Core.Abstractions.enums;
using GFramework.Core.Abstractions.model;
using GFramework.Core.Abstractions.enums;
using GFramework.Core.model;
namespace GFramework.Core.Tests.model;
/// <summary>
/// 测试模型类,用于框架测试目的
/// </summary>
public sealed class TestModel : IModel
public sealed class TestModel : AbstractModel, ITestModel
{
private IArchitectureContext _context = null!;
public const int DefaultXp = 5;
/// <summary>
/// 获取模型是否已初始化的状态
@ -24,17 +23,14 @@ public sealed class TestModel : IModel
Initialized = true;
}
public void SetContext(IArchitectureContext context)
{
_context = context;
}
public IArchitectureContext GetContext()
{
return _context;
}
public void OnArchitecturePhase(ArchitecturePhase phase)
{
}
public int GetCurrentXp { get; } = DefaultXp;
protected override void OnInit()
{
}
}

View File

@ -0,0 +1,28 @@
using GFramework.Core.Abstractions.query;
using GFramework.Core.extensions;
using GFramework.Core.query;
using GFramework.Core.Tests.model;
namespace GFramework.Core.Tests.query;
/// <summary>
/// 测试查询类,用于执行测试查询操作
/// </summary>
/// <param name="input">测试查询输入参数</param>
public class TestQuery(TestQueryInput input) : AbstractQuery<TestQueryInput, int>(input)
{
/// <summary>
/// 执行查询操作的重写方法
/// </summary>
/// <param name="input">测试查询输入参数</param>
/// <returns>返回固定的整数值42</returns>
protected override int OnDo(TestQueryInput input)
{
return this.GetModel<ITestModel>()!.GetCurrentXp;
}
}
/// <summary>
/// 测试查询输入类,实现查询输入接口
/// </summary>
public sealed class TestQueryInput : IQueryInput;

View File

@ -1,6 +1,7 @@
using GFramework.Core.Abstractions.enums;
using GFramework.Core.architecture;
using GFramework.Core.Tests.architecture;
using GFramework.Core.Tests.events;
using GFramework.Core.Tests.model;
using GFramework.Core.Tests.system;
using NUnit.Framework;
@ -137,4 +138,68 @@ public class SyncArchitectureTests : ArchitectureTestsBase<SyncTestArchitecture>
AssertInitializationFailed();
}
/// <summary>
/// 测试事件是否能够被正确接收和处理
/// </summary>
/// <remarks>
/// 该测试验证了事件系统的注册和发送功能,确保事件能够被正确传递给注册的处理器
/// </remarks>
[Test]
public void Event_Should_Be_Received()
{
Architecture!.Initialize();
var context = Architecture.Context;
var receivedValue = 0;
const int tagetValue = 100;
// 注册事件处理器将接收到的值赋给receivedValue变量
context.RegisterEvent<TestEvent>(e => { receivedValue = e.ReceivedValue; });
// 发送测试事件
context.SendEvent(new TestEvent
{
ReceivedValue = tagetValue
});
Assert.That(receivedValue, Is.EqualTo(tagetValue));
}
/// <summary>
/// 测试事件取消注册功能是否正常工作
/// </summary>
/// <remarks>
/// 该测试验证了事件处理器的取消注册功能,确保取消注册后事件处理器不再被调用
/// </remarks>
[Test]
public void Event_UnRegister_Should_Work()
{
Architecture!.Initialize();
var context = Architecture.Context;
var count = 0;
// 注册事件处理器并获取取消注册对象
var unRegister = context.RegisterEvent<EmptyEvent>(Handler);
// 发送第一个事件,此时处理器应该被调用
context.SendEvent(new EmptyEvent());
// 验证事件处理器被调用了一次
Assert.That(count, Is.EqualTo(1), "Handler should be called once before unregistration");
// 取消注册事件处理器
unRegister.UnRegister();
// 发送第二个事件,此时处理器不应该被调用
context.SendEvent(new EmptyEvent());
// 验证取消注册后,计数没有增加
Assert.That(count, Is.EqualTo(1), "Handler should not be called after unregistration");
void Handler(EmptyEvent _)
{
count++;
}
}
}

View File

@ -18,7 +18,6 @@ namespace GFramework.Core.architecture;
/// <summary>
/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。
/// 专注于生命周期管理、初始化流程控制和架构阶段转换。
/// 不直接提供业务操作方法,业务操作通过 ArchitectureRuntime 提供。
/// </summary>
public abstract class Architecture(
IArchitectureConfiguration? configuration = null,