It’s the last week in October which means it’s my last week as an Intern at Narbase. It’s already been three months, the time flew very fast and it was such a journey. And from now on instead of writing these posts as an Intern I will be writing these posts as a full-time mobile application developer at Narbase.
In this week I started working on a new app, it a simple app but contains new concepts that I didn’t encounter before such as navigation drawer and Firebase cloud messaging. I will talk about them in the future, but for this week post, I will dedicate it to answer a question I found in the comments section of a previous post I wrote in the blog. The question was:
So at first what is a DTO?
According to Wikipedia: “A data transfer object (DTO) is an object that carries data between processes.”
In Mobile applications when you have to communicate with a server, you want for sure to transfer data to and from the server, but each call is an expensive call and the majority of the cost of each call is the time of the round trip between your application and the server. Take for example your app want to send to the server the information of a certain restaurant, let’s say we have the name of the restaurant, its location and its rating and each filed require an individual call to the server. Here comes the DTOs, you can encapsulate all these fields in a single DTO class and send it in one call. Very nice huh.
Before showing you an example of using a DTO, let us look at the question again. He mentioned the POJO class. But what is POJO class in the first place?
POJO stands for Plain Old Java Object, it just a data structure that has fields to hold data with getters and possibly also setters, it just like structure in C language. So to answer your last part of your question, yes you hold your data received from the server in a POJO and DTO is a POJO itself.
You asked: What is the role of DTO class in application development?
Let me respond to this question by an example.
Let’s consider we have an app like Goodreads app, and you want the information about a book you liked. You can create a DTO class to hold the book’s name as the following:
data class BookInfoRequestDto( val bookName: String )
Then send that DTO to the server and the response of the server will be held in the response DTO class like the following:
data class BookInfoResponseDto( val bookName: String, val bookAuthor: String, val bookPublishDate: String )
Then you can extract the book information from that class.
I hope this answered your question :).
If you have any questions, thoughts or feedback please lets us know at the comment. But for now, that’s it for this week.
Till next week.
One thought on “Overview of DTO”