From 56ff201f942d57e8c573099b618e982ab5dca5fe Mon Sep 17 00:00:00 2001
From: GwWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Mon, 29 Dec 2025 21:42:52 +0800
Subject: [PATCH] =?UTF-8?q?feat(architecture):=20=E4=B8=BA=E6=9E=B6?=
=?UTF-8?q?=E6=9E=84=E6=9C=8D=E5=8A=A1=E6=B7=BB=E5=8A=A0=E4=B8=8A=E4=B8=8B?=
=?UTF-8?q?=E6=96=87=E6=94=AF=E6=8C=81=E5=B9=B6=E6=94=B9=E8=BF=9B=E4=BA=8B?=
=?UTF-8?q?=E4=BB=B6=E7=B3=BB=E7=BB=9F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 在 ArchitectureServices 中添加 SetContext 和 GetContext 方法
- 为 IArchitectureServices 接口添加 IContextAware 继承
- 在架构初始化过程中设置服务上下文
- 将事件系统的 GetEvent 方法替换为 GetOrAddEvent 方法
- 重构测试类添加测试装置和拆卸逻辑
- 为测试类添加 NonParallelizable 特性确保测试隔离
---
.../architecture/IArchitectureServices.cs | 3 +-
.../tests/ArchitectureInitializationTests.cs | 42 ++++++++++++-------
GFramework.Core/architecture/Architecture.cs | 4 ++
.../architecture/ArchitectureServices.cs | 12 ++++++
GFramework.Core/events/TypeEventSystem.cs | 8 +++-
5 files changed, 50 insertions(+), 19 deletions(-)
diff --git a/GFramework.Core.Abstractions/architecture/IArchitectureServices.cs b/GFramework.Core.Abstractions/architecture/IArchitectureServices.cs
index 34c2433..677f244 100644
--- a/GFramework.Core.Abstractions/architecture/IArchitectureServices.cs
+++ b/GFramework.Core.Abstractions/architecture/IArchitectureServices.cs
@@ -1,12 +1,13 @@
using GFramework.Core.Abstractions.events;
using GFramework.Core.Abstractions.ioc;
+using GFramework.Core.Abstractions.rule;
namespace GFramework.Core.Abstractions.architecture;
///
/// 架构服务接口,定义了框架核心架构所需的服务组件
///
-public interface IArchitectureServices
+public interface IArchitectureServices : IContextAware
{
///
/// 获取依赖注入容器
diff --git a/GFramework.Core.Tests/tests/ArchitectureInitializationTests.cs b/GFramework.Core.Tests/tests/ArchitectureInitializationTests.cs
index c1e3cc1..bd5f590 100644
--- a/GFramework.Core.Tests/tests/ArchitectureInitializationTests.cs
+++ b/GFramework.Core.Tests/tests/ArchitectureInitializationTests.cs
@@ -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();
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();
Assert.That(system, Is.Not.Null);
- Assert.That(system.Inited, Is.True, "System should be initialized");
+ Assert.That(system!.Inited, Is.True);
}
}
\ No newline at end of file
diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs
index 951f776..f9bc982 100644
--- a/GFramework.Core/architecture/Architecture.cs
+++ b/GFramework.Core/architecture/Architecture.cs
@@ -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();
diff --git a/GFramework.Core/architecture/ArchitectureServices.cs b/GFramework.Core/architecture/ArchitectureServices.cs
index d25ec5f..7b29217 100644
--- a/GFramework.Core/architecture/ArchitectureServices.cs
+++ b/GFramework.Core/architecture/ArchitectureServices.cs
@@ -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;
+ }
}
\ No newline at end of file
diff --git a/GFramework.Core/events/TypeEventSystem.cs b/GFramework.Core/events/TypeEventSystem.cs
index 5893056..3659406 100644
--- a/GFramework.Core/events/TypeEventSystem.cs
+++ b/GFramework.Core/events/TypeEventSystem.cs
@@ -15,7 +15,9 @@ public class TypeEventSystem : ITypeEventSystem
/// 事件类型,必须具有无参构造函数
public void Send() where T : new()
{
- _mEvents.GetEvent>().Trigger(new T());
+ _mEvents
+ .GetOrAddEvent>()
+ .Trigger(new T());
}
///
@@ -25,7 +27,9 @@ public class TypeEventSystem : ITypeEventSystem
/// 事件实例
public void Send(T e)
{
- _mEvents.GetEvent>().Trigger(e);
+ _mEvents
+ .GetOrAddEvent>()
+ .Trigger(e);
}
///