mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 引入 IArchitectureContextProvider 接口解耦上下文获取逻辑 - 创建 GameContextProvider 作为默认上下文提供者 - 添加 ScopedContextProvider 支持多架构实例场景 - 修改源代码生成器使用上下文提供者模式 - 增加 SetContextProvider 方法支持测试和多架构场景 - 添加 RegistryInitializationHookBase 简化注册表初始化逻辑
44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using GFramework.Core.Abstractions.architecture;
|
||
|
||
namespace GFramework.Core.architecture;
|
||
|
||
/// <summary>
|
||
/// 作用域上下文提供者,用于多架构实例场景
|
||
/// </summary>
|
||
public sealed class ScopedContextProvider : IArchitectureContextProvider
|
||
{
|
||
private readonly IArchitectureContext _context;
|
||
|
||
/// <summary>
|
||
/// 初始化作用域上下文提供者
|
||
/// </summary>
|
||
/// <param name="context">要绑定的架构上下文实例</param>
|
||
public ScopedContextProvider(IArchitectureContext context)
|
||
{
|
||
_context = context;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前的架构上下文
|
||
/// </summary>
|
||
/// <returns>架构上下文实例</returns>
|
||
public IArchitectureContext GetContext() => _context;
|
||
|
||
/// <summary>
|
||
/// 尝试获取指定类型的架构上下文
|
||
/// </summary>
|
||
/// <typeparam name="T">架构上下文类型</typeparam>
|
||
/// <param name="context">输出的上下文实例</param>
|
||
/// <returns>如果成功获取则返回true,否则返回false</returns>
|
||
public bool TryGetContext<T>(out T? context) where T : class, IArchitectureContext
|
||
{
|
||
if (_context is T typedContext)
|
||
{
|
||
context = typedContext;
|
||
return true;
|
||
}
|
||
|
||
context = null;
|
||
return false;
|
||
}
|
||
} |