Path: redb-route/redb.Route.Controllers/ControllerRegistry.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Controllers/ControllerRegistry.cs
----
using System.Collections.Concurrent;
using System.Reflection;
using System.Text.RegularExpressions;
using redb.Route.Controllers.Attributes;
namespace redb.Route.Controllers;
///
/// Route table entry holding controller type, method info, and compiled template segments.
///
public sealed record ControllerAction(
Type ControllerType,
MethodInfo Method,
HttpMethodType HttpMethod,
string Template,
string[] Segments);
///
/// Scans assemblies for subclasses, builds a route lookup table,
/// and resolves incoming (method, path) pairs to controller actions.
/// Thread-safe. Lookup is O(n) on segments but typically fast given small route counts.
///
public sealed class ControllerRegistry
{
private readonly ConcurrentBag _actions = new();
private static readonly Regex TemplateParamRegex = new(@"\{(\w+)\}", RegexOptions.Compiled);
/// Returns all registered controller actions.
public IReadOnlyCollection Actions => _actions.ToArray();
///
/// Scans an assembly for all non-abstract subclasses
/// and registers their attributed actions.
///
/// Assembly to scan.
/// Number of actions registered from this assembly.
public int RegisterAssembly(Assembly assembly)
{
ArgumentNullException.ThrowIfNull(assembly);
var count = 0;
foreach (var type in assembly.GetTypes())
{
if (type.IsAbstract || !type.IsSubclassOf(typeof(RedbController)))
continue;
count += RegisterController(type);
}
return count;
}
///
/// Registers a single controller type and its attributed actions.
///
/// Controller type to register.
/// Number of actions registered.
public int RegisterController(Type controllerType)
{
ArgumentNullException.ThrowIfNull(controllerType);
var routeAttr = controllerType.GetCustomAttribute();
var basePath = routeAttr?.Template ?? controllerType.Name.Replace("Controller", "").ToLowerInvariant();
var count = 0;
foreach (var method in controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
var httpAttr = method.GetCustomAttribute();
if (httpAttr is null)
continue;
var template = string.IsNullOrEmpty(httpAttr.Template)
? basePath
: $"{basePath}/{httpAttr.Template}";
var segments = template.Split('/', StringSplitOptions.RemoveEmptyEntries);
_actions.Add(new ControllerAction(controllerType, method, httpAttr.Method, template, segments));
count++;
}
return count;
}
///
/// Matches an incoming request path and HTTP method against the route table.
/// Extracts path parameters from template placeholders.
///
/// HTTP method (GET, POST, etc.).
/// Request path (e.g. "modules/123").
/// Extracted route parameters (e.g. {id} → "123").
/// Matched action or null if no match.
public ControllerAction? Resolve(HttpMethodType httpMethod, string path, out Dictionary routeParams)
{
routeParams = new Dictionary(StringComparer.OrdinalIgnoreCase);
var pathSegments = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
// Collect all matches, then pick the most specific. Specificity = fewer
// placeholder segments (literals win over {param}) so that e.g.
// DELETE /me/sessions/current
// wins over
// DELETE /me/sessions/{sessionId}
// The underlying store is a ConcurrentBag whose enumeration order is undefined,
// so without this explicit ranking collisions would be non-deterministic.
ControllerAction? best = null;
Dictionary? bestParams = null;
var bestPlaceholderCount = int.MaxValue;
foreach (var action in _actions)
{
if (action.HttpMethod != httpMethod)
continue;
if (action.Segments.Length != pathSegments.Length)
continue;
var matched = true;
var placeholderCount = 0;
var candidateParams = new Dictionary(StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < action.Segments.Length; i++)
{
var seg = action.Segments[i];
var match = TemplateParamRegex.Match(seg);
if (match.Success && match.Value == seg)
{
// Entire segment is a parameter placeholder
candidateParams[match.Groups[1].Value] = pathSegments[i];
placeholderCount++;
}
else if (!seg.Equals(pathSegments[i], StringComparison.OrdinalIgnoreCase))
{
matched = false;
break;
}
}
if (matched && placeholderCount < bestPlaceholderCount)
{
best = action;
bestParams = candidateParams;
bestPlaceholderCount = placeholderCount;
if (placeholderCount == 0)
break; // exact literal match — cannot be beaten
}
}
if (best is not null)
{
routeParams = bestParams!;
return best;
}
return null;
}
///
/// Resolves using string method name (case-insensitive) instead of enum.
/// Parses "GET", "POST", etc. to .
///
public ControllerAction? Resolve(string httpMethod, string path, out Dictionary routeParams)
{
if (!Enum.TryParse(httpMethod, ignoreCase: true, out var method))
{
routeParams = new Dictionary();
return null;
}
return Resolve(method, path, out routeParams);
}
}