Path: redb-route/redb.Route.Xml/CoreElements/RouteBuilderContextContribution.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.Xml/CoreElements/RouteBuilderContextContribution.cs ---- using System.Xml.Linq; using redb.Route.Abstractions; using redb.Route.Components.Bean; using redb.Route.Core; namespace redb.Route.Xml.CoreElements; /// /// <routeBuilder type="Orders.Routes.OrdersRoute, Orders"/> in context.xml: adds a C# route builder to the /// context, as context.AddRoutes(new OrdersRoute()) does in code, so one package carries routes in markup and in the /// DSL, in one context. The type resolves through the context's type resolver, the same lookup as <bean type> /// (in a Tsak package: the package's own assemblies first). It must be a public, non-abstract class deriving from /// with a public parameterless constructor, the rule of AddRoutes(Assembly[]). /// internal sealed class RouteBuilderContextContribution : IXmlContextContribution { /// The one instance, in the core registry. internal static RouteBuilderContextContribution Instance { get; } = new(); // Not constructible from outside: a host that discovers package contributions by scanning assemblies for // IXmlElementContribution types with a parameterless constructor (redb.Tsak) must not register a core element twice. private RouteBuilderContextContribution() { } /// public string Name => "routeBuilder"; /// public XmlElementKind Kind => XmlElementKind.ContextLevel; /// public ElementSpec Spec => CoreContributions.Specs[Name]; /// public IRouteDefinition Apply(XElement element, IRouteDefinition current, XmlParseContext context) { context.AddError(element, " adds C# routes to the whole context — put it inside , not in a route."); return current; } /// public void ApplyContext(XElement element, RouteContext context, XmlParseContext parseContext) { if (parseContext.RequiredAttr(element, "type") is not { } typeName) return; var resolver = context.GetService() ?? DefaultBeanTypeResolver.Instance; if (resolver.Resolve(typeName) is not { } type) { parseContext.AddError(element, $": type '{typeName}' was not found in the loaded assemblies."); return; } if (Refusal(type) is { } reason) { parseContext.AddError(element, $": '{typeName}' {reason}"); return; } // The same builder twice compiles its routes twice and fails only at Start, on a duplicate route id. if (element.ElementsBeforeSelf().Any(e => e.Name == element.Name && e.Attribute("type")?.Value is { Length: > 0 } earlier && resolver.Resolve(earlier) == type) || context.RouteBuilders.Any(b => b.GetType() == type)) { parseContext.AddError(element, $": '{type.FullName}' is already added to this context."); return; } RouteBuilder builder; try { builder = (RouteBuilder)Activator.CreateInstance(type)!; } catch (System.Reflection.TargetInvocationException ex) { parseContext.AddError(element, $": '{typeName}' threw from its constructor: {(ex.InnerException ?? ex).Message}"); return; } // Added once the whole document is valid: a context.xml with an error further down adds no routes (in Tsak the // module faults, and its routes must not run in the context it shares). parseContext.WhenValid(() => context.AddRoutes(builder)); } /// /// Why cannot be a markup route builder, or null when it can. The base type is matched by full /// name, so the packaging check can hold a type loaded in its own load context against the same rule. /// internal static string? Refusal(Type type) { if (!DerivesFrom(type, typeof(RouteBuilder).FullName!)) return $"does not derive from {typeof(RouteBuilder).FullName}."; if (type.FullName == typeof(InlineRouteBuilder).FullName) return "is the inline builder of AddRoutes(Action), not a route builder of its own."; if (!type.IsVisible) return "is not public."; if (type.IsAbstract) return "is abstract."; if (type.ContainsGenericParameters) return "is an open generic type."; if (type.GetConstructor(Type.EmptyTypes) is null) return "has no public parameterless constructor; a builder named in markup is created without arguments. " + "Read configuration through {{key}} properties and reach beans by their registry name."; return null; } private static bool DerivesFrom(Type type, string baseFullName) { for (var current = type.BaseType; current is not null; current = current.BaseType) { if (current.FullName == baseFullName) return true; } return false; } }