AdaskoTheBeAsT.FluentValidation.SimpleInjector 12.0.0

dotnet add package AdaskoTheBeAsT.FluentValidation.SimpleInjector --version 12.0.0
                    
NuGet\Install-Package AdaskoTheBeAsT.FluentValidation.SimpleInjector -Version 12.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AdaskoTheBeAsT.FluentValidation.SimpleInjector" Version="12.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AdaskoTheBeAsT.FluentValidation.SimpleInjector" Version="12.0.0" />
                    
Directory.Packages.props
<PackageReference Include="AdaskoTheBeAsT.FluentValidation.SimpleInjector" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AdaskoTheBeAsT.FluentValidation.SimpleInjector --version 12.0.0
                    
#r "nuget: AdaskoTheBeAsT.FluentValidation.SimpleInjector, 12.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AdaskoTheBeAsT.FluentValidation.SimpleInjector@12.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AdaskoTheBeAsT.FluentValidation.SimpleInjector&version=12.0.0
                    
Install as a Cake Addin
#tool nuget:?package=AdaskoTheBeAsT.FluentValidation.SimpleInjector&version=12.0.0
                    
Install as a Cake Tool

AdaskoTheBeAsT.FluentValidation.SimpleInjector

Seamlessly integrate FluentValidation with SimpleInjector - automatic validator registration made simple.

CodeFactor Build Status Azure DevOps tests Azure DevOps coverage Quality Gate Status Nuget Nuget


Why Use This?

Stop manually registering validators one by one. This library automatically scans your assemblies and registers all IValidator<T> implementations with SimpleInjector, saving you time and reducing boilerplate code.

Key Features

  • Zero Configuration - Works out of the box with sensible defaults
  • Flexible Scanning - Register validators by assembly, marker types, or custom configuration
  • Lifecycle Control - Choose between Singleton, Scoped, or Transient lifestyles
  • Smart Registration - Supports both single validator and collection patterns
  • Selective Registration - Skip specific validators with [SkipValidatorRegistration] attribute
  • Multi-Target Support - Compatible with .NET 8.0, 9.0, and 10.0

Installation

dotnet add package AdaskoTheBeAsT.FluentValidation.SimpleInjector

Quick Start

Basic Usage

The simplest way to register all validators from an assembly:

using SimpleInjector;
using AdaskoTheBeAsT.FluentValidation.SimpleInjector;

var container = new Container();

// Register all validators from the assembly containing PersonValidator
container.AddFluentValidation(typeof(PersonValidator));

That's it! All validators in that assembly are now registered and ready to use.

Resolve and Use

var validator = container.GetInstance<IValidator<Person>>();
var result = validator.Validate(new Person { Name = "John" });

Usage Patterns

1. Register by Marker Types

Use types as markers to identify which assemblies to scan:

// Single assembly
container.AddFluentValidation(typeof(PersonValidator));

// Multiple assemblies
container.AddFluentValidation(
    typeof(PersonValidator),
    typeof(OrderValidator),
    typeof(ProductValidator)
);

2. Register by Assemblies

Directly specify assemblies to scan:

var assembly = typeof(PersonValidator).Assembly;
container.AddFluentValidation(assembly);

// Or multiple assemblies
var assemblies = new[] 
{ 
    typeof(PersonValidator).Assembly,
    typeof(OrderValidator).Assembly 
};
container.AddFluentValidation(assemblies);

3. Scan Solution Assemblies

Automatically discover and register validators from all assemblies in your solution:

public static class ValidatorConfig
{
    public static void RegisterValidators(Container container)
    {
        var assemblies = AppDomain.CurrentDomain
            .GetAssemblies()
            .Where(a => a.FullName.StartsWith("YourCompany."))
            .ToList();

        container.AddFluentValidation(assemblies);
    }
}

Advanced Configuration

Lifecycle Management

Choose how validators are instantiated and cached:

// Singleton (default) - one instance shared across the application
container.AddFluentValidation(cfg => 
{
    cfg.WithAssembliesToScan(assemblies);
    cfg.AsSingleton();
});

// Scoped - one instance per scope/request
container.AddFluentValidation(cfg => 
{
    cfg.WithAssembliesToScan(assemblies);
    cfg.AsScoped();
});

// Transient - new instance every time
container.AddFluentValidation(cfg => 
{
    cfg.WithAssembliesToScan(assemblies);
    cfg.AsTransient();
});

Registration Patterns

Single Validator Pattern (Default)

Registers one validator per type. Use this when you have one validator per model:

container.AddFluentValidation(cfg =>
{
    cfg.WithAssembliesToScan(assemblies);
    cfg.RegisterAsSingleValidator(); // Default
});

// Resolves to a single IValidator<Person>
var validator = container.GetInstance<IValidator<Person>>();
Validator Collection Pattern

Register multiple validators for the same type:

container.AddFluentValidation(cfg =>
{
    cfg.WithAssembliesToScan(assemblies);
    cfg.RegisterAsValidatorCollection();
});

// Resolves to multiple validators
var validators = container.GetAllInstances<IValidator<Person>>();

Selective Registration

Skip specific validators from auto-registration:

using AdaskoTheBeAsT.FluentValidation.SimpleInjector;

// This validator will be excluded from registration
[SkipValidatorRegistration]
public class PersonInternalValidator : AbstractValidator<Person>
{
    public PersonInternalValidator()
    {
        RuleFor(x => x.Name).NotEmpty();
    }
}

Use Case: When building composite validators using included rules:

public class PersonValidator : AbstractValidator<Person>
{
    public PersonValidator()
    {
        Include(new PersonInternalValidator());
        Include(new PersonExternalValidator());
    }
}

// Mark the included validators to prevent duplicate registration
[SkipValidatorRegistration]
public class PersonInternalValidator : AbstractValidator<Person> { ... }

[SkipValidatorRegistration]
public class PersonExternalValidator : AbstractValidator<Person> { ... }

Complete Configuration Example

using SimpleInjector;
using AdaskoTheBeAsT.FluentValidation.SimpleInjector;

public class Startup
{
    public void ConfigureServices()
    {
        var container = new Container();

        // Advanced configuration
        container.AddFluentValidation(cfg =>
        {
            // Specify assemblies to scan
            cfg.WithAssembliesToScan(
                typeof(PersonValidator).Assembly,
                typeof(OrderValidator).Assembly
            );

            // Set validator lifecycle
            cfg.AsScoped();

            // Use single validator pattern
            cfg.RegisterAsSingleValidator();
        });

        // Verify container configuration
        container.Verify();
    }
}

Real-World Examples

ASP.NET Core Integration

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Add SimpleInjector
        builder.Services.AddSimpleInjector(container =>
        {
            // Register validators with scoped lifetime
            container.AddFluentValidation(cfg =>
            {
                cfg.WithAssembliesToScan(typeof(Program).Assembly);
                cfg.AsScoped();
            });
        });

        var app = builder.Build();
        app.Run();
    }
}

MediatR Pipeline Integration

// Validation pipeline behavior
public class ValidationBehavior<TRequest, TResponse> 
    : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    private readonly IValidator<TRequest> _validator;

    public ValidationBehavior(IValidator<TRequest> validator)
    {
        _validator = validator;
    }

    public async Task<TResponse> Handle(
        TRequest request, 
        RequestHandlerDelegate<TResponse> next, 
        CancellationToken cancellationToken)
    {
        var validationResult = await _validator.ValidateAsync(request, cancellationToken);
        
        if (!validationResult.IsValid)
        {
            throw new ValidationException(validationResult.Errors);
        }

        return await next();
    }
}

Framework Support

.NET Version Support
.NET 10.0
.NET 9.0
.NET 8.0

Dependencies


Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


License

This project is licensed under the MIT License - see the LICENSE file for details.


Author

Adam "AdaskoTheBeAsT" Pluciński

If this library saved you time, consider giving it a ⭐ on GitHub!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
12.0.0 0 8/2/2026
11.0.0 446 11/12/2025
10.0.0 504 1/4/2025
9.2.0 425 8/18/2024
9.0.2 302 5/27/2024
9.0.1 365 1/27/2024
9.0.0 369 12/2/2023
8.2.0 374 7/16/2023
8.1.0 323 5/4/2023
7.1.0 499 1/22/2023
7.0.0 581 11/13/2022
6.3.2 588 10/17/2022
6.3.1 685 9/25/2022
6.3.0 669 9/7/2022
6.2.0 672 7/24/2022
6.1.0 673 7/5/2022
6.0.0 653 5/14/2022
5.3.4 781 2/9/2022
5.3.3 516 1/2/2022
5.3.2 621 10/30/2021
Loading failed

- update FluentValidation 12.1.1
     - update SimpleInjector 5.6.0