mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-06 16:16:44 +08:00
- 实现了CqrsHandlerRegistryGenerator用于自动生成CQRS处理器注册器 - 添加了EnumExtensionsGenerator用于自动生成枚举相关的扩展方法 - 创建了ContextAwareGenerator为标记类自动生成IContextAware接口实现 - 支持运行时类型引用的安全编码和反射回退机制 - 实现了精确的运行时类型引用描述和泛型类型处理 - 添加了完整的诊断报告和错误处理机制
97 lines
4.5 KiB
C#
97 lines
4.5 KiB
C#
// <auto-generated/>
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
/// <summary>
|
|
/// 为当前规则类型补充自动生成的架构上下文访问实现。
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 生成代码会在实例级缓存首次解析到的上下文,并在未显式配置提供者时回退到 <see cref="GFramework.Core.Architectures.GameContextProvider" />。
|
|
/// 同一生成类型的所有实例共享一个静态上下文提供者;切换或重置提供者只会影响尚未缓存上下文的新实例或未初始化实例,
|
|
/// 已缓存的实例上下文需要通过 <see cref="GFramework.Core.Abstractions.Rule.IContextAware.SetContext(GFramework.Core.Abstractions.Architectures.IArchitectureContext)" /> 显式覆盖。
|
|
/// </remarks>
|
|
partial class MyRule : global::GFramework.Core.Abstractions.Rule.IContextAware
|
|
{
|
|
private global::GFramework.Core.Abstractions.Architectures.IArchitectureContext? _context;
|
|
private static global::GFramework.Core.Abstractions.Architectures.IArchitectureContextProvider? _contextProvider;
|
|
private static readonly object _contextSync = new();
|
|
|
|
/// <summary>
|
|
/// 获取当前实例绑定的架构上下文。
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 该属性会先返回通过 <c>IContextAware.SetContext(...)</c> 显式注入的实例上下文;若尚未设置,则在同一个同步域内惰性初始化共享提供者。
|
|
/// 当静态提供者尚未配置时,生成代码会回退到 <see cref="GFramework.Core.Architectures.GameContextProvider" />。
|
|
/// 一旦某个实例成功缓存上下文,后续 <see cref="SetContextProvider(GFramework.Core.Abstractions.Architectures.IArchitectureContextProvider)" />
|
|
/// 或 <see cref="ResetContextProvider" /> 不会自动清除此缓存;如需覆盖,请显式调用 <c>IContextAware.SetContext(...)</c>。
|
|
/// </remarks>
|
|
protected global::GFramework.Core.Abstractions.Architectures.IArchitectureContext Context
|
|
{
|
|
get
|
|
{
|
|
var context = _context;
|
|
if (context is not null)
|
|
{
|
|
return context;
|
|
}
|
|
|
|
// 在同一个同步域内协调懒加载与 provider 切换,避免读取到被并发重置的空提供者。
|
|
lock (_contextSync)
|
|
{
|
|
_contextProvider ??= new global::GFramework.Core.Architectures.GameContextProvider();
|
|
_context ??= _contextProvider.GetContext();
|
|
return _context;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 配置当前生成类型共享的上下文提供者。
|
|
/// </summary>
|
|
/// <param name="provider">后续懒加载上下文时要使用的提供者实例。</param>
|
|
/// <remarks>
|
|
/// 该方法使用与 <see cref="Context" /> 相同的同步锁,避免提供者切换与惰性初始化交错。
|
|
/// 已经缓存上下文的实例不会因为提供者切换而自动失效;该变更仅影响尚未初始化上下文的新实例或未缓存实例。
|
|
/// 如需覆盖已有实例的上下文,请显式调用 <c>IContextAware.SetContext(...)</c>。
|
|
/// </remarks>
|
|
public static void SetContextProvider(global::GFramework.Core.Abstractions.Architectures.IArchitectureContextProvider provider)
|
|
{
|
|
lock (_contextSync)
|
|
{
|
|
_contextProvider = provider;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置共享上下文提供者,使后续懒加载回退到默认提供者。
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 该方法主要用于测试清理或跨用例恢复默认行为。
|
|
/// 它不会清除已经缓存到实例字段中的上下文;只有后续尚未初始化上下文的实例会重新回退到 <see cref="GFramework.Core.Architectures.GameContextProvider" />。
|
|
/// 如需覆盖已有实例的上下文,请显式调用 <c>IContextAware.SetContext(...)</c>。
|
|
/// </remarks>
|
|
public static void ResetContextProvider()
|
|
{
|
|
lock (_contextSync)
|
|
{
|
|
_contextProvider = null;
|
|
}
|
|
}
|
|
|
|
void global::GFramework.Core.Abstractions.Rule.IContextAware.SetContext(global::GFramework.Core.Abstractions.Architectures.IArchitectureContext context)
|
|
{
|
|
// 与 Context getter 共享同一同步协议,避免显式注入被并发懒加载覆盖。
|
|
lock (_contextSync)
|
|
{
|
|
_context = context;
|
|
}
|
|
}
|
|
|
|
global::GFramework.Core.Abstractions.Architectures.IArchitectureContext global::GFramework.Core.Abstractions.Rule.IContextAware.GetContext()
|
|
{
|
|
return Context;
|
|
}
|
|
|
|
}
|