using System.Collections.Concurrent;
namespace GFramework.Core.Abstractions.Architectures;
///
/// 架构模块注册表 - 用于外部模块的自动注册
///
public static class ArchitectureModuleRegistry
{
private static readonly ConcurrentDictionary> Factories = new(StringComparer.Ordinal);
///
/// 注册模块工厂(幂等操作,相同模块名只会注册一次)
///
/// 模块工厂函数
public static void Register(Func factory)
{
// 创建临时实例以获取模块名(用于幂等性检查)
var tempModule = factory();
var moduleName = tempModule.ModuleName;
// 幂等注册:相同模块名只注册一次
Factories.TryAdd(moduleName, factory);
}
///
/// 创建所有已注册的模块实例
///
/// 模块实例集合
public static IEnumerable CreateModules()
{
return Factories.Values.Select(f => f());
}
///
/// 清空注册表(主要用于测试)
///
public static void Clear()
{
Factories.Clear();
}
}