cover-img

Code Smell 40 - DTOs

DTOs are widely used and the 'solve' real problems, do they?

2 January, 2023

2

2

0

DTOs are widely used and the 'solve' real problems, do they?

Problems

Anemic Object

Inconsistent Data

Duplicated logic

Class Polluting

Information Hiding

Code repeated among mutators, accessors, serializers, parsers

Ripple Effect

Data integrity

Solutions

1.

Transfer anemic data on arrays

2.

Use real business objects

3.

If we want to transfer partial objects: use proxies or null objects to break the reference graph.

Refactorings

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

More Info

Credits

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
This article is part of the CodeSmell Series.

2

2

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.