DispatcherBuilder
Namespace: RA.Utilities.Dispatcher.Builders
DispatcherBuilder<TRequest> and DispatcherBuilder<TRequest, TResponse> are the fluent builders returned by AddDispatcher<...>(). They configure the pipeline of a single request: decorations (behaviors) and validators.
📦 Type Definition
public class DispatcherBuilder<TRequest> : IDispatcherBuilder where TRequest : IRequest
{
public IServiceCollection Services { get; }
public DispatcherBuilder<TRequest> AddDecoration<TBehavior>() where TBehavior : class, IPipelineBehavior<TRequest>;
public DispatcherBuilder<TRequest> AddValidator<TValidator>() where TValidator : class, IValidator<TRequest>;
}
public class DispatcherBuilder<TRequest, TResponse> : IDispatcherBuilder where TRequest : IRequest<TResponse>
{
public IServiceCollection Services { get; }
public DispatcherBuilder<TRequest, TResponse> AddDecoration<TBehavior>() where TBehavior : class, IPipelineBehavior<TRequest, TResponse>;
public DispatcherBuilder<TRequest, TResponse> AddValidator<TValidator>() where TValidator : class, IValidator<TRequest>;
}
🎯 Member Breakdown
AddDecoration<TBehavior>()
Registers the pipeline behavior transiently under its behavior interface:
services.AddTransient<IPipelineBehavior<TRequest, TResponse>, TBehavior>();
Behaviors compose in registration order, outermost first — the first AddDecoration call wraps the rest of the pipeline. Auto-registered closed behaviors compose by message interface, then by [PipelineBehaviorOrder], then by type name; explicit decorations keep their position relative to AddDispatcher() (before → outside the auto-registered block, after → inside it).
To register a generic behavior once for every request instead of decorating a single request, add it to DispatcherOptions.Behaviors: options.Behaviors.Add(typeof(LoggingBehavior<,>)). Those registrations behave like decorations added before AddDispatcher() — they wrap the auto-registered block and keep list order.
AddValidator<TValidator>()
Two registrations in one call:
- The validator itself:
services.AddTransient<IValidator<TRequest>, TValidator>() - The
ValidationBehavior:services.AddTransient<IPipelineBehavior<TRequest[, TResponse]>, ValidationBehavior<TRequest[, TResponse]>>()
When using the generated dispatcher, DispatcherOptions.Validators performs the same two registrations in bulk — options.Validators.Add(typeof(TValidator), lifetime) infers the request and response types.
🚀 Usage
builder.Services
.AddDispatcher<CreateProductCommand, int, CreateProductHandler>()
.AddDecoration<LoggingBehavior<CreateProductCommand, int>>() // outermost — logs around everything
.AddValidator<CreateProductCommandValidator>(); // innermost — validates first
// Execution order: Logging → Validation → Handler
AddDispatcher also registers the handler itself (scoped). Since plain handlers are already auto-registered by the source generator, the registration simply stacks — you only need AddDispatcher when the feature also has behaviors or validators to add.
🔗 Related
NotificationDispatcherBuilder<TNotification>— the notification counterpart.DispatcherBuilderExtensions— the extension methods that return these builders.DispatcherOptions— bulk behavior and validator registration for the generated dispatcher.PipelineBehaviorOrderAttribute— ordering for auto-registered behaviors.