FluentValidation extensions to SimpleInjector
This library scans assemblies and adds implementations of IValidator<>
to the SimpleInjector container.
There are few options to use with Container
instance:
-
Marker type from assembly which will be scanned
container.AddFluentValidation(typeof(MyValidator), type2 /*, ...*/);
-
List of assemblies which will be scanned.
Below is sample for scanning assemblies from some solution.
[ExcludeFromCodeCoverage] public static class FluentValidationConfigurator { private const string NamespacePrefix = "YourNamespace"; public static void Configure(Container container) { var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var assemblies = new List<Assembly>(); var mainAssembly = typeof(FluentValidationConfigurator).Assembly; var refAssemblies = mainAssembly.GetReferencedAssemblies(); foreach (var assemblyName in refAssemblies .Where(a => a.FullName.StartsWith(NamespacePrefix, StringComparison.OrdinalIgnoreCase))) { var assembly = loadedAssemblies.Find(l => l.FullName == assemblyName.FullName) ?? AppDomain.CurrentDomain.Load(assemblyName); assemblies.Add(assembly); } container.AddFluentValidation(assemblies); } }
-
Special configuration action with different lifetime for validators
container.AddFluentValidation( cfg => { cfg.WithAssembliesToScan(assemblies); cfg.AsScoped(); });