Skip to main content

DispatcherOptions

Namespace: RA.Utilities.Dispatcher.Options

DispatcherOptions configures the AddDispatcher(Action<DispatcherOptions>) overload. It does three things:

  • Filters the registrations produced by the source generator before they take effect — the generated module initializer still queues everything, and the filter removes the descriptors that belong to excluded handlers (and to the pipeline/notification behaviors related to their messages).
  • Registers additional pipeline behaviors explicitly — including open generic behaviors, which the generator cannot auto-register.
  • Registers validators and their validation behavior together — one entry replaces the explicit IValidator<TRequest> and ValidationBehavior registrations.

Nothing is filtered by default: when both filter properties are null, every discovered handler and closed behavior registers exactly as described in Auto Registration. Behaviors and Validators are empty by default, so no extra behavior or validator is registered.

📦 Type Definition​

public sealed class DispatcherOptions
{
public List<Type>? IncludeHandlers { get; set; }
public List<Type>? ExcludeHandlers { get; set; }
public List<Type> Behaviors { get; }
public ServiceLifetime BehaviorLifetime { get; set; }
public Dictionary<Type, ServiceLifetime> Validators { get; }
}

🎯 Member Breakdown​

PropertyTypeDefaultDescription
IncludeHandlersList<Type>?null (ignored)When set, only the listed handlers — and the pipeline/notification behaviors related to the messages they handle — are registered.
ExcludeHandlersList<Type>?null (ignored)When set, the listed handlers are not registered, and neither are the behaviors related to a message once no other registered handler handles it.
BehaviorsList<Type>empty listPipeline behaviors to register explicitly, in chain order: the first entry is the outermost behavior. Accepts IPipelineBehavior<>, IPipelineBehavior<,>, and INotificationBehavior<> implementations, closed or open generic.
BehaviorLifetimeServiceLifetimeTransientThe lifetime used for every type in Behaviors.
ValidatorsDictionary<Type, ServiceLifetime>empty dictionaryValidator types mapped to their lifetime. Each entry registers IValidator<TRequest> and the matching ValidationBehavior, so validation works without per-request AddValidator or explicit behavior registrations.

Filtering rules​

  • Exclusions win over inclusions when a type is in both lists.
  • Behavior types are accepted in either list. A listed behavior is kept even when the handler of its message is filtered out (IncludeHandlers), and removed even when its handler is kept (ExcludeHandlers).
  • A request behavior is removed together with the handler of its request. A behavior whose request has no handler in the compilation at all is left alone unless IncludeHandlers restricts registration.
  • A notification behavior is removed only once no registered handler for its notification remains.
  • The filter applies to source-generated registrations only. Handlers registered explicitly with AddDispatcher<TRequest, TResponse, THandler>() / AddNotification<TNotification>(), behaviors added with AddDecoration<TBehavior>(), and behaviors listed in Behaviors are untouched.
  • Include and exclude are evaluated against the handler's implementation type, not the closed interface type. Registering the same handler twice (automatically and explicitly) leaves the explicit registration in place.

🧩 Registering pipeline behaviors in bulk​

Behaviors covers the cases the generator cannot: open generic behaviors, behaviors shared by many requests, and behaviors that must wrap the whole auto-registered block.

using Microsoft.Extensions.DependencyInjection;
using RA.Utilities.Dispatcher.Extensions;

builder.Services.AddDispatcher(options =>
{
// Open generic: registered once and applied to every matching request.
options.Behaviors.Add(typeof(MetricsBehavior<,>)); // outermost
options.Behaviors.Add(typeof(LoggingBehavior<,>)); // runs inside metrics

// Closed: applies to one request/response pair.
options.Behaviors.Add(typeof(CachingBehavior<GetOrderQuery, OrderDto>));

// Optional; Transient is the default.
options.BehaviorLifetime = ServiceLifetime.Transient;
});

Rules for the list:

  • Supported contracts — IPipelineBehavior<TRequest>, IPipelineBehavior<TRequest, TResponse>, and INotificationBehavior<TNotification>. A type implementing several of them is registered under each.
  • Chain order is list order — the first entry wraps the rest. Configured behaviors are registered before the generated registrations, so they form the outer layer around auto-registered closed behaviors; the [PipelineBehaviorOrder] attribute is not read for them, exactly as with AddDecoration.
  • Open generics must pass their own type parameters in order. MyBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> works; a partially closed mapping such as MyBehavior<T> : IPipelineBehavior<ConcreteRequest, T> is rejected at startup because the container cannot close it.
  • Registered with TryAddEnumerable — listing the same behavior twice, or calling AddDispatcher more than once, leaves a single registration. A closed behavior that the generator also auto-registers is registered once, at its configured (outer) position; the generated duplicate is removed.
  • Validated at startup — a non-class, abstract, or non-behavior type throws ArgumentException when AddDispatcher runs.
  • Transient is recommended for behaviors because they should not hold request-scoped state. The lifetime is configurable for advanced scenarios.

🧪 Registering validators in bulk​

Validators replaces the three registrations a validated request normally needs — the validator, the validation behavior, and (for the behavior) its position in the pipeline:

Before
services.AddTransient<IValidator<TodoCreateInput>, TodoCreateValidator>();
services.AddTransient<IPipelineBehavior<TodoCreateInput, TodoCreateOutput>, ValidationBehavior<TodoCreateInput, TodoCreateOutput>>();
services.AddDispatcher(options =>
{
options.Behaviors.Add(typeof(ValidationBehavior<TodoCreateInput, TodoCreateOutput>));
});
After
using Microsoft.Extensions.DependencyInjection;
using RA.Utilities.Dispatcher.Extensions;

services.AddDispatcher(options =>
{
options.Validators.Add(typeof(TodoCreateValidator), ServiceLifetime.Transient);
});

AddDispatcher infers the message types from the validator:

  • IValidator<TRequest> gives the request type.
  • If the request implements IRequest<TResponse>, the response type comes from there and ValidationBehavior<TRequest, TResponse> is registered.
  • If the request implements only IRequest, ValidationBehavior<TRequest> is registered.
  • A request type that implements neither cannot be validated in the pipeline; AddDispatcher throws ArgumentException.

Each entry registers both the validator and its validation behavior with the configured lifetime. The request must be sent through the generated IDispatcher — that is the code path that resolves behaviors.

Rules for the dictionary:

  • The key is the validator implementation type, and the value is its ServiceLifetime. The same lifetime is used for the validation behavior.
  • Validators must be non-abstract, closed classes implementing IValidator<TRequest>. Abstract and open generic validator types throw ArgumentException.
  • Several validators for one request are supported — each entry registers its validator, and they share the single validation behavior for that request.
  • Registered with TryAddEnumerable — a validator or behavior already registered manually, listed in Behaviors, or added by an earlier AddDispatcher call is not duplicated. To position the validation behavior explicitly, list it in Behaviors; the entry from Validators then only contributes the validator.
  • Validation runs inside the behaviors from Behaviors — validator behaviors are registered after that list, so they sit closer to the handler. With the default configuration, validation is the outermost behavior.
  • IncludeHandlers / ExcludeHandlers do not remove configured validators or their behaviors — they are explicit registrations.

🚀 Complete Example​

using Microsoft.Extensions.DependencyInjection;
using RA.Utilities.Dispatcher.Extensions;
using RA.Utilities.Dispatcher.Options;

// Register every discovered handler except the legacy importer, only the
// order slice in a trimmed-down worker host, plus two generic behaviors
// and the create-order validator:
builder.Services.AddDispatcher(options =>
{
options.IncludeHandlers = [typeof(CreateOrderHandler), typeof(GetOrderQueryHandler)];
options.ExcludeHandlers = [typeof(LegacyImportHandler)];

options.Behaviors.Add(typeof(MetricsBehavior<,>));
options.Behaviors.Add(typeof(LoggingBehavior<,>));

options.Validators.Add(typeof(CreateOrderCommandValidator), ServiceLifetime.Transient);
});

// Default: register everything the generator discovered, no extra behaviors.
builder.Services.AddDispatcher();

With the configuration above, CreateOrderHandler and GetOrderQueryHandler (plus the behaviors bound to their messages) register; LegacyImportHandler and any handler not listed do not. The two generic behaviors still register — filtering never removes explicit registrations. A behavior listed in ExcludeHandlers is removed even if its message's handler survives.