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

View File

@ -9,39 +9,49 @@ using NUnit.Framework;
namespace GFramework.Core.Tests.tests; namespace GFramework.Core.Tests.tests;
[TestFixture] [TestFixture]
[NonParallelizable]
public class ArchitectureInitializationTests public class ArchitectureInitializationTests
{ {
[SetUp]
public void SetUp()
{
_architecture = new TestArchitecture();
}
[TearDown]
public void TearDown()
{
_architecture!.Destroy();
_architecture = null;
}
private TestArchitecture? _architecture;
[Test] [Test]
public void Architecture_Should_Initialize_All_Components_Correctly() public void Architecture_Should_Initialize_All_Components_Correctly()
{ {
// Arrange
var architecture = new TestArchitecture();
// Act // Act
architecture.Initialize(); _architecture!.Initialize();
// Assert - Init() 被调用 // Assert
Assert.That(architecture.InitCalled, Is.True, "Architecture.Init() should be called"); Assert.That(_architecture.InitCalled, Is.True);
// Assert - Runtime 已创建 Assert.That(_architecture.Runtime, Is.Not.Null);
Assert.That(architecture.Runtime, Is.Not.Null, "ArchitectureRuntime should be created");
// Assert - Phase 已进入 Ready
var phaseProperty = typeof(Architecture) var phaseProperty = typeof(Architecture)
.GetProperty("CurrentPhase", BindingFlags.Instance | BindingFlags.NonPublic); .GetProperty("CurrentPhase", BindingFlags.Instance | BindingFlags.NonPublic);
var phase = (ArchitecturePhase)phaseProperty!.GetValue(architecture)!; var phase = (ArchitecturePhase)phaseProperty!.GetValue(_architecture)!;
Assert.That(phase, Is.EqualTo(ArchitecturePhase.Ready), "Architecture should be in Ready phase"); Assert.That(phase, Is.EqualTo(ArchitecturePhase.Ready));
var context = _architecture.Context;
// Assert - Model 初始化
var context = architecture.Context;
var model = context.GetModel<TestModel>(); var model = context.GetModel<TestModel>();
Assert.That(model, Is.Not.Null); 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>(); var system = context.GetSystem<TestSystem>();
Assert.That(system, Is.Not.Null); 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); Runtime = new ArchitectureRuntime(_context);
((ArchitectureContext)_context).Runtime = Runtime; ((ArchitectureContext)_context).Runtime = Runtime;
// 设置服务的上下文
Services.SetContext(_context);
// 调用用户实现的初始化 // 调用用户实现的初始化
Init(); Init();
@ -301,6 +303,8 @@ public abstract class Architecture(
// 创建架构运行时实例 // 创建架构运行时实例
Runtime = new ArchitectureRuntime(_context); Runtime = new ArchitectureRuntime(_context);
((ArchitectureContext)_context).Runtime = Runtime; ((ArchitectureContext)_context).Runtime = Runtime;
// 设置服务的上下文
Services.SetContext(_context);
// 调用用户实现的初始化 // 调用用户实现的初始化
Init(); Init();

View File

@ -8,6 +8,18 @@ namespace GFramework.Core.architecture;
public class ArchitectureServices : IArchitectureServices public class ArchitectureServices : IArchitectureServices
{ {
private IArchitectureContext _context = null!;
public IIocContainer Container { get; } = new IocContainer(); public IIocContainer Container { get; } = new IocContainer();
public ITypeEventSystem TypeEventSystem { get; } = new TypeEventSystem(); 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> /// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
public void Send<T>() where T : new() public void Send<T>() where T : new()
{ {
_mEvents.GetEvent<EasyEvent<T>>().Trigger(new T()); _mEvents
.GetOrAddEvent<EasyEvent<T>>()
.Trigger(new T());
} }
/// <summary> /// <summary>
@ -25,7 +27,9 @@ public class TypeEventSystem : ITypeEventSystem
/// <param name="e">事件实例</param> /// <param name="e">事件实例</param>
public void Send<T>(T e) public void Send<T>(T e)
{ {
_mEvents.GetEvent<EasyEvent<T>>().Trigger(e); _mEvents
.GetOrAddEvent<EasyEvent<T>>()
.Trigger(e);
} }
/// <summary> /// <summary>