Data Annotation vs Fluent API vs Fluent Validation

Dec 27, 2012 at 4:01 AM

I need to clarify about Data Annotation,Fluent API and Fluent Validation.

These are my questions:

  1. When I work with EF code first which technologies should I use as best practices and when ?

  2. Fluent API and Fluent Validation are same or for different purposes ?

  3. When I am using 

 

modelBuilder.Entity() .Property(s => s.Name) .IsRequired();

 with Fluent API is that reflect to my MVC view's supplier Name validation (like Data Annotation [Required] ) ?

 

4. If I am using ViewModel for my views then How can I use Fluent Validation for do ViewModel validation ? 

Please clarify above doubts which I am having .

Coordinator
Dec 27, 2012 at 9:17 AM
DataAnnotation and FluentValidation are both validation libraries that serve a similar purpose. They are used to ensure that the values that have been assigned to the properties on your object meet a set of rules. They are best used at the view-model level. As they serve the same purpose, it's really a matter of personal preference - use what you like best.

The EF fluent api is different. This is used to define the mapping between your object and the database. Calling IsRequired on modelbuilder is not the same as a required attribute. The model builder defines the mapping to the database. For example, If you use EF's ability to generate the database from the model then the corresponding column will be generated as non-nullable.

4. If I am using ViewModel for my views then How can I use Fluent Validation for do ViewModel validation ?

It's essentially the same as if you're using DataAnnotations. Check out the documentation page on how to configure fluentvalidation's MVC integration. Once configured, model instances passed to controller actions are automatically validated.

Jeremy
Dec 27, 2012 at 10:30 AM

Hi Jeremy,

First of all I must Thanks for your grate explanation.Lot of issues Which I had now cleared.

One small question:

Related to the My Q 3. That is when I set fluent API as above is that enough for validate my View's relevant property also (here it is supplier Name ) ?.

(I mean is there any requirement to use any model validation framework (either DataAnnotation or Fluent Validation) for do validate view's supplier Name text box)

 

Coordinator
Dec 27, 2012 at 11:14 AM
You still need to use something like the Required attribute if you want validation in your view. As I mentioned before, the modelbuilder is only used for defining the database field mappings - it does not control validation in your application.
Dec 27, 2012 at 11:19 AM

Hi Jeremy,

Thanks a lot.

It's very clear Now.

Feb 15, 2013 at 6:48 PM
I think a great idea would be to integrate something like a plugin to define the same rules configured in EF in my ViewModel
See: http://fluentvalidation.codeplex.com/discussions/433299


JeremyS wrote:
You still need to use something like the Required attribute if you want validation in your view. As I mentioned before, the modelbuilder is only used for defining the database field mappings - it does not control validation in your application.