1. Overview

Microservices have become popular in recent years. One of the essential characteristics of microservices is that they are modular, isolated, and easy to scale. The microservices need to work together and exchange data. To achieve this, we create a shared data transfer objects called as DTOs.

In this article, we will present ways in which DTOs are shared between microservices.

2. Exposing Domain Objects as DTO

Models that represent the application domain are managed using microservices. Domain models are different concerns, and we separate them from data models in the DAO layer.

The main reason for this is that we don’t want to expose the complexity of our domain through the services to the clients. Instead, we expose DTOs between our services that serve application clients through REST APIs. While DTOs pass between these services, we convert them to domain objects.

The service-oriented architecture above schematically shows the components and flow of DTO to Domain objects.

3. DTO Sharing Between Microservices

Take, as an example, the process of a customer ordering a product. This process is based on the Customer-Order model. Let’s look at the process from the side of the service architecture.

Let’s say that the Customer service sends request data to the Order service as:

"order": {
    "customerId": 1,
    "itemId": "A152"
}

The Customer and Order services communicate with each other using contracts_._The contract, which is otherwise a service request, is displayed in JSON format. As a Java model, the OrderDTO class represents a contract between the Customer service and the Order service:

public class OrderDTO {
    private int customerId;
    private String itemId;
 
    // constructor, getters, setters
}

#microservices #developer

Learn Ways in Which DTOs are Shared Between Microservices
42.65 GEEK