mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(ioc): 重构依赖注入容器和模型上下文管理
- 将IocContainer的Init方法重命名为OnContextReady并设为protected override - 重构AbstractModel中的Architecture字段为_context属性并实现IContextAware接口 - 移除GetArchitecture和SetArchitecture方法,添加GetContext和SetContext方法 - 为IModel接口添加IContextAware继承 - 添加TestArchitecture、TestModel和TestSystem测试类 - 创建ArchitectureInitializationTests测试用例验证组件初始化 - 更新项目文件添加NUnit包引用和测试项目配置 - 在解决方案文件中添加测试项目引用
This commit is contained in:
parent
603b06325d
commit
8130cf7fb0
@ -1,9 +1,11 @@
|
||||
namespace GFramework.Core.Abstractions.model;
|
||||
using GFramework.Core.Abstractions.rule;
|
||||
|
||||
namespace GFramework.Core.Abstractions.model;
|
||||
|
||||
/// <summary>
|
||||
/// 模型接口,定义了模型的基本行为和功能
|
||||
/// </summary>
|
||||
public interface IModel
|
||||
public interface IModel : IContextAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化模型
|
||||
|
||||
18
GFramework.Core.Tests/GFramework.Core.Tests.csproj
Normal file
18
GFramework.Core.Tests/GFramework.Core.Tests.csproj
Normal file
@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1"/>
|
||||
<PackageReference Include="NUnit" Version="4.4.0"/>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="6.0.1"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GFramework.Core\GFramework.Core.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
18
GFramework.Core.Tests/architecture/TestArchitecture.cs
Normal file
18
GFramework.Core.Tests/architecture/TestArchitecture.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.Tests.model;
|
||||
using GFramework.Core.Tests.system;
|
||||
|
||||
namespace GFramework.Core.Tests.architecture;
|
||||
|
||||
public sealed class TestArchitecture : Architecture
|
||||
{
|
||||
public bool InitCalled { get; private set; }
|
||||
|
||||
protected override void Init()
|
||||
{
|
||||
InitCalled = true;
|
||||
|
||||
RegisterModel(new TestModel());
|
||||
RegisterSystem(new TestSystem());
|
||||
}
|
||||
}
|
||||
35
GFramework.Core.Tests/model/TestModel.cs
Normal file
35
GFramework.Core.Tests/model/TestModel.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using GFramework.Core.Abstractions.architecture;
|
||||
using GFramework.Core.Abstractions.model;
|
||||
|
||||
namespace GFramework.Core.Tests.model;
|
||||
|
||||
/// <summary>
|
||||
/// 测试模型类,用于框架测试目的
|
||||
/// </summary>
|
||||
public sealed class TestModel : IModel
|
||||
{
|
||||
private IArchitectureContext _context = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型是否已初始化的状态
|
||||
/// </summary>
|
||||
public bool Inited { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化模型
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
Inited = true;
|
||||
}
|
||||
|
||||
public void SetContext(IArchitectureContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public IArchitectureContext GetContext()
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
}
|
||||
59
GFramework.Core.Tests/system/TestSystem.cs
Normal file
59
GFramework.Core.Tests/system/TestSystem.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using GFramework.Core.Abstractions.architecture;
|
||||
using GFramework.Core.Abstractions.system;
|
||||
|
||||
namespace GFramework.Core.Tests.system;
|
||||
|
||||
/// <summary>
|
||||
/// 测试系统类,实现了ISystem接口
|
||||
/// </summary>
|
||||
public sealed class TestSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// 架构上下文对象
|
||||
/// </summary>
|
||||
private IArchitectureContext _context = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取系统是否已初始化的状态
|
||||
/// </summary>
|
||||
public bool Inited { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取系统是否已销毁的状态
|
||||
/// </summary>
|
||||
public bool Destroyed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设置架构上下文
|
||||
/// </summary>
|
||||
/// <param name="context">架构上下文对象</param>
|
||||
public void SetContext(IArchitectureContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构上下文
|
||||
/// </summary>
|
||||
/// <returns>架构上下文对象</returns>
|
||||
public IArchitectureContext GetContext()
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化系统
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
Inited = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁系统
|
||||
/// </summary>
|
||||
public void Destroy()
|
||||
{
|
||||
Destroyed = true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
using System.Reflection;
|
||||
using GFramework.Core.Abstractions.enums;
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.Tests.architecture;
|
||||
using GFramework.Core.Tests.model;
|
||||
using GFramework.Core.Tests.system;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GFramework.Core.Tests.tests;
|
||||
|
||||
[TestFixture]
|
||||
public class ArchitectureInitializationTests
|
||||
{
|
||||
[Test]
|
||||
public void Architecture_Should_Initialize_All_Components_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
var architecture = new TestArchitecture();
|
||||
|
||||
// Act
|
||||
architecture.Initialize();
|
||||
|
||||
// Assert - Init() 被调用
|
||||
Assert.That(architecture.InitCalled, Is.True, "Architecture.Init() should be called");
|
||||
|
||||
// Assert - Runtime 已创建
|
||||
Assert.That(architecture.Runtime, Is.Not.Null, "ArchitectureRuntime should be created");
|
||||
|
||||
// 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");
|
||||
|
||||
// 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 - System 初始化
|
||||
var system = context.GetSystem<TestSystem>();
|
||||
Assert.That(system, Is.Not.Null);
|
||||
Assert.That(system.Inited, Is.True, "System should be initialized");
|
||||
}
|
||||
}
|
||||
@ -52,7 +52,7 @@ public class IocContainer : ContextAwareBase, IIocContainer
|
||||
|
||||
#region Register
|
||||
|
||||
public void Init()
|
||||
protected override void OnContextReady()
|
||||
{
|
||||
_logger = Context.LoggerFactory.GetLogger(nameof(IocContainer));
|
||||
}
|
||||
|
||||
@ -11,7 +11,7 @@ public abstract class AbstractModel : IModel
|
||||
/// <summary>
|
||||
/// 模型所属的架构实例
|
||||
/// </summary>
|
||||
protected IArchitecture Architecture;
|
||||
protected IArchitectureContext _context { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化模型,调用抽象方法OnInit执行具体初始化逻辑
|
||||
@ -21,24 +21,17 @@ public abstract class AbstractModel : IModel
|
||||
OnInit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型所属的架构实例
|
||||
/// </summary>
|
||||
/// <returns>返回当前模型关联的架构对象</returns>
|
||||
public IArchitecture GetArchitecture()
|
||||
public void SetContext(IArchitectureContext context)
|
||||
{
|
||||
return Architecture;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置模型所属的架构实例
|
||||
/// </summary>
|
||||
/// <param name="architecture">要关联到此模型的架构实例</param>
|
||||
public void SetArchitecture(IArchitecture architecture)
|
||||
public IArchitectureContext GetContext()
|
||||
{
|
||||
Architecture = architecture;
|
||||
return _context;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 抽象初始化方法,由子类实现具体的初始化逻辑
|
||||
/// </summary>
|
||||
|
||||
@ -46,6 +46,7 @@
|
||||
<None Remove="GFramework.Core.Abstractions\**"/>
|
||||
<None Remove="GFramework.Godot.Abstractions\**"/>
|
||||
<None Remove="GFramework.Game.Abstractions\**"/>
|
||||
<None Remove="GFramework.Core.Tests\**"/>
|
||||
</ItemGroup>
|
||||
<!-- 聚合核心模块 -->
|
||||
<ItemGroup>
|
||||
@ -75,6 +76,7 @@
|
||||
<Compile Remove="GFramework.Core.Abstractions\**"/>
|
||||
<Compile Remove="GFramework.Godot.Abstractions\**"/>
|
||||
<Compile Remove="GFramework.Game.Abstractions\**"/>
|
||||
<Compile Remove="GFramework.Core.Tests\**"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Remove="GFramework.Core\**"/>
|
||||
@ -90,9 +92,13 @@
|
||||
<EmbeddedResource Remove="GFramework.Core.Abstractions\**"/>
|
||||
<EmbeddedResource Remove="GFramework.Godot.Abstractions\**"/>
|
||||
<EmbeddedResource Remove="GFramework.Game.Abstractions\**"/>
|
||||
<EmbeddedResource Remove="GFramework.Core.Tests\**"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<AdditionalFiles Remove="AnalyzerReleases.Shipped.md"/>
|
||||
<AdditionalFiles Remove="AnalyzerReleases.Unshipped.md"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NUnit" Version="4.4.0"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -24,6 +24,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Core.Abstraction
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Game.Abstractions", "GFramework.Game.Abstractions\GFramework.Game.Abstractions.csproj", "{E20DBA4C-CEB9-4184-B614-5A99A9AE4472}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Core.Tests", "GFramework.Core.Tests\GFramework.Core.Tests.csproj", "{759BCD95-A9D9-4D8F-9255-A9F1B661DF74}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -78,5 +80,9 @@ Global
|
||||
{E20DBA4C-CEB9-4184-B614-5A99A9AE4472}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E20DBA4C-CEB9-4184-B614-5A99A9AE4472}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E20DBA4C-CEB9-4184-B614-5A99A9AE4472}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{759BCD95-A9D9-4D8F-9255-A9F1B661DF74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{759BCD95-A9D9-4D8F-9255-A9F1B661DF74}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{759BCD95-A9D9-4D8F-9255-A9F1B661DF74}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{759BCD95-A9D9-4D8F-9255-A9F1B661DF74}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user