Reputation: 319
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:
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
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