Class ServiceCollectionExtensions
Extension methods to add validation to a dependency injection container.
Inheritance
Inherited Members
Namespace: CSF.Validation
Assembly: CSF.Validation.dll
Syntax
public static class ServiceCollectionExtensions
Remarks
Use the methods of this class when configuring the validation framework with your dependency injection container.
Methods
| Improve this Doc View SourceUseMessageProviders(IServiceCollection, Action<IRegistersMessageProviders>)
Configures the validation framework with types to use as failure message providers, when EnableMessageGeneration is true.
Declaration
public static IServiceCollection UseMessageProviders(this IServiceCollection serviceCollection, Action<IRegistersMessageProviders> configAction)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | A service collection |
System.Action<IRegistersMessageProviders> | configAction | An action which indicates which message provider types to use. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
UseValidationFramework(IServiceCollection, Action<ValidationOptions>)
Adds the validation framework to the service collection, such that it may be injected into classes which should consume validation.
Declaration
public static IServiceCollection UseValidationFramework(this IServiceCollection serviceCollection, Action<ValidationOptions> optionsAction = null)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validation framework should be added. |
System.Action<ValidationOptions> | optionsAction | An optional callback which allows setting up default validation options for the validator. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method to configure the validation framework with your own application's dependency injection.
This method makes all of the interfaces in the CSF.Validation.Abstractions
assembly (found in the
corresponding NuGet package of the same name) injectable via DI.
Thus, validators may be defined and consumed from .NET projects which have only a reference to the abstractions package. These projects do not require a dependency upon the full CSF.Validation NuGet package. For larger applications with good project separation, this will mean that the only project(s) which need a reference to the full CSF.Validation NuGet package are startup projects where dependency injection is configured.
If you provide an optionsAction
then you may configure the default options for all validators
created using this dependency injection container.
Validation options may still be provided at the point of performing validation, using either
ValidateAsync(Object, ValidationOptions, CancellationToken) or
ValidateAsync(TValidated, ValidationOptions, CancellationToken).
When both default options are configured here, and options are specified upon the ValidateAsync
method, the
options specified at the point of validation take precedence.
See Also
UseValidationRule(IServiceCollection, Type)
Adds a single validation rule type to the the service collection, so that it may be dependency-injected.
Declaration
public static IServiceCollection UseValidationRule(this IServiceCollection serviceCollection, Type ruleType)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validation rule should be added. |
System.Type | ruleType | The type of validation rule to add to DI. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method only when you wish to add individual rules to dependency injection. It is usually more convenient to use one of the following methods to add rules in bulk, using assembly-scanning techniques.
UseValidationRulesInAssemblies(IServiceCollection, IEnumerable<Assembly>)
Scans the specified assemblies
for validation rules and adds every one of them to
the service collection, so that they may be dependency-injected.
Declaration
public static IServiceCollection UseValidationRulesInAssemblies(this IServiceCollection serviceCollection, IEnumerable<Assembly> assemblies)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validation rules should be added. |
System.Collections.Generic.IEnumerable<System.Reflection.Assembly> | assemblies | A collection of assemblies to scan for validation rule classes. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method, UseValidationRulesInAssembly(IServiceCollection, Assembly) or UseValidationRulesInAssemblies(IServiceCollection, Assembly[]) as a convenient way to add many validation rules to your dependency injection container.
UseValidationRulesInAssemblies(IServiceCollection, Assembly[])
Scans the specified assemblies
for validation rules and adds every one of them to
the service collection, so that they may be dependency-injected.
Declaration
public static IServiceCollection UseValidationRulesInAssemblies(this IServiceCollection serviceCollection, params Assembly[] assemblies)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validation rules should be added. |
System.Reflection.Assembly[] | assemblies | A collection of assemblies to scan for validation rule classes. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method, UseValidationRulesInAssembly(IServiceCollection, Assembly) or UseValidationRulesInAssemblies(IServiceCollection, IEnumerable<Assembly>) as a convenient way to add many validation rules to your dependency injection container.
UseValidationRulesInAssembly(IServiceCollection, Assembly)
Scans the specified assembly
for validation rules and adds every one of them to
the service collection, so that they may be dependency-injected.
Declaration
public static IServiceCollection UseValidationRulesInAssembly(this IServiceCollection serviceCollection, Assembly assembly)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validation rules should be added. |
System.Reflection.Assembly | assembly | An assembly to scan for validation rule classes. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method, UseValidationRulesInAssemblies(IServiceCollection, Assembly[]) or UseValidationRulesInAssemblies(IServiceCollection, IEnumerable<Assembly>) as a convenient way to add many validation rules to your dependency injection container.
UseValidatorBuilder(IServiceCollection, Type)
Adds a single validator builder type to the the service collection, so that it may be dependency-injected.
Declaration
public static IServiceCollection UseValidatorBuilder(this IServiceCollection serviceCollection, Type builderType)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validator builder should be added. |
System.Type | builderType | The type of validation builder to add to DI. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method only when you wish to add individual builders to dependency injection. It is usually more convenient to use one of the following methods to add builders in bulk, using assembly-scanning techniques.
UseValidatorBuildersInAssemblies(IServiceCollection, IEnumerable<Assembly>)
Scans the specified assemblies
for validator builders and adds every one of them to
the service collection, so that they may be dependency-injected.
Declaration
public static IServiceCollection UseValidatorBuildersInAssemblies(this IServiceCollection serviceCollection, IEnumerable<Assembly> assemblies)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validator builders should be added. |
System.Collections.Generic.IEnumerable<System.Reflection.Assembly> | assemblies | A collection of assemblies to scan for validator builder classes. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method, UseValidatorBuildersInAssembly(IServiceCollection, Assembly) or UseValidatorBuildersInAssemblies(IServiceCollection, Assembly[]) as a convenient way to add many validator builders to your dependency injection container.
UseValidatorBuildersInAssemblies(IServiceCollection, Assembly[])
Scans the specified assemblies
for validator builders and adds every one of them to
the service collection, so that they may be dependency-injected.
Declaration
public static IServiceCollection UseValidatorBuildersInAssemblies(this IServiceCollection serviceCollection, params Assembly[] assemblies)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validator builders should be added. |
System.Reflection.Assembly[] | assemblies | A collection of assemblies to scan for validator builder classes. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method, UseValidatorBuildersInAssembly(IServiceCollection, Assembly) or UseValidatorBuildersInAssemblies(IServiceCollection, IEnumerable<Assembly>) as a convenient way to add many validator builders to your dependency injection container.
UseValidatorBuildersInAssembly(IServiceCollection, Assembly)
Scans the specified assembly
for validator builders and adds every one of them to
the service collection, so that they may be dependency-injected.
Declaration
public static IServiceCollection UseValidatorBuildersInAssembly(this IServiceCollection serviceCollection, Assembly assembly)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | serviceCollection | The service collection to which the validator builders should be added. |
System.Reflection.Assembly | assembly | An assembly to scan for validator builder classes. |
Returns
Type | Description |
---|---|
Microsoft.Extensions.DependencyInjection.IServiceCollection | The service collection, so that calls may be chained. |
Remarks
Use this method, UseValidatorBuildersInAssemblies(IServiceCollection, Assembly[]) or UseValidatorBuildersInAssemblies(IServiceCollection, IEnumerable<Assembly>) as a convenient way to add many validator builders to your dependency injection container.