HandlerR.FluentValidation 1.0.0

dotnet add package HandlerR.FluentValidation --version 1.0.0
                    
NuGet\Install-Package HandlerR.FluentValidation -Version 1.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="HandlerR.FluentValidation" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HandlerR.FluentValidation" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="HandlerR.FluentValidation" />
                    
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 HandlerR.FluentValidation --version 1.0.0
                    
#r "nuget: HandlerR.FluentValidation, 1.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 HandlerR.FluentValidation@1.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=HandlerR.FluentValidation&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=HandlerR.FluentValidation&version=1.0.0
                    
Install as a Cake Tool

HandlerR.FluentValidation

为 .NET 10 应用接入 FluentValidation。HandlerR 在调用请求处理器前运行已注册的 IValidator<TRequest>。校验失败时抛出 RequestValidationException,其 Errors 包含字段名、错误消息和错误码。

安装

以下命令创建独立的 .NET 10 控制台示例。已有 DI 容器的宿主可直接从安装适配包开始。

dotnet new console -n HandlerR.ValidationExample -f net10.0
cd HandlerR.ValidationExample
dotnet add package HandlerR.FluentValidation --version 1.0.0
dotnet add package Microsoft.Extensions.DependencyInjection --version 10.0.15

安装适配包时会一并还原 HandlerR 和 FluentValidation。

注册

将以下内容保存为 Program.cs。它在宿主注册校验器,并在 AddHandlerR 的配置回调中启用适配器。

using FluentValidation;
using HandlerR.Abstractions;
using HandlerR.Contracts;
using HandlerR.DependencyInjection;
using HandlerR.FluentValidation;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddScoped<IValidator<CreateOrder>, CreateOrderValidator>();
services.AddHandlerR(configure =>
{
    configure.RegisterFromAssemblyContaining<CreateOrderHandler>();
    configure.AddFluentValidation();
});

await using var provider = services.BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true });
await using var scope = provider.CreateAsyncScope();
var sender = scope.ServiceProvider.GetRequiredService<ISender>();
Console.WriteLine(await sender.SendAsync(new CreateOrder(2)));

try
{
    await sender.SendAsync(new CreateOrder(0));
}
catch (RequestValidationException exception)
{
    foreach (var error in exception.Errors)
        Console.WriteLine($"{error.Identifier}: {error.ErrorCode}");
}

public sealed record CreateOrder(int Quantity) : IRequest<int>;

public sealed class CreateOrderHandler : IRequestHandler<CreateOrder, int>
{
    public ValueTask<int> HandleAsync(CreateOrder request, CancellationToken cancellationToken)
        => ValueTask.FromResult(request.Quantity);
}

public sealed class CreateOrderValidator : AbstractValidator<CreateOrder>
{
    public CreateOrderValidator()
        => RuleFor(order => order.Quantity).GreaterThan(0).WithErrorCode("QUANTITY_INVALID");
}

运行 dotnet run。第一行输出 2。第二次调用会在进入处理器前触发校验,输出 Quantity: QUANTITY_INVALID。

AddFluentValidation() 会注册校验行为和适配器。此接入方式适用于 JIT 宿主。 主包注册与 Native AOT 边界

Product Compatible and additional computed target framework versions.
.NET 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
1.0.0 0 11/24/2026