Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use fluentvalidation instead of ASP.NET MVC validation

In which circumstances would you choose FluentValidation (FV) over the ASP.NET MVC 3 way?

What are the advantages of FV over MVC? I realise that with the latter we have to write much more code and can litter the code with Data Annotations. Moreover, it would seem to be easier to write custom validation using FV than MVC. However, with MVC it is possible to make use the data annotation and plug jQuery validation in.

So what in your view would make you choose one over the other? Are there circumstances where you would even use both?

like image 612
DavidS Avatar asked Jun 07 '11 14:06

DavidS


People also ask

Should I use FluentValidation?

Summary. FluentValidation provides a great alternative to Data Annotations in order to validate models. It gives better control of validation rules and makes validation rules easy to read, easy to test, and enable great separation of concerns.

What is FluentValidation MVC?

Fluent validation is one way of setting up dedicated validator objects that you can use when you want to treat validation logic as separate from the business logic. The Aspect-Oriented Programming (AOP) paradigm enables separation of cross-cutting concerns within a system and validation is one such concern.

What is FluentValidation .NET core?

Fluent Validation is a free to use . NET validation library that helps you make your validations clean, easy to create, and maintain. It even works on external models that you don't have access to, with ease. With this library, you can separate the model classes from the validation logic like it is supposed to be.

What is FluentValidation C#?

FluentValidation is a .NET library for building strongly-typed validation rules. It Uses a fluent interface and lambda expressions for building validation rules. It helps clean up your domain code and make it more cohesive, as well as giving you a single place to look for validation logic.


1 Answers

Fluent validation is one way of setting up dedicated validator objects, which you would use when you want to treat validation logic as separate from business logic. The aspect-oriented programming (AOP) paradigm enables separation of cross-cutting concerns within a system, and validation is one such concern. Separating validation helps clean up your domain code and make it more cohesive, as well as giving you a single place to look for validation logic.

MVC annotation-driven validation is a very 'cheap' way to get some basic validation into an application, without going to the hassle of creating dedicated validator objects, creating a validation system which organizes them and plugging it all together. It's very easy to set up, but can make your domain objects less clean.

For small systems where all the validation logic can be handled using annotations, I would recommend just using annotations, because they're so easy to set up. For larger, more complex systems, I would recommend separating the validation concern using validator objects.

I personally like to use both approaches: add validation attributes to ViewModel classes (which means the annotations don't clutter up my domain objects), as well as having dedicated validator objects within my domain layer. This is a small amount of duplication, but using annotations is so quick and easy, I find it worth the extra maintenance cost.

like image 93
Steve Wilkes Avatar answered Oct 19 '22 05:10

Steve Wilkes