Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions GFramework.Core.Abstractions/architecture/IArchitectureContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,55 @@ public interface IArchitectureContext
/// <returns>服务实例,如果不存在则返回null</returns>
TService? GetService<TService>() where TService : class;

/// <summary>
/// 获取指定类型的所有服务实例
/// </summary>
/// <typeparam name="TService">服务类型</typeparam>
/// <returns>所有符合条件的服务实例列表</returns>
IReadOnlyList<TService> GetServices<TService>() where TService : class;

/// <summary>
/// 获取指定类型的系统实例
/// </summary>
/// <typeparam name="TSystem">系统类型,必须继承自ISystem接口</typeparam>
/// <returns>系统实例,如果不存在则返回null</returns>
TSystem? GetSystem<TSystem>() where TSystem : class, ISystem;

/// <summary>
/// 获取指定类型的所有系统实例
/// </summary>
/// <typeparam name="TSystem">系统类型,必须继承自ISystem接口</typeparam>
/// <returns>所有符合条件的系统实例列表</returns>
IReadOnlyList<TSystem> GetSystems<TSystem>() where TSystem : class, ISystem;

/// <summary>
/// 获取指定类型的模型实例
/// </summary>
/// <typeparam name="TModel">模型类型,必须继承自IModel接口</typeparam>
/// <returns>模型实例,如果不存在则返回null</returns>
TModel? GetModel<TModel>() where TModel : class, IModel;

/// <summary>
/// 获取指定类型的所有模型实例
/// </summary>
/// <typeparam name="TModel">模型类型,必须继承自IModel接口</typeparam>
/// <returns>所有符合条件的模型实例列表</returns>
IReadOnlyList<TModel> GetModels<TModel>() where TModel : class, IModel;

/// <summary>
/// 获取指定类型的工具类实例
/// </summary>
/// <typeparam name="TUtility">工具类类型,必须继承自IUtility接口</typeparam>
/// <returns>工具类实例,如果不存在则返回null</returns>
TUtility? GetUtility<TUtility>() where TUtility : class, IUtility;

/// <summary>
/// 获取指定类型的所有工具类实例
/// </summary>
/// <typeparam name="TUtility">工具类类型,必须继承自IUtility接口</typeparam>
/// <returns>所有符合条件的工具类实例列表</returns>
IReadOnlyList<TUtility> GetUtilities<TUtility>() where TUtility : class, IUtility;

/// <summary>
/// 发送一个命令
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,41 @@ public class TestArchitectureContextV3 : IArchitectureContext
return _container.Get<TService>();
}

public IReadOnlyList<TService> GetServices<TService>() where TService : class
{
return _container.GetAll<TService>();
}

public TModel? GetModel<TModel>() where TModel : class, IModel
{
return _container.Get<TModel>();
}

public IReadOnlyList<TModel> GetModels<TModel>() where TModel : class, IModel
{
return _container.GetAll<TModel>();
}

public TSystem? GetSystem<TSystem>() where TSystem : class, ISystem
{
return _container.Get<TSystem>();
}

public IReadOnlyList<TSystem> GetSystems<TSystem>() where TSystem : class, ISystem
{
return _container.GetAll<TSystem>();
}

public TUtility? GetUtility<TUtility>() where TUtility : class, IUtility
{
return _container.Get<TUtility>();
}

public IReadOnlyList<TUtility> GetUtilities<TUtility>() where TUtility : class, IUtility
{
return _container.GetAll<TUtility>();
}

public void SendEvent<TEvent>() where TEvent : new()
{
}
Expand Down
40 changes: 40 additions & 0 deletions GFramework.Core.Tests/architecture/GameContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ public class TestArchitectureContext : IArchitectureContext
return _container.Get<TService>();
}

/// <summary>
/// 获取指定类型的所有服务
/// </summary>
/// <typeparam name="TService">服务类型</typeparam>
/// <returns>所有服务实例列表</returns>
public IReadOnlyList<TService> GetServices<TService>() where TService : class
{
return _container.GetAll<TService>();
}

/// <summary>
/// 获取指定类型的模型
/// </summary>
Expand All @@ -278,6 +288,16 @@ public class TestArchitectureContext : IArchitectureContext
return _container.Get<TModel>();
}

/// <summary>
/// 获取指定类型的所有模型
/// </summary>
/// <typeparam name="TModel">模型类型</typeparam>
/// <returns>所有模型实例列表</returns>
public IReadOnlyList<TModel> GetModels<TModel>() where TModel : class, IModel
{
return _container.GetAll<TModel>();
}

/// <summary>
/// 获取指定类型的系统
/// </summary>
Expand All @@ -288,6 +308,16 @@ public class TestArchitectureContext : IArchitectureContext
return _container.Get<TSystem>();
}

/// <summary>
/// 获取指定类型的所有系统
/// </summary>
/// <typeparam name="TSystem">系统类型</typeparam>
/// <returns>所有系统实例列表</returns>
public IReadOnlyList<TSystem> GetSystems<TSystem>() where TSystem : class, ISystem
{
return _container.GetAll<TSystem>();
}

/// <summary>
/// 获取指定类型的工具
/// </summary>
Expand All @@ -298,6 +328,16 @@ public class TestArchitectureContext : IArchitectureContext
return _container.Get<TUtility>();
}

/// <summary>
/// 获取指定类型的所有工具
/// </summary>
/// <typeparam name="TUtility">工具类型</typeparam>
/// <returns>所有工具实例列表</returns>
public IReadOnlyList<TUtility> GetUtilities<TUtility>() where TUtility : class, IUtility
{
return _container.GetAll<TUtility>();
}

/// <summary>
/// 发送事件
/// </summary>
Expand Down
133 changes: 133 additions & 0 deletions GFramework.Core.Tests/rule/ContextAwareEnvironmentExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using GFramework.Core.Abstractions.environment;
using GFramework.Core.Abstractions.rule;
using GFramework.Core.architecture;
using GFramework.Core.extensions;
using GFramework.Core.ioc;
using GFramework.Core.rule;
using NUnit.Framework;

namespace GFramework.Core.Tests.rule;

/// <summary>
/// 测试 ContextAwareEnvironmentExtensions 的单元测试类
/// 验证环境对象的获取功能
/// </summary>
[TestFixture]
public class ContextAwareEnvironmentExtensionsTests
{
[SetUp]
public void SetUp()
{
_container = new MicrosoftDiContainer();
_context = new ArchitectureContext(_container);
_contextAware = new TestContextAware();

((IContextAware)_contextAware).SetContext(_context);
}

[TearDown]
public void TearDown()
{
_container.Clear();
}

private TestContextAware _contextAware = null!;
private ArchitectureContext _context = null!;
private MicrosoftDiContainer _container = null!;

[Test]
public void GetEnvironment_Should_Return_Registered_Environment()
{
// Arrange
var environment = new TestEnvironment();
_container.Register<IEnvironment>(environment);
_container.Freeze();

// Act
var result = _contextAware.GetEnvironment();

// Assert
Assert.That(result, Is.SameAs(environment));
}

[Test]
public void GetEnvironment_Generic_Should_Return_Typed_Environment()
{
// Arrange
var environment = new TestEnvironment { Name = "TestEnv" };
_container.Register<IEnvironment>(environment);
_container.Freeze();

// Act
var result = _contextAware.GetEnvironment<TestEnvironment>();

// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.SameAs(environment));
Assert.That(result!.Name, Is.EqualTo("TestEnv"));
}

[Test]
public void GetEnvironment_Generic_Should_Return_Null_When_Type_Mismatch()
{
// Arrange
var environment = new TestEnvironment();
_container.Register<IEnvironment>(environment);
_container.Freeze();

// Act
var result = _contextAware.GetEnvironment<AnotherEnvironment>();

// Assert
Assert.That(result, Is.Null);
}

private class TestEnvironment : IEnvironment
{
public string Name { get; set; } = string.Empty;

public T? Get<T>(string key) where T : class => default;

public bool TryGet<T>(string key, out T value) where T : class
{
value = default!;
return false;
}

public T GetRequired<T>(string key) where T : class => throw new NotImplementedException();

public void Register(string key, object value)
{
}

public void Initialize()
{
}
}

private class AnotherEnvironment : IEnvironment
{
public string Name => "Another";
public T? Get<T>(string key) where T : class => default;

public bool TryGet<T>(string key, out T value) where T : class
{
value = default!;
return false;
}

public T GetRequired<T>(string key) where T : class => throw new NotImplementedException();

public void Register(string key, object value)
{
}

public void Initialize()
{
}
}

private class TestContextAware : ContextAwareBase
{
}
}
Loading
Loading