Reputation: 1384
Normally when I create REST applications I create them in the following way:
Controller (Receive/Return Dtos) -> Service (Receive and Return Dtos and internally map from-to Entities using MapStruct) -> Repositories (Always receive Entities)
But I realized that now we can use the spring property:
spring.jpa.open-in-view=false
So I'm wondering if the Dto usage in Spring REST applications is still needed and what are the implications of using them or avoid using them?
Upvotes: 1
Views: 924
Reputation: 1523
I think there is no correct method. As you said, we need to discuss the tradeoffs.
Developing using one model for all layers, might make faster your development. You will not spend time creating several DTOs (and also avoid creating the mapping between your entity model and DTOs). However, you will couple all. A small change in your model can affect all layers and you will probably have to deal with it. More effort might be spent whenever you need to adapt your code.
An alternative is to have DTOs (or dedicated/specific models) for each layer which might increase your development time. In this case, whenever a change is requested, the impact into other layers can be small. I mean, the risk of breaking things in other layers is reduced. As in your question, a change in the entity model will not affect the REST API since it exposes just the DTO.
Summarizing, the main question is: Do you prefer "speed of development" or "losing coupling between layers/classes"?
As you see, it depends on your need.
Upvotes: 2