Skip to content

Instantly share code, notes, and snippets.

@Cheranga
Created August 9, 2022 09:25
Show Gist options
  • Save Cheranga/3d86116a93ab047eeffb83e5545aff24 to your computer and use it in GitHub Desktop.
Save Cheranga/3d86116a93ab047eeffb83e5545aff24 to your computer and use it in GitHub Desktop.
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