DTOs are widely used and they 'solve' real problems, do they?
Problems
Anemic Object
Inconsistent Data
Duplicated logic
Duplicated structure
Class Polluting
Information Hiding Violation
Code repeated among mutators, accessors, serializers, parsers
Ripple Effect
Data integrity
Solutions
Transfer anemic data on arrays.
Use real business objects.
If we want to transfer partial objects: use proxies or null objects to break the reference graph.
Sample Code
Wrong
Right
Detection
We can use the same anemic object detectors.
We can check for anemic classes with no business object behavior (removing serializes, constructors, mutators etc).
# Tags
- Anemic
Conclusion
DTOs are a tool and an established practice in some languages. We should use them with care and responsibility.
If we need to disassemble our objects in order to send them away from our realms, we need to be extremely cautioned. Since dismembered objects have no integrity considerations.
His author warns us about its actual abuse.
Relations
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 01 - Anemic Models
Maxi Contieri ・ Oct 20 '20
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 20 - Premature Optimization
Maxi Contieri ・ Nov 8 '20
More info
The best smells are something that's easy to spot and most of time lead you to really interesting problems. Data classes (classes with all data and no behavior) are good examples of this. You look at them and ask yourself what behavior should be in this class.
Martin Fowler
Top comments (0)