Add RequestDTO and ResponseDTO in common DTO or use them separately?

4.1k views Asked by At

I'm working on a web service in java. I need help and advice on the issue of Request and Response DTOs. having gone through this question here on stackoverflow:

Reusing DTO for various request/response types vs explicitness of what is required / what should be returned What is better between the two implementation below:

public class PropertyRequestDTO {
private String province;
private String propertyType;
private String propertyArea;
...

public class PropertyResponseDTO {
private String address;
private String street;
private String province;
....

or this:

public class PropertyDTO {
private PropertyRequestDTO propertyRequestDTO;
private PropertyResponseDTO propertyResponseDTO;

In my implementation in setting these DTOs, is better and maintainable to use the PropertyDTO or use the PropertyRequestDTO and PropertyResponseDTO separately?

2

There are 2 answers

2
flopcoder On

I think Request and Response Should be different. There is not need to define PropertyDTO . According to rules you will pass data on user end Response Object no need to pass Request Object again. So it will help to decrease network data. COntroller layer only need PropertyRequestDTO no need to pass PropertyResponseDTO object. so no need to combining this two object to another object. These two Objects purpose is different.

0
TheSprinter On

First thing:
if you keep both request and response in 1 object then that object will be heavier than one single element and always you will be adding unnecessary load to application for every request and response.

Second thing :
Request and response should not be in same object unless both are identical, so separate those objects. This may leads to other problems in future.