Skip to content

AdaskoTheBeAsT/AdaskoTheBeAsT.FluentValidation.SimpleInjector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AdaskoTheBeAsT.FluentValidation.SimpleInjector

FluentValidation extensions to SimpleInjector

Badges

CodeFactor Build Status Azure DevOps tests Azure DevOps coverage Quality Gate Status Sonar Tests Sonar Test Count Sonar Test Execution Time Sonar Coverage Nuget

Usage

This library scans assemblies and adds implementations of IValidator<> to the SimpleInjector container.

There are few options to use with Container instance:

  1. Marker type from assembly which will be scanned

     container.AddFluentValidation(typeof(MyValidator), type2 /*, ...*/);
  2. 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);
        }
    }
  3. Special configuration action with different lifetime for validators

     container.AddFluentValidation(
         cfg =>
         {
             cfg.WithAssembliesToScan(assemblies);
             cfg.AsScoped();
         });

About

FluentValidation extensions to SimpleInjector

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages