In MVC, I can create a Model Validator which can take Dependencies. I normally use FluentValidation for this. This allows me to, for example, check on account registration tha
I have DI working with Fluent Validators in WebApi no problems. I've found that the validators get called a lot, and these sort of heavy logic validations have no place in a model validator. Model validators, in my opinion, are meant to be lightweight checking the shape of the data. Does Email look like an email and has the caller provided FirstName, LastName and either Mobile or HomePhone?
Logic validation like Can this email be registered belongs in the service layer, not at a controller. My implementations also don't share an implicit data context since I think that's an anti-pattern.
I think the current NuGet package for this has an MVC3 dependency, so I ended up just looking at the source directly and creating my own NinjectFluentValidatorFactory.
In App_Start/NinjectWebCommon.cs we have the following.
///
/// Set up Fluent Validation for WebApi.
///
private static void FluentValidationSetup(IKernel kernel)
{
var ninjectValidatorFactory
= new NinjectFluentValidatorFactory(kernel);
// Configure MVC
FluentValidation.Mvc.FluentValidationModelValidatorProvider.Configure(
provider => provider.ValidatorFactory = ninjectValidatorFactory);
// Configure WebApi
FluentValidation.WebApi.FluentValidationModelValidatorProvider.Configure(
System.Web.Http.GlobalConfiguration.Configuration,
provider => provider.ValidatorFactory = ninjectValidatorFactory);
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
}
I believe the only other required packages for the above are: