NativeFluentValidator.SourceGenerator.DependencyInjection 1.0.3

dotnet add package NativeFluentValidator.SourceGenerator.DependencyInjection --version 1.0.3
                    
NuGet\Install-Package NativeFluentValidator.SourceGenerator.DependencyInjection -Version 1.0.3
                    
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="NativeFluentValidator.SourceGenerator.DependencyInjection" Version="1.0.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NativeFluentValidator.SourceGenerator.DependencyInjection" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="NativeFluentValidator.SourceGenerator.DependencyInjection">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 NativeFluentValidator.SourceGenerator.DependencyInjection --version 1.0.3
                    
#r "nuget: NativeFluentValidator.SourceGenerator.DependencyInjection, 1.0.3"
                    
#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 NativeFluentValidator.SourceGenerator.DependencyInjection@1.0.3
                    
#: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=NativeFluentValidator.SourceGenerator.DependencyInjection&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=NativeFluentValidator.SourceGenerator.DependencyInjection&version=1.0.3
                    
Install as a Cake Tool

NativeFluentValidator.SourceGenerator.DependencyInjection

NuGet

AOT-first Source Generator for FluentValidation Validator Registration - Automatically registers all AbstractValidator<T> implementations without assembly scanning.

Features

  • 100% Native AOT Compatible - No reflection, no assembly scanning
  • Compile-time Registration - All validators discovered at build time
  • Seamless FluentValidation Integration - Works with existing FluentValidation patterns
  • IIncrementalGenerator - Fast, incremental builds

Installation

dotnet add package NativeFluentValidator.SourceGenerator.DependencyInjection
dotnet add package FluentValidation
dotnet add package FluentValidation.DependencyInjectionExtensions

Usage

1. Create your validators as usual

using FluentValidation;

public class User
{
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
}

public class UserValidator : AbstractValidator<User>
{
    public UserValidator()
    {
        RuleFor(x => x.Name).NotEmpty().MaximumLength(100);
        RuleFor(x => x.Email).NotEmpty().EmailAddress();
        RuleFor(x => x.Age).InclusiveBetween(0, 150);
    }
}

2. Register with generated method

using Microsoft.Extensions.DependencyInjection;
using NativeFluentValidator.SourceGenerator.DependencyInjection.Generated;

var services = new ServiceCollection();

// Generated method - registers all AbstractValidator implementations
services.AddNativeValidators();

Generated Code

The generator discovers all AbstractValidator<T> implementations and generates:

// Auto-generated
namespace NativeFluentValidator.SourceGenerator.DependencyInjection.Generated
{
    public static class NativeValidatorServiceCollectionExtensions
    {
        public static IServiceCollection AddNativeValidators(this IServiceCollection services)
        {
            // Scoped by default, matching FluentValidation conventions
            services.AddScoped<IValidator<User>, UserValidator>();
            services.AddScoped<IValidator<Order>, OrderValidator>();
            // ... all discovered validators
            return services;
        }
    }
}

Validator Discovery

The generator automatically finds:

  • Classes inheriting from AbstractValidator<T>
  • Non-abstract, non-generic classes only
  • Supports validators in any namespace

Diagnostics

Code Description
FV001 Validator must be a non-abstract class
FV002 Validator must have a public constructor

Requirements

  • .NET 10 SDK
  • C# 13+
  • FluentValidation package (v11+)

Why Use This?

FluentValidation's AddValidatorsFromAssembly() uses reflection to scan assemblies, which is incompatible with Native AOT. This generator eliminates that by discovering validators at compile-time and generating explicit registrations.

Example: Full Setup

using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using NativeFluentValidator.SourceGenerator.DependencyInjection.Generated;

var services = new ServiceCollection();
services.AddNativeValidators();

var provider = services.BuildServiceProvider();
var validator = provider.GetRequiredService<IValidator<User>>();

var user = new User { Name = "", Email = "invalid", Age = -1 };
var result = validator.Validate(user);

foreach (var error in result.Errors)
{
    Console.WriteLine($"{error.PropertyName}: {error.ErrorMessage}");
}

License

MIT

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

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
1.0.3 35 2/11/2026
1.0.2 34 2/10/2026
1.0.1 33 2/10/2026
1.0.0 34 2/10/2026