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
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.
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.
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?”."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With