asp.net-mvcvalidationdomain-driven-designviewmodel

Where and how to validate and map ViewModel?


I am trying to learn Domain Driven Design and recently read that lots of people advocate creating a ViewModels for your views that store all the values you want to display in a given view.

My question is how should I do the form validation? should I create separate validation classes for each view, or group them together? I'm also confused on what this would look like in code.

This is how I currently think validation and viewmodels fit in to the scheme of things:

View (some user input) -> Controller -> FormValidation(of ViewModel) -> (If valid map to ViewModel to Domain Model) -> Domain Layer Service -> Infrastructure

Thanks!

P.S. I use Asp.net MVC with C#


Solution

  • I have been experimenting with placing my form validation in the ViewModel, and for complex business validation I use a service layer.

    This has actually been working out real well and the code is so much easier to read and maintain