Path: redb-route/demo/redb.Route.Demo/Routes/DeepDslShowcaseRoutes.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/demo/redb.Route.Demo/Routes/DeepDslShowcaseRoutes.cs
----
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Demo.Routes;
///
/// Showcase of the rich, deeply-nested fluent DSL — the same patterns exercised by
/// DeepNestedDslTests in the test suite, but assembled into runnable demo routes.
///
/// What this file demonstrates:
/// • Choice with sibling When() / Otherwise() reachable through
/// IRouteDefinition after a sub-scope was just closed (e.g. .EndSplit().When(...)).
/// • Rich logging scope (.Log(LogLevel.X)) with multiple static and dynamic
/// messages, captured headers, properties and route id rendering.
/// • TryCatch around a step that may throw, with a rich log entry inside the
/// catch handler that prints the actual exception type via e.Exception.
/// • Mixed scope closers: typed EndChoice() / EndSplit() / EndLog()
/// and the universal End() that walks the parent chain to the nearest scope.
/// • Cascading EndChoice() from deep inside a Split inside a When — closes
/// every intermediate scope in one shot and lands back at the route root.
///
/// All comments are intentionally English-only since this file is part of the public demo.
///
internal sealed class DeepDslShowcaseRoutes : RouteBuilder
{
private readonly ILogger? _log;
/// Creates the showcase builder. The optional logger is used by inline lambdas.
public DeepDslShowcaseRoutes(ILogger? log) => _log = log;
/// Hook called by to register every showcase route.
protected override void Configure()
{
ConfigureChoiceWithSplitAndRichLog();
ConfigureTryCatchWithRichLogInCatch();
ConfigureCascadingEndChoiceFromDeepInside();
ConfigureUniversalEndChain();
}
// ─────────────────────────────────────────────────────────────────────────
// Showcase 1 — Choice → When → RichLog → Split → per-item Process + dynamic
// Debug log → EndLog → EndSplit, then a sibling When and an
// Otherwise reached via the IRouteDefinition extension methods.
//
// Triggered manually via Direct: send a body of either:
// - IEnumerable → list branch (split + upper-case)
// - non-empty string → single string branch
// - anything else → fallback branch
// ─────────────────────────────────────────────────────────────────────────
private void ConfigureChoiceWithSplitAndRichLog()
{
// A simple in-memory sink so the demo can assert what flowed through.
var processed = new ConcurrentBag();
From("direct://demo-choice-richlog")
.RouteId("demo-choice-richlog")
.SetHeader("step", "start")
.Choice()
// Branch 1: body is a collection of strings (and not a single string).
.When(e => e.In.Body is IEnumerable && e.In.Body is not string)
.SetHeader("branch", "list")
// Rich log: multiple pieces composed into one structured message.
// .Header("...") → captures a header value into the output.
// .Property("...") → captures an exchange property.
// .ShowRouteId(true) → prefixes the log line with [rId:].
.Log(LogLevel.Information)
.Message("opening list branch")
.Header("branch")
.Property("trace")
.ShowRouteId(true)
.EndLog()
// Split fans the items out one-by-one through the inner pipeline.
.Split(e => ((IEnumerable)e.In.Body!).Cast