Path: redb-route/redb.Route.Templates/TemplateRouteDefinitionExtensions.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.Templates/TemplateRouteDefinitionExtensions.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Templates;
///
/// DSL of the payload template step. A string argument is a locator (file path relative
/// to , or assembly:Name/path); inline text is
/// written as TextSource.Inline("...").
///
///
/// .SetBodyTemplate("Templates/order-confirm.json.sbn", MediaType.Json)
/// .SetBodyTemplate(TextSource.Inline("""{"id": {{ headers.orderId }}}"""), MediaType.Json)
/// .SetBodyTemplate("Templates/notify.xml.sbn", MediaType.Xml, a => a
/// .Set("customer", "header.customerId")
/// .Set("total", "header.amount * header.qty")
/// .SetValue("channel", "email"))
/// .SetHeaderTemplate("X-Summary", TextSource.Inline("{{ body.items | array.size }} items"), MediaType.Text)
///
///
///
public static class TemplateRouteDefinitionExtensions
{
/// Renders the template into the message body; ContentType follows .
public static IRouteDefinition SetBodyTemplate(this IRouteDefinition route, string locator, MediaType mediaType, Action? args = null)
=> SetBodyTemplate(route, TextSource.FromLocator(locator), mediaType, args);
/// Renders the template into the message body; ContentType follows .
public static IRouteDefinition SetBodyTemplate(this IRouteDefinition route, TextSource source, MediaType mediaType, Action? args = null)
=> Append(route, new PayloadTemplateDefinition(source, mediaType, TemplateTarget.Body, null, BuildArgs(args)));
/// Renders the template into header .
public static IRouteDefinition SetHeaderTemplate(this IRouteDefinition route, string name, string locator, MediaType mediaType, Action? args = null)
=> SetHeaderTemplate(route, name, TextSource.FromLocator(locator), mediaType, args);
/// Renders the template into header .
public static IRouteDefinition SetHeaderTemplate(this IRouteDefinition route, string name, TextSource source, MediaType mediaType, Action? args = null)
=> Append(route, new PayloadTemplateDefinition(source, mediaType, TemplateTarget.Header, name, BuildArgs(args)));
/// Renders the template into exchange property .
public static IRouteDefinition SetPropertyTemplate(this IRouteDefinition route, string key, string locator, MediaType mediaType, Action? args = null)
=> SetPropertyTemplate(route, key, TextSource.FromLocator(locator), mediaType, args);
/// Renders the template into exchange property .
public static IRouteDefinition SetPropertyTemplate(this IRouteDefinition route, string key, TextSource source, MediaType mediaType, Action? args = null)
=> Append(route, new PayloadTemplateDefinition(source, mediaType, TemplateTarget.Property, key, BuildArgs(args)));
private static TemplateArgs? BuildArgs(Action? configure)
{
if (configure is null) return null;
var args = new TemplateArgs();
configure(args);
return args;
}
/// Appends the node to the current scope — the public tree API, identical to ProcessorDefinition.AddOutput.
private static IRouteDefinition Append(IRouteDefinition route, PayloadTemplateDefinition definition)
{
ArgumentNullException.ThrowIfNull(route);
definition.Parent = route;
route.Outputs.Add(definition);
return route;
}
}