Table of Contents

Class FluentValidationServiceCollectionExtensions

Namespace
Trellis.FluentValidation
Assembly
Trellis.FluentValidation.dll

Extension methods for plugging FluentValidation into the Trellis Mediator validation stage.

public static class FluentValidationServiceCollectionExtensions
Inheritance
FluentValidationServiceCollectionExtensions
Inherited Members

Methods

AddTrellisFluentValidation(IServiceCollection)

Registers FluentValidationMessageValidatorAdapter<TMessage> as the open-generic IMessageValidator<TMessage> implementation. Every FluentValidation.IValidator<T> registered for the message in DI will then run inside the existing ValidationBehavior<TMessage, TResponse> and contribute its failures to the aggregated Error.UnprocessableContent response.

public static IServiceCollection AddTrellisFluentValidation(this IServiceCollection services)

Parameters

services IServiceCollection

The service collection.

Returns

IServiceCollection

The service collection for chaining.

Examples

services.AddTrellisBehaviors();
services.AddTrellisFluentValidation();
services.AddScoped<IValidator<CreateOrderCommand>, CreateOrderCommandValidator>();
services.AddTrellisUnitOfWork<AppDbContext>();

Remarks

This overload is AOT-friendly: it relies on .NET's open-generic DI registration to construct closed adapter types at runtime, with no reflection over assemblies. Validators must be registered explicitly (e.g., services.AddScoped<IValidator<CreateOrderCommand>, CreateOrderCommandValidator>()).

FluentValidation does not introduce an additional pipeline behavior; it extends the existing ValidationBehavior<TMessage, TResponse> via the IMessageValidator<TMessage> abstraction.

AddTrellisFluentValidation(IServiceCollection, params Assembly[])

Registers the FluentValidation adapter and scans the supplied assemblies for concrete FluentValidation.IValidator<T> implementations, registering each one as a scoped service.

[RequiresUnreferencedCode("Assembly scanning requires unreferenced types. Use the parameterless overload with explicit registration for AOT/trimming scenarios.")]
[RequiresDynamicCode("Constructs closed generic IValidator<T> service types at runtime. Use the parameterless overload for AOT scenarios.")]
public static IServiceCollection AddTrellisFluentValidation(this IServiceCollection services, params Assembly[] assemblies)

Parameters

services IServiceCollection

The service collection.

assemblies Assembly[]

The assemblies to scan for validator implementations.

Returns

IServiceCollection

The service collection for chaining.

Examples

services.AddTrellisFluentValidation(typeof(CreateOrderCommandValidator).Assembly);

Remarks

Uses reflection over GetTypes() and constructs closed FluentValidation.IValidator<T> service types at runtime, so this overload is not AOT or trimming compatible. For AOT scenarios, call the parameterless overload and register validators explicitly.