Path: redb-route/redb.Route/Expressions/ExpressionResolver.PrefixResolvers.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/Expressions/ExpressionResolver.PrefixResolvers.cs
----
using System;
using System.Collections.Concurrent;
using redb.Route.Abstractions;
namespace redb.Route.Expressions;
///
/// Partial class ExpressionResolver — pluggable prefix resolvers for
/// extending the expression DSL with domain-specific roots
/// (e.g. ${conversation.*}, ${tool.*}) without coupling
/// the core resolver to those domains.
///
public static partial class ExpressionResolver
{
private static readonly ConcurrentDictionary> _prefixResolvers =
new(StringComparer.OrdinalIgnoreCase);
///
/// Registers a resolver for a custom property prefix. The
/// must end with a dot (e.g. "conversation."); the resolver receives the
/// current exchange and the remainder of the property path after the prefix
/// (e.g. "id" for ${conversation.id}).
///
/// If multiple registrations target the same prefix, the latest one wins.
///
///
public static void RegisterPrefixResolver(string prefix, Func resolver)
{
ArgumentException.ThrowIfNullOrWhiteSpace(prefix);
ArgumentNullException.ThrowIfNull(resolver);
if (!prefix.EndsWith('.'))
throw new ArgumentException("Prefix must end with a dot, e.g. \"conversation.\"", nameof(prefix));
_prefixResolvers[prefix] = resolver;
}
///
/// Removes a previously registered prefix resolver. Returns true
/// if a resolver was removed.
///
public static bool UnregisterPrefixResolver(string prefix)
{
if (string.IsNullOrWhiteSpace(prefix)) return false;
return _prefixResolvers.TryRemove(prefix, out _);
}
///
/// Resolves the value of a property path against the registered prefix
/// resolvers. Returns (handled: true, value) if a resolver matched
/// the prefix; otherwise (handled: false, null).
///
internal static (bool Handled, object? Value) TryResolveByPrefix(IExchange exchange, string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
return (false, null);
// The exchange's own context first: a prefix a module registered there goes with the context.
if (exchange.Context?.GetService() is { } contextResolvers
&& contextResolvers.TryResolve(exchange, propertyName, out var contextValue))
return (true, contextValue);
if (_prefixResolvers.IsEmpty)
return (false, null);
foreach (var (prefix, resolver) in _prefixResolvers)
{
if (!propertyName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
continue;
var remainder = propertyName.Substring(prefix.Length);
DebugLog($"Prefix resolver '{prefix}' matched, remainder='{remainder}'");
return (true, resolver(exchange, remainder));
}
return (false, null);
}
}
///
/// The prefix resolvers of one route context: ${prefix.rest} roots that the context's own code (a module
/// loaded for it) contributes. They live as a service of the context, so they go when the context is disposed and
/// keep no delegate of an unloaded module alive; an expression evaluated on an exchange of the context consults
/// them before the process-wide ones of . Register through
/// context.RegisterPrefixResolver(prefix, resolver).
///
public sealed class ExpressionPrefixResolvers
{
private static readonly object CreateGate = new();
private readonly ConcurrentDictionary> _resolvers =
new(StringComparer.OrdinalIgnoreCase);
/// The resolvers of , created on first use.
public static ExpressionPrefixResolvers For(IRouteContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context.GetService() is { } existing)
return existing;
lock (CreateGate)
{
if (context.GetService() is { } raced)
return raced;
var created = new ExpressionPrefixResolvers();
context.AddService(typeof(ExpressionPrefixResolvers), created);
return created;
}
}
///
/// Registers for (ending with a dot, e.g. "order.");
/// a second registration of the prefix replaces the first.
///
public void Register(string prefix, Func resolver)
{
ArgumentException.ThrowIfNullOrWhiteSpace(prefix);
ArgumentNullException.ThrowIfNull(resolver);
if (!prefix.EndsWith('.'))
throw new ArgumentException("Prefix must end with a dot, e.g. \"order.\"", nameof(prefix));
_resolvers[prefix] = resolver;
}
/// Removes the resolver of ; true when there was one.
public bool Unregister(string prefix) => !string.IsNullOrWhiteSpace(prefix) && _resolvers.TryRemove(prefix, out _);
internal bool TryResolve(IExchange exchange, string propertyName, out object? value)
{
value = null;
if (_resolvers.IsEmpty)
return false;
foreach (var (prefix, resolver) in _resolvers)
{
if (!propertyName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
continue;
value = resolver(exchange, propertyName[prefix.Length..]);
return true;
}
return false;
}
}