Java – DDD and Validation of Aggregate Root

designdomain-driven-designjavavalidation

Suppose an aggregate root : MailConfiguration (wrapping an AddressPart object). The AddressPart object is a simple immutable value object with some fields like senderAdress, recipentAddress (to make example simple).

As being an invariant object, AddressPart should logically wrap its own Validator (by the way of external a kind of AddressValidator for respecting Single Responsibility Principle)

I read some articles that claimed an aggregateRoot must validate its 'children'.
However, if we follow this principle, one could create an AddressPart with an uncohesive/invalid state.

What are your opinion? Should I move the collaborator AddressValidator(used in constructor so in order to validate immediately the cohesion of an AddressPart) from AddressPart and assign it to aggregateRoot (MailConfiguration) ?

Best Answer

Will AddressPart ever be used elsewhere outside of your MailConfiguration aggregate? If it is (or is likely to be) then the validation should absolutely stay with it.

Actually, I'd argue that the validation should stay wit it regardless. Even if the parent was 'responsible', it should simply delegate that call down to the children it has.