Created
August 9, 2022 09:25
-
-
Save Cheranga/3d86116a93ab047eeffb83e5545aff24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class FluentValidationExtensions | |
{ | |
public static void RegisterValidatorsInAssembly(this IMvcBuilder builder, Assembly assembly) | |
{ | |
builder.RegisterValidatorsInAssemblyList(assembly); | |
} | |
public static void RegisterValidatorsInAssemblyList(this IMvcBuilder builder, params Assembly[] assemblies) | |
{ | |
var assemblyList = assemblies?.ToList() ?? new List<Assembly>(); | |
if (!assemblyList.Any()) | |
{ | |
return; | |
} | |
// register the validators from the respective assemblies | |
assemblyList.ForEach(x=>builder.Services.AddValidatorsFromAssembly(x)); | |
// register the custom validator factory, to get `IValidator` instances to validate requests | |
builder.Services.AddSingleton<ICustomValidatorFactory, CustomValidatorFactory>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment