20 July 2012

MVC Data Annotation Validators - The Solution

This "Problem - Solution" is a two series article . So this is the second part: The Solution.
The first part explains the problems facing the implementation of the MVC Data Annotation Validators in a Service Oriented Architecture (SOA) with clear separation of the layers. You can read it here

The solution I found is to use Fluent Validation framework with an Inversion of Control container (IoC) to instantiate my validators.

The Fluent Validation framework is a validation engine that can be used in several scenarios. In this specific solution, I will be focusing in the integration with MVC without using any attributes.
To use Fluent Validation with ASP.Net MVC I am going to use an Inversion of Control container to instantiate the validators.

The difference between an Inversion of Control (IoC) and any other kind of frameworks is that the control gets inverted. The objects in a application are controlled by the Inversion of Control Containers and the application is completely unaware of what the IoC does. A IoC container manages it's life-cycle, invoks methods and is fully autonomous form the application.

The solution implemented by Fluent Validation is to use a custom Validator Factory.
The process to implement can be described in the following steps:
1) Create a Validator Factory for FluentValidation that inherits from ValidatorFactoryBase. Override the CreateInstance method to call the IoC container that is responsible for instantiating the validators.

3) Register your validator types with StructureMap, using the FluentValidation AssemblyScanner. The AssemblyScanner automatically registers all of the validator classes in a particular assembly with a IoC container.

5) Now the real fun can begin. The FluentValidation library can be used in MVC.
Let's create a customer validator class, where the First Name and Last Name are mandatory and we also want to set a custom error message.
Also perform the same validation on the email and guarantee that the email follows the basic rules.
The validation class must inherit from AbstractValidator.
The rules are defined using lambda expressions.

The FluentValidation has Built in Validators or you can define your custom validators.
You can read more about the fluent validation here
Structuremap download and documentation can be found hereCodeProject

Other Blogs

Disclaimer

1) The owner of this blog is not to be held responsible for any damage, loss or problem, including any interference or damage to a visitor’s computer, software or data, in relation to the use of this blog or it's content.
2) All the blog content is provided "as is" without warranty of any kind. Use it at your own risk. The owner of the blog is not to be held responsible for any loss or damage suffered by you or any third party .
3) All comments expressed by visitors are not endorsed by the owner of this blog.
4) The owner of this blog reserves the right to filter all published content, including comments.
5) All links provided at this blog are provided for the convenience of visitors. The owner of this blog is not to be held responsible for the content or use of these links.
6) The terms and conditions of this blog are subjected to change without prior notice.