Path: redb-route/redb.Route.Controllers/Extensions/ControllerRouteExtensions.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/Extensions/ControllerRouteExtensions.cs
----
using redb.Route.Abstractions;
using redb.Route.Controllers;
using redb.Route.Controllers.Attributes;
namespace redb.Route.Controllers.Extensions;
///
/// DSL extension methods for dispatching exchanges to actions.
/// Provides five overloads of .RedbController() on .
///
public static class ControllerRouteExtensions
{
///
/// Dispatches to a controller action resolved from exchange headers (route.path, route.method).
/// Full auto-dispatch: the dispatcher reads headers and matches against the registry.
///
/// Route definition.
/// Controller registry with registered actions.
public static IRouteDefinition RedbController(this IRouteDefinition route, ControllerRegistry registry)
{
ArgumentNullException.ThrowIfNull(registry);
return route.Process((exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var dispatcher = new ControllerDispatcherProcessor(registry, context);
return dispatcher.Process(exchange, ct);
});
}
///
/// Dispatches to a specific controller type. Method is resolved from exchange header (route.method)
/// and path from (route.path), but only actions on are considered.
///
/// Controller type to dispatch to.
/// Route definition.
public static IRouteDefinition RedbController(this IRouteDefinition route)
where TController : RedbController
{
var registry = new ControllerRegistry();
registry.RegisterController(typeof(TController));
return route.RedbController(registry);
}
///
/// Dispatches to a specific method on a specific controller type.
/// Ignores route.method and route.path headers — directly invokes the named method.
///
/// Controller type.
/// Route definition.
/// Name of the method to invoke.
public static IRouteDefinition RedbController(this IRouteDefinition route, string methodName)
where TController : RedbController
{
ArgumentNullException.ThrowIfNull(methodName);
return route.Process(async (exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var method = typeof(TController).GetMethod(methodName,
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
if (method is null)
throw new InvalidOperationException($"Method '{methodName}' not found on {typeof(TController).Name}");
var controller = (RedbController)Activator.CreateInstance(typeof(TController))!;
controller.Context = context;
controller.Exchange = exchange;
var parameters = ParameterResolver.ResolveParameters(method, exchange, new Dictionary());
var result = await ActionInvoker.InvokeAsync(method, controller, parameters);
if (result is not null)
{
exchange.Out ??= exchange.In.Clone();
exchange.Out.Body = result;
exchange.Out.setHeader("status.code", 200);
}
});
}
///
/// Dispatches to a controller and method by string names.
/// Resolves the controller type from the registry, then invokes the named method.
///
/// Route definition.
/// Controller registry.
/// Controller name (without "Controller" suffix).
/// Method name to invoke.
public static IRouteDefinition RedbController(
this IRouteDefinition route,
ControllerRegistry registry,
string controllerName,
string methodName)
{
ArgumentNullException.ThrowIfNull(registry);
ArgumentNullException.ThrowIfNull(controllerName);
ArgumentNullException.ThrowIfNull(methodName);
return route.Process(async (exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var action = registry.Actions.FirstOrDefault(a =>
a.ControllerType.Name.Replace("Controller", "")
.Equals(controllerName, StringComparison.OrdinalIgnoreCase)
&& a.Method.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase));
if (action is null)
throw new InvalidOperationException(
$"No action found for controller '{controllerName}', method '{methodName}'");
var controller = (RedbController)Activator.CreateInstance(action.ControllerType)!;
controller.Context = context;
controller.Exchange = exchange;
var parameters = ParameterResolver.ResolveParameters(action.Method, exchange, new Dictionary());
var result = await ActionInvoker.InvokeAsync(action.Method, controller, parameters);
if (result is not null)
{
exchange.Out ??= exchange.In.Clone();
exchange.Out.Body = result;
exchange.Out.setHeader("status.code", 200);
}
});
}
///
/// Dispatches to a controller action using dynamic expressions for controller and method selection.
/// The expressions are evaluated at runtime from exchange data.
///
/// Route definition.
/// Controller registry.
/// Expression returning controller name from exchange.
/// Expression returning method name from exchange.
public static IRouteDefinition RedbController(
this IRouteDefinition route,
ControllerRegistry registry,
Func controllerExpr,
Func methodExpr)
{
ArgumentNullException.ThrowIfNull(registry);
ArgumentNullException.ThrowIfNull(controllerExpr);
ArgumentNullException.ThrowIfNull(methodExpr);
return route.Process(async (exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var controllerName = controllerExpr(exchange);
var methodName = methodExpr(exchange);
var action = registry.Actions.FirstOrDefault(a =>
a.ControllerType.Name.Replace("Controller", "")
.Equals(controllerName, StringComparison.OrdinalIgnoreCase)
&& a.Method.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase));
if (action is null)
throw new InvalidOperationException(
$"No action found for controller '{controllerName}', method '{methodName}'");
var controller = (RedbController)Activator.CreateInstance(action.ControllerType)!;
controller.Context = context;
controller.Exchange = exchange;
var parameters = ParameterResolver.ResolveParameters(action.Method, exchange, new Dictionary());
var result = await ActionInvoker.InvokeAsync(action.Method, controller, parameters);
if (result is not null)
{
exchange.Out ??= exchange.In.Clone();
exchange.Out.Body = result;
exchange.Out.setHeader("status.code", 200);
}
});
}
// ── SignalR controller dispatch ────────────────────────
///
/// Dispatches SignalR hub invocations to methods on .
/// Method name is read from redbSignalR.Method header; arguments are resolved
/// positionally from the exchange body.
/// No HTTP attributes required — all public instance methods are exposed.
///
/// Controller type whose methods become callable hub methods.
/// Route definition.
public static IRouteDefinition RedbSignalRController(this IRouteDefinition route)
where TController : RedbController
{
return route.Process((exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var dispatcher = new SignalRControllerDispatcher(context, typeof(TController));
return dispatcher.Process(exchange, ct);
});
}
///
/// Dispatches SignalR hub invocations to methods across multiple controller types.
/// Method name is read from redbSignalR.Method header. Use qualified format
/// "Controller.Method" (e.g. "Modules.GetAll") to disambiguate.
/// Unqualified names resolve to first match.
///
/// Route definition.
/// Controller types to register.
public static IRouteDefinition RedbSignalRController(
this IRouteDefinition route,
params Type[] controllerTypes)
{
return route.Process((exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var dispatcher = new SignalRControllerDispatcher(context, controllerTypes);
return dispatcher.Process(exchange, ct);
});
}
///
/// Dispatches SignalR hub invocations to controllers registered in an existing
/// . Reuses the registry as a source of controller types;
/// dispatch is by method name (not by HTTP method/path template).
///
/// Route definition.
/// Controller registry with registered controllers.
public static IRouteDefinition RedbSignalRController(
this IRouteDefinition route,
ControllerRegistry registry)
{
ArgumentNullException.ThrowIfNull(registry);
var types = registry.Actions.Select(a => a.ControllerType).Distinct().ToArray();
return route.RedbSignalRController(types);
}
// ── SOAP controller dispatch ──────────────────────────
///
/// Dispatches SOAP requests to methods on by operation. The operation is
/// read from redbSoap.operation (set by the SOAP consumer); the XML body binds to the method's
/// [FromBody] / single complex parameter, and the return value is XML-serialized into the response.
/// No HTTP attributes required. Use when the method
/// name differs from the operation.
///
/// Controller whose methods become SOAP operations.
/// Route definition (behind a Soap.Listen(...) consumer).
public static IRouteDefinition RedbSoapController(this IRouteDefinition route)
where TController : RedbController
{
return route.Process((exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var dispatcher = new SoapControllerDispatcher(context, typeof(TController));
return dispatcher.Process(exchange, ct);
});
}
///
/// Dispatches SOAP requests across multiple controller types by operation (redbSoap.operation). Use
/// the qualified form "Controller.Operation" to disambiguate; unqualified names resolve to the first
/// match.
///
/// Route definition.
/// Controller types to register.
public static IRouteDefinition RedbSoapController(
this IRouteDefinition route,
params Type[] controllerTypes)
{
return route.Process((exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var dispatcher = new SoapControllerDispatcher(context, controllerTypes);
return dispatcher.Process(exchange, ct);
});
}
///
/// Dispatches SOAP requests to controllers registered in an existing ,
/// reusing it as a source of controller types; dispatch is by SOAP operation, not HTTP method/path.
///
/// Route definition.
/// Controller registry with registered controllers.
public static IRouteDefinition RedbSoapController(
this IRouteDefinition route,
ControllerRegistry registry)
{
ArgumentNullException.ThrowIfNull(registry);
var types = registry.Actions.Select(a => a.ControllerType).Distinct().ToArray();
return route.RedbSoapController(types);
}
// ── HTTP controller dispatch ──────────────────────────
///
/// Dispatches HTTP requests to controller actions using REST conventions.
/// Reads redbHttp.Method and redbHttp.Path headers set by the HTTP consumer,
/// resolves actions from the , and binds parameters from
/// route templates (redbHttp.RouteParam.*), query strings (redbHttp.QueryParam.*),
/// and request body (byte[]).
/// This is the HTTP-native dispatch — no manual header translation needed.
///
/// Route definition.
/// Controller registry with registered actions.
public static IRouteDefinition RedbHttpController(this IRouteDefinition route, ControllerRegistry registry)
{
ArgumentNullException.ThrowIfNull(registry);
return route.Process((exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var dispatcher = new HttpControllerDispatcher(registry, context);
return dispatcher.Process(exchange, ct);
});
}
///
/// Dispatches HTTP requests to a specific controller type using REST conventions.
/// Reads redbHttp.Method and redbHttp.Path from the HTTP consumer.
/// Only actions on are considered.
///
/// Controller type to dispatch to.
/// Route definition.
public static IRouteDefinition RedbHttpController(this IRouteDefinition route)
where TController : RedbController
{
var registry = new ControllerRegistry();
registry.RegisterController(typeof(TController));
return route.RedbHttpController(registry);
}
// ── gRPC controller dispatch ──────────────────────────
///
/// Dispatches gRPC requests to methods on .
/// Method name is read from dispatch-method header (flows through gRPC metadata
/// unfiltered); body is deserialized from byte[] JSON for positional argument binding.
/// No HTTP attributes required — all public instance methods are exposed.
///
/// Controller type whose methods become callable via gRPC.
/// Route definition.
public static IRouteDefinition RedbGrpcController(this IRouteDefinition route)
where TController : RedbController
{
return route.Process((exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var dispatcher = new GrpcControllerDispatcher(context, typeof(TController));
return dispatcher.Process(exchange, ct);
});
}
///
/// Dispatches gRPC requests to methods across multiple controller types.
/// Method name is read from dispatch-method header. Use qualified format
/// "Controller.Method" (e.g. "Modules.GetAll") to disambiguate.
/// Unqualified names resolve to first match.
///
/// Route definition.
/// Controller types to register.
public static IRouteDefinition RedbGrpcController(
this IRouteDefinition route,
params Type[] controllerTypes)
{
return route.Process((exchange, ct) =>
{
var context = route.GetContext()
?? throw new InvalidOperationException("RouteContext is not available.");
var dispatcher = new GrpcControllerDispatcher(context, controllerTypes);
return dispatcher.Process(exchange, ct);
});
}
///
/// Dispatches gRPC requests to controllers registered in an existing
/// . Reuses the registry as a source of controller types;
/// dispatch is by method name (not by HTTP method/path template).
///
/// Route definition.
/// Controller registry with registered controllers.
public static IRouteDefinition RedbGrpcController(
this IRouteDefinition route,
ControllerRegistry registry)
{
ArgumentNullException.ThrowIfNull(registry);
var types = registry.Actions.Select(a => a.ControllerType).Distinct().ToArray();
return route.RedbGrpcController(types);
}
}