Timeline for Where we should put validation for domain model
Current License: CC BY-SA 3.0
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 28, 2018 at 16:36 | comment | added | Geo C. | @Konrad I am widely using value objects in my domain. So nothing reaches the DB (no queries made) unless all the data that comes from the client is valid. My value objects generally make sure the data is valid. Even IDs, I have a value object for them. After the value object is created (id confirmed as valid), then I query the DB to fetchs omething by ID (this is a simple example of course, but I think you get my point). After a few years, I am still using this approach to data validation. Even my libraries tend to do the same thing, keep validation in the value objects or DTOs. | |
| Sep 28, 2018 at 7:21 | comment | added | Konrad | @GeoC. you need upper layer validation it many times saves expensive calls to the db if something is invalid. | |
| Sep 27, 2016 at 9:28 | comment | added | IAbstract | Interesting thoughts and brings to mind several philosophical questions about DDD. | |
| Sep 11, 2015 at 9:43 | vote | accept | adisembiring | ||
| Jun 9, 2014 at 1:03 | comment | added | Geo C. | P.S. I don't mix authorization (is allowed to do something), authentication (did the message came from the right location or was sent by the right client, both being identified by api key/token/username or anything else) with format validation or business rules. When i say 90% i mean those business rules that most of them also include format validation. Ofcourse format validation can be in upper layers, but most of them will be in the domain (even email address format that will be validated in the EmailAddress value object). | |
| Jun 9, 2014 at 0:31 | comment | added | Geo C. | Enforcing the upper layers to know too much about the domain for keeping it in a valid state smells like bad bad design. The domain should be the one that guarantees it's state to be valid. Moving too much on the shoulders of the upper layers can make your domain anemic and you could slip some imporatant constraints that could hurt your business. What i realise now, a proper generalization would be to keep your validation as close to your persistence as possible, or as close to your data manipulation code (when it's manipulated to reach a final state). | |
| Jun 9, 2014 at 0:27 | comment | added | Geo C. | I wonder why so many votes for such an ambiguous answer. When using DDD , sometimes there are some rules that simply check if some data is INT or is in a range. For example when you let your app user to choose some constraints on it's products (how many times can someone preview my product, and in what days interval of a month). Here both constraints should be int and one of them should be in a range of 0-31. This seems data format validation that in a non DDD environment would fit in a service or controller. But in DDD I'm on the side of keeping validaion in the domain (90% of it). | |
| Jun 9, 2014 at 0:20 | comment | added | Geo C. | @psr Do you even need back-end logic if your data is not persisted? You can leave all the manipulation on the client side if your data has no meaning on your business model. Would be also waste of resources to send messages back and forth (client - server) if the data is meaningless. So we get back to the ideea of "never allowing you domain objects to enter in invalid state!" . | |
| Nov 15, 2011 at 18:23 | comment | added | psr | Thank goodness someone said this. Forms that have 90% of the data but won't save anything are unfair to users, who often make up the other 10% just to not lose data, so all the validation has done is force the system to lose track of which 10% was made up. Similar issues can happen on the back end - say a data import. I've found it's usually better to try to work properly with invalid data than to try to prevent it from ever happening. | |
| Nov 15, 2011 at 9:06 | history | answered | Michael Borgwardt | CC BY-SA 3.0 |