Files
BotPages/BotPages.Core/Routing/RoutesRegistry.cs
FrigaT 41986987b1
All checks were successful
CI / build-test (push) Successful in 1m2s
чистка кода
2025-12-16 16:19:50 +03:00

42 lines
1.2 KiB
C#

using System.Reflection;
namespace BotPages.Core.Routing;
/// <summary>
/// Реестр маршрутов страниц.
/// </summary>
internal sealed class RoutesRegistry
{
private readonly Dictionary<string, Type> _routes = new(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Зарегистрировать маршрут для страницы.
/// </summary>
public void Map<TPage>(string template) where TPage : Page
{
if (_routes.ContainsKey(template))
throw new InvalidOperationException($"Route '{template}' is already mapped.");
_routes[template] = typeof(TPage);
}
/// <summary>
/// Найти страницу по маршруту.
/// </summary>
public Type? Resolve(string template) => _routes.TryGetValue(template, out var t) ? t : null;
/// <summary>
/// Получить снимок всех маршрутов.
/// </summary>
public IReadOnlyDictionary<string, Type> Snapshot() => _routes;
internal void Map(Type? type)
{
foreach (var attr in type.GetCustomAttributes<RouteAttribute>(inherit: true))
{
_routes.Add(attr.Template, type);
}
_routes.Add(type.FullName, type);
}
}