Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation in domain model?

I've designed a domain model following DDD methodology. I would like to add validation.

Do you think adding validation at the domain model level is a good idea? If no, where should I validate my domain objects?

Thanks

like image 548
manash Avatar asked Apr 03 '12 12:04

manash


3 Answers

If the validation rules form part of the business domain, they belong in the model.

An aggregate root is responsible of maintaining the invariants encompassed by it, so validating them falls under its responsibility.

If you find that the validation rules are very complex, you can create a validation service that will be used by the aggregate root for this function.

like image 179
Oded Avatar answered Oct 01 '22 00:10

Oded


It depends on the kind of validation you have.

If it's just that a Customer name is required and that there is a maximum length for a field, then it's not the responsibility of the domain itself. This should be input validation.

If you are creating a webshop and the Order is invalid when not all items are in stock, then you have some real domain validation.

Domain Driven Design 101 has some nice examples starting at slide 44.

like image 27
Wouter de Kort Avatar answered Oct 01 '22 00:10

Wouter de Kort


Seealso: http://lostechies.com/jimmybogard/2009/02/15/validation-in-a-ddd-world.

"Instead of answering the question, “is this object valid”, try and answer the question, “Can this operation be performed?”."

like image 38
Ben Biddington Avatar answered Oct 01 '22 01:10

Ben Biddington