feat(architecture): 为架构服务添加上下文支持并改进事件系统

- 在 ArchitectureServices 中添加 SetContext 和 GetContext 方法
- 为 IArchitectureServices 接口添加 IContextAware 继承
- 在架构初始化过程中设置服务上下文
- 将事件系统的 GetEvent 方法替换为 GetOrAddEvent 方法
- 重构测试类添加测试装置和拆卸逻辑
- 为测试类添加 NonParallelizable 特性确保测试隔离
This commit is contained in:
GwWuYou 2025-12-29 21:42:52 +08:00
parent 8130cf7fb0
commit 56ff201f94
5 changed files with 50 additions and 19 deletions

View File

@ -1,12 +1,13 @@
using GFramework.Core.Abstractions.events;
using GFramework.Core.Abstractions.ioc;
using GFramework.Core.Abstractions.rule;
namespace GFramework.Core.Abstractions.architecture;
/// <summary>
/// 架构服务接口,定义了框架核心架构所需的服务组件
/// </summary>
public interface IArchitectureServices
public interface IArchitectureServices : IContextAware
{
/// <summary>
/// 获取依赖注入容器

View File

@ -9,39 +9,49 @@ using NUnit.Framework;
namespace GFramework.Core.Tests.tests;
[TestFixture]
[NonParallelizable]
public class ArchitectureInitializationTests
{
[SetUp]
public void SetUp()
{
_architecture = new TestArchitecture();
}
[TearDown]
public void TearDown()
{
_architecture!.Destroy();
_architecture = null;
}
private TestArchitecture? _architecture;
[Test]
public void Architecture_Should_Initialize_All_Components_Correctly()
{
// Arrange
var architecture = new TestArchitecture();
// Act
architecture.Initialize();
_architecture!.Initialize();
// Assert - Init() 被调用
Assert.That(architecture.InitCalled, Is.True, "Architecture.Init() should be called");
// Assert
Assert.That(_architecture.InitCalled, Is.True);
// Assert - Runtime 已创建
Assert.That(architecture.Runtime, Is.Not.Null, "ArchitectureRuntime should be created");
Assert.That(_architecture.Runtime, Is.Not.Null);
// Assert - Phase 已进入 Ready
var phaseProperty = typeof(Architecture)
.GetProperty("CurrentPhase", BindingFlags.Instance | BindingFlags.NonPublic);
var phase = (ArchitecturePhase)phaseProperty!.GetValue(architecture)!;
Assert.That(phase, Is.EqualTo(ArchitecturePhase.Ready), "Architecture should be in Ready phase");
var phase = (ArchitecturePhase)phaseProperty!.GetValue(_architecture)!;
Assert.That(phase, Is.EqualTo(ArchitecturePhase.Ready));
var context = _architecture.Context;
// Assert - Model 初始化
var context = architecture.Context;
var model = context.GetModel<TestModel>();
Assert.That(model, Is.Not.Null);
Assert.That(model.Inited, Is.True, "Model should be initialized");
Assert.That(model!.Inited, Is.True);
// Assert - System 初始化
var system = context.GetSystem<TestSystem>();
Assert.That(system, Is.Not.Null);
Assert.That(system.Inited, Is.True, "System should be initialized");
Assert.That(system!.Inited, Is.True);
}
}

View File

@ -250,6 +250,8 @@ public abstract class Architecture(
// 创建架构运行时实例
Runtime = new ArchitectureRuntime(_context);
((ArchitectureContext)_context).Runtime = Runtime;
// 设置服务的上下文
Services.SetContext(_context);
// 调用用户实现的初始化
Init();
@ -301,6 +303,8 @@ public abstract class Architecture(
// 创建架构运行时实例
Runtime = new ArchitectureRuntime(_context);
((ArchitectureContext)_context).Runtime = Runtime;
// 设置服务的上下文
Services.SetContext(_context);
// 调用用户实现的初始化
Init();

View File

@ -8,6 +8,18 @@ namespace GFramework.Core.architecture;
public class ArchitectureServices : IArchitectureServices
{
private IArchitectureContext _context = null!;
public IIocContainer Container { get; } = new IocContainer();
public ITypeEventSystem TypeEventSystem { get; } = new TypeEventSystem();
public void SetContext(IArchitectureContext context)
{
_context = context;
Container.SetContext(context);
}
public IArchitectureContext GetContext()
{
return _context;
}
}

View File

@ -15,7 +15,9 @@ public class TypeEventSystem : ITypeEventSystem
/// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
public void Send<T>() where T : new()
{
_mEvents.GetEvent<EasyEvent<T>>().Trigger(new T());
_mEvents
.GetOrAddEvent<EasyEvent<T>>()
.Trigger(new T());
}
/// <summary>
@ -25,7 +27,9 @@ public class TypeEventSystem : ITypeEventSystem
/// <param name="e">事件实例</param>
public void Send<T>(T e)
{
_mEvents.GetEvent<EasyEvent<T>>().Trigger(e);
_mEvents
.GetOrAddEvent<EasyEvent<T>>()
.Trigger(e);
}
/// <summary>