Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This might be too opinionated a question, but looking for help!

I have been trying to refine my ASP.NET MVC program structure. I just started using it at preview 5 and it is my first foray into business application development -- so everyting is new!

At the controller level, I have a service object responsible for talking to the repository and taking care of all business logic. At the action level, I have an object that holds all view data -- user input and generated output -- that I'll call view object (is there a general term for this?). Unlike most examples I see, this object is not a database object, but an object specific to the view.

So now I want to add user validation. The problem is, I'm not sure where to put it. It makes the most sense to me to do it in the Service layer. The Service layer is responsible for all the business logic and validation is business logic. On the other hand, most validation frameworks that I see are for validating an object, which makes me think the view object should be validation aware. Finally, there are some validation methods that would require a database connection (checking if a user input field has a corresponding database record for example), and the view object has no concept of a database, only the Service.

So some options I see are:

  • Do validation in the Service.Method on the parameters passed to the method.
  • Do validation in the view object before calling Service.Method.
  • Do validation in the view object but make the Service.Method require a view object reference so it is initiating the validation on the object.

I'm sure there are more. I'm curious of how other people handle user input validation in the MVC sense. I've previously used the Enterprise Validation Block and liked being able to use stock validators for everything, but I'm not sure how to make it fit into a seperate view object and service layer. It would be easy if they (view object / service) were the same object, which is maybe what people do? Like I said, its all new to me and I'm looking for best practice / patterns.

like image 955
anonymous Avatar asked Dec 10 '22 22:12

anonymous


2 Answers

I typically do basic validation (required fields, email format, etc.) in the controller action when the form is submitted. I then let the business layer handle validation that requires business knowledge. I usually double check the basic stuff in the business layer as well, so if I expose that logic through web services or use it in another application later, I still have validation where it is most important (IMO).

like image 109
Andrew Van Slaars Avatar answered Jan 23 '23 05:01

Andrew Van Slaars


There are some interesting links on this

  • MVC & MS Validation Application Block
  • MVC & Data Annotation Validators
  • Form Validation video

Personally, i've added validation to my Service layer objects (first two links). This way, if any method in any controller decides to call my service method .. the logic is all checked and boxed in one location. DRY.

That said, i also have some UI validation (3rd link) .. to reduce the roundtrip time.

Lastly, the current MVC dll's have the ability to pass error messages back to the UI .. so the view can display them nicely. Check out :-

  • ViewData.ModelState.AddModelError(key, message)

hth!

like image 23
Pure.Krome Avatar answered Jan 23 '23 04:01

Pure.Krome