Where to put data management rules for complex data validation in ASP.NET MVC?
Asked Answered
G

3

3

I am currently working on an ASP.NET MVC2 project. This is the first time I am working on a real MVC web application. The ASP.NET MVC website really helped me to get started really fast, but I still have some obscure knowledge concerning datamodel validation.

My problem is that I do not really know where to manage my filled datamodel when it comes to complex validation rules. For example, validating a string field with a Regex is quite easy and I know that I just have to decorate my field with a specific attribute, so data management rules are implemented in the model. But if I have multiple fields that I need to validate which each other, for example multiple datetime that need to be correctly set following a specific time rule, where do I need to validate them? I know that I could create my own validation attributes, but sometimes validation ask a specific validation path which is to complex to be validated using attributes.

This first question also leads me to a related question which is, is it right to validate a model in the controller? Because for the moment that is the only way I found for complex validation. But I find this a bit dirty and I feel it does not really fit a the controller role and much harder to test (multiple code path).

Thanks.

NB: I got some pretty good solutions here but I am waiting for other ideas and some "best practice" solution".

Gusti answered 21/5, 2010 at 12:1 Comment(1)
I'm asking myself the same question. Trying to be as "DRY" as possible, what should I do with simple validations (required, format, etc.) ? Should I use DataAnnotation to enforce it in the view and redo the same validations (plus the more complex ones) in my business layer ?Wearing
B
4

Mega Dupe. Mega Subjective. The "where and how to validate with MVC" argument has been beaten to death without coming up with a straight answer. This is so subjective and philosophical to each developer/shop that its almost impossible for everybody to agree on anything.

The other issue is even the validation tooling comes in a bunch of shapes and sizes and can function in different scopes and layers. Its almost insane the variety in validation tooling. How did if( someString != "" ) get so hard? ;)

If you read these other answers you'll quickly find there is no best practice at all. Once you get into Domain Driven Design principals and the discussion on invalid state and objects you'll find the discussion gets even more complicated.

Where do you do your validation? model, controller or view

ASP.NET MVC 2 validation using DTOs instead of domain entities

Does ASP.Net MVC 2 validation need some more thought in terms of patterns and use?

Mapping Validation Attributes From Domain Entity to DTO

Which validation library for ASP.NET MVC?

ASP.NET MVC - User Input and Service/Repository - Where to do Validation?

ASP.NET MVC: Is Data Annotation Validation Enough?

MVC - where to implement form validation (server-side)?

Asp.Net MVC Validation

DDD:

Validation in a Domain Driven Design

Berliner answered 21/5, 2010 at 17:46 Comment(3)
@R0MANARMY I get that a lot; how I always sound angry? I'm really not. :(Berliner
I was just joking, don't take it too personallyAvast
Thanks for all these links. So I bet it is all about practicing. As a beginner developer I got used to search for best practices when starting in a new technology.Gusti
S
2

My person opinion would be to keep the view as clean as possible and try and force the view only to display data (keeping the view as dumb as possible).

Sure, you can do some simple validation in the view such as Required, Regex rules and so on.

Complex business rules should sit inside a business entity or some business logic layer.

What I do in my MVC projects is have the model call a method such as Validate() which will check the final level of validation such as business rules and so on and then I can call Save();

Supportable answered 21/5, 2010 at 12:12 Comment(1)
I believe I could do that pretty easily extending my partial LinqToSql models partial classes. Thanks.Gusti
L
1

Once you have a populated class that is ready to be validated simply pass it to a validation class in the controller.

Lalita answered 21/5, 2010 at 12:18 Comment(1)
I pretty love this idea. It remembers me the design used in the default Account controller.Gusti

© 2022 - 2024 — McMap. All rights reserved.