I hope this isn’t terribly subjective, but when it comes to validating business logic, I see two paths that, unless I’m mistaken, deliver pretty much the same result:
-
Use a service layer where all validation on your models is performed (ref: http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/validating-with-a-service-layer-cs)
-
Decorate your models with Data Annotations
In the first instance, the model is a dumb container for data, and in the second, the model knows about its valid state. Above and beyond that, is there nuance between the two that I’m missing? Should one be used over the other in some instances?
Thanks,
Chris
In my opinion, you could keep the basic validation (required fields, regex fields, comparative fields etc…) on your Model (InputModel) with Data Annotations or Fluent Validation, and your bussiness validation in service layer. I think the Annotations is much more to create a screen validation and inputs to server side than business validation. If you keep the business at the service layer you must remember to create a
ModelStateWrapper to integrate it with Asp.Net MVC, and show it on the view.Take a look at a samepl of ModelState Wrapper: