Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Cinthya Spitzer




Posted Questions



Wait...

Posted Answers



Answer


A few weeks ago I was working on a bug. The issue was whenever business support people tried to edit some ticket seat trait (e.g. aisle seat, parking pass) using our internal tool, some value for the ticket seat trait became null when changes are saved. I work at StubHub, a secondary ticket marketplace, in case you wonder why I'm talking about tickets and seats.

Upon investigation, I found out that this value being nulled-out was a new column added to our ticket seat trait table. What caught my eyes was the convoluted way data from this table is exposed to the user:

By the time you read this, you probably have guessed it why the values are null: some one forgot to update the code that does the conversion between the different models when the new column was added to the persistent model. I was pretty upset when I saw this. NOT that I'm mad at the person who forgot to update the conversion code. I wouldn't blame him/her at all. I probably would have made the same mistake because there's no easy way to detect this type of errors. What drove me nuts was the use of the intermediary classes that does nothing but making the code bloated and fragile. What a classic example of misuse of the Data Transfer Object (DTO) pattern!

And that's not all. I searched through our code and counted. We have around 2500 DTOs in all the projects!

Data Transfer Object, also know as "Value Object", originated from the book "Core J2EE Patterns: Best Practices and Design Strategies":

In Martin Fowler's P of EAA catalog, he defines DTO as the following (notice that he uses Value Object for a different concept):

It may not be obvious from the above descriptions, but the problem the DTO pattern tries to address is: method calls to EJB objects can be remote, hence expensive. So instead of making multiple calls across the network, DTO is used to fetch all needed data in one call to reduce the network chattiness and improve the performance.

Here is more detailed explanation of the DTO pattern:

http://www.oracle.com/technetwork/java/transferobject-139757.html

Yesterday's pattern has become today's anti-pattern. EJB is something from the past that's no longer used any more. With Spring/Hibernate or JPA, entity objects are just POJOs, there's no performance overhead when you pass them between layers. But people continue to use DTO for the wrong purpose. In the example at the beginning of this article, using DTOs adds not benefits but extra cost of maintenance.

Some people may argue that DTO can act as a layer of abstraction so that changes to the entity object won't break upper layers that consume the object. In my opinion, that's exactly what you should not do. It's better to have the problem blowup in your face right away rather than having subtle bugs in production which are much more difficult to find and much more costly to fix. In the example above, if there weren't any DTOs between layers, the bug wouldn't exist at all since any modification to the entity object will be immediately available to the consuming layers. If the changes are not compatible with the consumers, the source code simply won't compile, which is a good thing because it's much easier to catch and fix. If we don't have control of the consumers and are worried that consumers will be broken by the changes, we can simply version our library.

Adam Bien has an article that goes even further by declaring that PREMATURE ENCAPSULATION IS THE ROOT OF ALL EVIL. He argues that any abstraction is leaky. Layers of abstraction makes the source code dominated by infrastructure code which hides the core logic. The result is bloated code that's difficult to understand and maintain.

Don't use Data Transfer Object unless you have a strong case for it. One such case maybe for performance reason. If using DTO can avoid multiple round trips that are expensive (remote calls, database visits, etc.), go ahead and do it. That is what DTO was originally introduced for: to improve performance and workaround older versions of J2EE's shortcomings. What has changed is that those shortcomings no longer exist so cases when you need DTO has gone down to almost never.

When in doubt, leave it out. You'll thank yourself later for staying away from DTOs. And your colleagues will appreciate it as well.


Answer is posted for the following question.

why dto is anti pattern?

Answer


Thankfully, there is a way for users of the Oculus Quest 2 to not have to use Facebook The Oculess app allows users to bypass the Facebook


Answer is posted for the following question.

How to bypass oculus setup?

Answer


1 Answer Wikipedia gives the etymology as Piruw [pɪɾʊw], from Quechua, the Inka language That [w] at the end would become a /v/ in Spanish when adding a suffix to produce Peruviano English demonyms have lots of odd features, and this isn't even English demonymy


Answer is posted for the following question.

Why is there a v in peruvian?

Answer


Description The Romsey Community Hospital is a hospital in Romsey, England. It is operated by the Southern Health NHS Foundation Trust. Wikipedia


Answer is posted for the following question.

Lovely people Hey Where is romsey hospital?

Answer


  • knowledge of teaching and the ability to design courses.
  • excellent verbal communication skills.
  • the ability to teach pupils how to do something.
  • customer service skills.
  • leadership skills.
  • to be thorough and pay attention to detail.
  • the ability to work on your own.

Answer is posted for the following question.

How to be a martial arts instructor?


Wait...