Created
March 3, 2015 14:43
-
-
Save khalidabuhakmeh/376337ec06701b314f15 to your computer and use it in GitHub Desktop.
FluentValidation working with WebAPI
This file contains hidden or 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
| FluentValidationModelValidatorProvider.Configure(GlobalConfiguration.Configuration, config => | |
| { | |
| config.ValidatorFactory = new WebApiValidatorFactory(GlobalConfiguration.Configuration); | |
| }); | |
| public class WebApiValidatorFactory : ValidatorFactoryBase | |
| { | |
| private readonly HttpConfiguration _configuration; | |
| public WebApiValidatorFactory(HttpConfiguration configuration) | |
| { | |
| _configuration = configuration; | |
| } | |
| public override IValidator CreateInstance(Type validatorType) | |
| { | |
| return _configuration.DependencyResolver.GetService(validatorType) as IValidator; | |
| } | |
| } | |
| public class ValidationRegistry : Registry | |
| { | |
| public ValidationRegistry() | |
| { | |
| AssemblyScanner.FindValidatorsInAssemblyContaining<ValidationRegistry>() | |
| .ForEach(result => | |
| { | |
| For(result.InterfaceType) | |
| .Use(result.ValidatorType); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment