Reputation: 3514
The Whole Values (1) that quantify a domain model have been checked to ensure that they are recognizable values, may have been further edited for suitability by the domain model and have been Echoed Back (4) to the user. All of these checks are immediate on entry. There is, however, a class of checking that should be deferred until the last possible moment.
In The CHECKS Pattern Language of Information Integrity Ward Cunningham addresses Deferred Validations (6) for whole objects. But this is still not fully clear to me :(
I understand deferred validation is a very detailed validation for a complex object. So, should I use this validation in a test method or inside the domain property while following DDD? Is this can be implemented for UI?
And also when should I avoid this? What are the cons of Deferred validation? Can anyone please explain this with an example? Thanks in advance
Upvotes: 4
Views: 1346
Reputation: 43728
There are various opinions on this and validation is a pretty large subject, but usually you never want to allow a domain object to be in an invalid state. Therefore, validation occurs at object construction and exceptions are thrown immediately.
E.g. A Person
object cannot exist without a name in most domains.
However, it's not always possible to validate an object invariants at construction time. This is the case when an object must be allowed to exist in an incomplete/transient state.
E.g.
You are building an application which allows users to post an ad. All the fields are required before posting the ad, but there are a lot of details to fill and you want to give the user the option to save their unfinished work and continue later.
In the exemple above, it is not possible to validate the Ad
entity at construction time since you must allow incomplete ads to be saved.
In this case, the ad posting's validation would occur only when it's about to be posted.
Keep in mind that there would be many other ways to solve the above issue in your domain. For exemple, one could not want to allow the Ad
entity to be in an invalid state and could introduce a persistent AdBuilder
object which serves the purpose of representing the stateful's ad creation process.
Also, someone could also decide that saving incomplete work is not a domain concern and that incomplete information should be stored on the client (e.g. localStorage
in a web browser) until it is ready to be posted.
Upvotes: 5