Timeline for What is the use of DTO instead of Entity?
Current License: CC BY-SA 4.0
18 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 31, 2021 at 14:48 | comment | added | Flater | @pinkpanther: Clean coding is a spectrum. On the clean end of that spectrum, every layer should have their own DTOs. However, in reality, that is usually considered overkill, and codebases tend to specify one level of DTO, usually on the business level. Note, however, that e.g. wrapping the business DTO in a viewmodel of sorts is a separate consideration here; a viewmodel does not supplant the need for a business DTO. | |
| Aug 31, 2021 at 14:43 | comment | added | pinkpanther | Is it a good idea to return dto in service or do the conversion in controller. If we return that in service layer(in 3 layered architecture), will we not have problem when we need to version APIs(REST)? | |
| Jun 16, 2020 at 10:01 | history | edited | CommunityBot |
Commonmark migration
|
|
| Apr 29, 2020 at 8:16 | comment | added | Flater | @GiladBaruchian That sounds like it could be fixed with more testing and making use of automatic mapping (e.g. Automapper), instead of being an argument against separation of concerns. Good practice sacrifices effort now in order to avoid a lot more effort down the line. Don't fall into the trap of avoiding the initial extra effort because in the long term it leads to even more effort, which is the opposite of what you'd want. | |
| Apr 27, 2020 at 13:43 | comment | added | Gilad Baruchian | @Flater i am working on a big application and we have very big and complex object, just because of this concept we are doing so much work and have many bugs because of mapping, and 99% of the time the DTO and entity are exactly the same | |
| Apr 26, 2020 at 12:41 | comment | added | Flater | @GiladBaruchian: From the answer: "It's important here to realize that the entity represents the database data, and nothing more. If it's not in the database, it's not in the entity." What you're suggesting is to use a DTO but call it an entity. That's just going to lead to confusion and muddying the separation between your persistence and domain/business layer. | |
| Apr 26, 2020 at 7:51 | comment | added | Gilad Baruchian | what if we dont create the DTO, and the Service class adds the age to the entity (like it would for the DTO) ? | |
| Jan 16, 2020 at 22:45 | comment | added | Chuck Adams | @Flater I understand, I was trying to "demystify" DTOs by presenting them as a verbose-but type-safe convention for named arguments. Nothing to be persisted or treated as the forever gospel of one's business object schema. In that sense, it's just a "glue layer", not a Great Abstraction Of Enterprisiness ;) | |
| Jan 16, 2020 at 10:35 | comment | added | Flater | @ChuckAdams: For the purpose of my answer specifically, there's no reason to distinguish between a DTO class and a collection containing the same data. The question and answer focus on the the necessity of the layer/abstraction rather than its specific structure. | |
| Jan 13, 2020 at 17:18 | comment | added | Chuck Adams | DTOs are a simple protocol in the form of a data structure all parties agree on. You can also see them as arguments objects. Languages that support anonymous records (whether it calls them "dicts" or "objects" or whatever) tend just pass those around rather than creating whole new DTO classes and get largely the same benefits. You do get type safety with DTOs but structural subtyping would work just as well (which I don't imagine java will ever get) | |
| Nov 27, 2019 at 11:24 | comment | added | Flater | @Arash: The difference between what you call a "business object" and a "view object" is context. To us humans, that distinction matters to understand things properly. But the compiler (and by extension the language itself) doesn't see a technical difference between them. When I say they're the same, I mean that from a technical perspective. Both are just a class with properties intended to hold data and be passed around. In that regard, there is no difference between them. | |
| Nov 6, 2019 at 17:02 | comment | added | Arash | One more question. You said "business object and a view object". I thought that both are equal. When I searched, I realized that Business Object has general meaning to compare with View Object. But Business Object should be derived from use case and Entity Object should be derived from Data Model therefore they are different, am i right? could you explain it a bit, please? | |
| Nov 6, 2019 at 16:13 | comment | added | Arash | Now that's make sense to me. Thanks a lot. Flater. | |
| Nov 6, 2019 at 11:18 | comment | added | Flater | @Arash (1) "DTO" is really a catch-all definition for any data class that is used for exchanging between two layers. A business object and a view object are both DTOs. (2) That very much depends on a lot of things. Creating a new dto for every collection of fields you requires is a cumbersome task. There's nothing inherently wrong with simply returning a full company DTO (where reasonable) and then letting the view pick the fields it's interested in. It's a matter of finding the balance between implementing adequate separation of concerns and avoiding overengineering and pointless repetition. | |
| Nov 6, 2019 at 10:24 | comment | added | Arash | Comprehensive answer, Thanks. I have questions; Appreciate if you answer: 1- Correct me, if i'm wrong. Business Object or View Object is for transferring data between Presentation Layer and Business Layer and Entity Object is for transferring data between Business Layer and Data Access Layer. and DTO can be used as a dumb BO. 2- Assume that two views need different info of a company then we need two different companyDTOs? | |
| Jul 3, 2018 at 9:10 | vote | accept | Naveen Kocherla | ||
| Jun 28, 2018 at 15:11 | history | edited | Flater | CC BY-SA 4.0 |
added 1819 characters in body
|
| Jun 28, 2018 at 14:18 | history | answered | Flater | CC BY-SA 4.0 |