1
This article shows you how to create a FluentValidator factory that uses the Castle Windsor IoC container to resolve IValidator<> instances

One of the nicest things about Jeremy Skinner's FluentValidator library is that it makes it really simple to inject validators into your MVC applications instead of being bound via the attributes style validation of the data annotations model.

The way this works is by implementing an IValidatorFactory which can then be passed to the FluentValidationModelValidatorProvider which the ASP.NET MVC framework uses to apply it's validation.

You can use any of the popular .net based IoC containers but this article will focus specifically on creating an IValidatorFactory for the Castle Windsor IoC container.

Creating the FluentValidator Factory

Using Jeremy's excellent blog post as a starting point, it's clear that creating a FluentValidator Factory is as simple as inheriting from the ValidatorFactoryBase class and overriding the CreateInstance method.

public class WindsorFluentValidatorFactory : ValidatorFactoryBase
{
    private readonly IKernel _kernel;

    public WindsorFluentValidatorFactory(IKernel kernel)
    {
        _kernel = kernel;
    }

    public override IValidator CreateInstance(Type validatorType)
    {
      return _kernel.HasComponent(validatorType)
                   ? _kernel.Resolve<IValidator>(validatorType)
                   : null;
    }
}

Installing the Validators Once you have set up your factory, you need to create an installer to register your validators with Castle Windsor.

public class ValidatorInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            AllTypes
                .FromThisAssembly()
                .BasedOn(typeof(IValidator<>))
                .WithService
                .Base());
    }
}

The most important thing here is to use the WithService.Base() method to register the validators as it registers the closed version of the interface which matches the type passed into the validation factory.

Bringing it all together

Once you have your factory and your installer, you need to set them up for use within the ASP.NET MVC framework. Again, this is a fairly straight forward process.

Within the Application_Start method of the Global.asax.cs class, simply create an instance of the container as you normally would, and then add then create a new instance of the FluentValidationModelValidatorProvider which takes in the newly created WindsorFluentValidatorFactory in it's constructor. The FluentValidationModelValidatorProvider then gets added to ASP.NET MVC's ModelValidatorProviders collection and that's all there is to it.

protected void Application_Start()
{
    //create the windsor container
    var container = new WindsorContainer().Install(FromAssembly.This());

    //setup fluent validation
    var fluentValidationModelValidatorProvider =
            new FluentValidationModelValidatorProvider(new WindsorFluentValidatorFactory(container.Kernel));

    //add fluent validator to the ModelValidatorProviders collection
    ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider);
}

Now the only thing left to do is write your fluent validators.