hello,
I've been looking for something related for a few weeks, but I'm not convinced.
I'm making a RestAPI. And, I know about the DTO.
First, look at the picture above.
The red picture is the part I drew.
Except for the red part, DTO is only mentioned between Controller-Service.
That is, the problem mentions the necessity of using DTO only between Controller-Serve in the example that explains most of the DTO.
But if you dig a little deeper, someone View(Front) - You mention that you need a "separate" DTO used by the Controller.
That means we already need two separate DTOs.
Then next, Now someone says that DTO should be broken down into RequestDTO and ResponseDTO.
Then I have a ControllerRequestDTO that needs to be passed from View to Controller
ServiceRequestDTO that should be passed from Controller to Service
After that, it is transformed into an Entity and after work is done, the returned Entity must be converted into a DTO.
So, ServiceResponseDTO created in Service and passed to Controller
ControllerResponseDTO that should be sent back from Controller to View
I understood that it should be composed of .
But once again, there is very little information about the DTO required by the View-Controller.
ControllerRequestDTO, ControllerResponseDTO, ServiceRequestDTO, ServiceResposeDTO
4 in total.
Am I making it right?
Or are you increasing unnecessary resources?
Software design is not a set of rules to be followed without thinking. Rather, software design is a means to pursue your goals - and different goals may require different means.
So when somebody tells you to create separate DTOs, you should ask (or actually, they should have told you without prompting) why they recommend this, so you can assess whether their goals and circumstances match yours.
Since I am not the person who suggested you create 4 different DTOs, I can only speculate about the goals and circumstances that may have prompted such a recommendation, but I can say with certainty that these goals and circumstances are not universal, and many applications get by just fine with far less DTOs.
Reasons for separate request/response DTOs:
Reasons for same request/response DTOs:
Reasons for different rest/service DTOs:
Reasons for same rest/service DTOs:
As you can see, it depends. But many apps use the same DTO for requests and responses, controllers and services. You should only make additional work for yourself if you see a clear benefit of doing so.