Dmitriy Mishenyov
Dmitriy Mishenyov

Reputation: 319

Architectural approach to consume single DTO from many REST services

In my Spring RESTful Service I have Domain Object Car, all service business logic uses it. These objects are obtained from CarDTO objects which in turn are obtained from several external services using RestTemplate. The questions are:

  1. Is it rational to create separate DTO for each external service if consuming structure is different and map them do domain object by different converters, or it's better to use general DTO and converter?
  2. If my previous suggestions are wrong - what is the pest practice of consuming domain object from several api with different structure?

Car domain object:

public class Car {
private Company company;
private String model;
private Location location;
private Double fuel;
private Double price; 

// getters / setters
}

Upvotes: 1

Views: 452

Answers (1)

Devkinandan Chauhan
Devkinandan Chauhan

Reputation: 1925

There is no hard and fast rule that you should create DTOs.

You can directly use your DB object(Car) instead of in-between DTOs. As per my understanding, if there is everything direct field to field mapped then no need for DTOs.

If we have logic or transformations to perform from Request/Response to convert it to DB objects then DTOs are perfect to use to make code clean. It Decouples persistence models from API models and makes code for maintainable.

For more detail, please go through REST API - DTOs or not?

Upvotes: 1

Related Questions