A cohesive set of infrastructure libraries for dotnet that utilizes abstractions for event handling, persistence, unit of work, mediator, distributed messaging, event bus, CQRS, email, and more
$ dotnet add package RCommon.FluentValidationFluentValidation integration for RCommon's IValidationProvider abstraction. This package bridges the FluentValidation library into RCommon's validation pipeline, resolving registered IValidator<T> instances from the DI container and executing them with support for automatic CQRS command/query validation.
IValidationProvider using the FluentValidation libraryIValidator<T> instances for a given type from DITask.WhenAllValidationOutcome and ValidationFault typesCqrsValidationOptionsthrowOnFaults behavior to throw ValidationException on failuredotnet add package RCommon.FluentValidation
Register FluentValidation through the RCommon builder and add your validators:
using RCommon;
using RCommon.FluentValidation;
services.AddRCommon(builder =>
{
builder.WithValidation<FluentValidationBuilder>(validation =>
{
validation.AddValidator<CreateOrderDto, CreateOrderDtoValidator>();
// Or scan an assembly for all validators
validation.AddValidatorsFromAssembly(typeof(CreateOrderDtoValidator).Assembly);
});
});
To enable automatic validation in the CQRS pipeline:
services.AddRCommon(builder =>
{
builder.WithValidation<FluentValidationBuilder>(options =>
{
options.ValidateCommands = true;
options.ValidateQueries = true;
});
});
Inject and use IValidationProvider directly when needed:
public class OrderService
{
private readonly IValidationProvider _validator;
public OrderService(IValidationProvider validator)
{
_validator = validator;
}
public async Task CreateOrder(CreateOrderDto dto)
{
var outcome = await _validator.ValidateAsync(dto, throwOnFaults: true);
// If throwOnFaults is false, inspect the outcome manually
if (!outcome.IsValid)
{
foreach (var fault in outcome.Errors)
{
Console.WriteLine($"{fault.PropertyName}: {fault.ErrorMessage}");
}
}
}
}
| Type | Description |
|---|---|
FluentValidationProvider | IValidationProvider implementation that resolves and runs FluentValidation validators |
FluentValidationBuilder | Registers FluentValidationProvider into the DI container |
IFluentValidationBuilder | Builder interface exposing IServiceCollection for validator registration |
FluentValidationBuilderExtensions | Provides AddValidator<T, TValidator>(), AddValidatorsFromAssembly(), and assembly scanning methods |
ValidationBuilderExtensions | Provides WithValidation<T>() on IRCommonBuilder for pipeline registration |
For full documentation, visit rcommon.com.
Licensed under the Apache License, Version 2.0.