A Data Transfer Object(DTO) is an object which is used to encapsulate data. It is commonly used in the Services layer which request data from third party API, or from the system itself. The benefit of DTOs is to convert the raw data in an object and reduce unnecessary information. It also makes a great model in MVC. Moreover, DTO makes the code very easy to maintain and test.
Given that we are writing some code to perform domain check from thrid party API.
Let’s see the example below:
The response is looked like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
In order to access the attributes of the response, it will requires to go through all the hierarchy key structure:
1 2 3 4 5
Let’s say that we want to check whether or not the domain is available(domain is available only if its status is ‘Available’ and price is less than 15)
The code will look like this:
As you can see, the code above is not really efficient at all, in case of readability and scalability.
In addition, what would happen when some fields in the hierarchy structure
needs to be changed? Let’s say field
result['response']['body'] changed to
result['response']['data']. As a result, the code above will be no longer work, so, the code need to be changed everywhere the
result object is called.
Perform with DTO
First, create a new class called
DTO::Domain. This class is responsible
for translating the data returned from the API into an object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
DTO::Domain, we could instantiate a new object by passing the
data responded from the API.
All the information responded form the API is encapsulated inside a
DTO::Domain class. Only needed information with some additional domain logic
is included in the object. In some case that the field in the API is changed,
DTO::Domain class alone needs to be changed.
1 2 3 4 5 6 7 8
The code now is much cleaner, maintainable, testable, and scalable.