3
votes

I am developing my first DDD application and trying to follow some basic rules I've studied in these last few months.

I've implemented the repository pattern with Nhibernate.
I thought I could have "moved" my entities from the controller to the view, but soon I've realized it's almost impossible.

Most people seem to prefer to define a viewmodel specific for each view.
I don't particularly fancy the idea to redefine the fields I've already create for my entities but it seems that this is the only way.

Now I am facing the situation where I want to attach some validation rules.
I thought I could have attached the validation rules (with DataAnnotations) to the entities but it can't work if I am using a viewmodel.

Here are the questions:

  • Shouldn't the validation be part of the domain model?
  • Isn't it time consuming to create the model and then spend time to remap the same fields (properties) on the viewmodel?
  • Isn't this an anemic model, if it doesn't have, at least, validation rules? I am starting to wonder if DDD is really suitable for small/medium size application.

I appreciate any help/suggestion.

2

2 Answers

7
votes

This has been asked hundreds of times here and I have answered it hundreds of times (so this makes you the hundredth and first person to ask this :-)): put user validation logic on your view models (things like required fields, datetime formats, ...) and put business validation logic on your entities (things like the username has already been taken, the user can no longer purchase products on your site because he has reached the maximum quota, ...).

3
votes

Shouldn't the validation be part of the domain model?

I think it should be on both the domain model and the viewmodels. The validation on the viewmodels will check for valid input as to type --datetime, decimal,int, etc whereas the validation on the domain model should check for rules specific to the application. In this way even if you decide to use another UI the business validation will still be in place, while the UI will need to take care of the input validation.

Isn't it time consuming to create the model and then spend time to remap the same fields (properties) on the viewmodel?

There are tools that can help you with that, for example, automapper on codeplex. In my opinion this results in cleaner separation between BLL and UI.

Whereas this approach is in overall more time consuming, it is also more scalable. If your application will need to grow in the future than this is a reasonable way to design the architecture.