vendredi 11 août 2017

Should we have similar DTOs?

I have 2 dtos that are used differently, but they have similar fields. Eg, MyIdESDto uses info from MyIdDto. The latter object is built with data from the DB, while MyIdESDto is built from the ElasticSearch and completed with MyIdDto data.

In my opinion, there should be only 1 DTO, and the code set as many fields in the DTO as it wants. Other people say the MyIdESDto should extend MyIdDto. Other people agree with this implementation, saying that it won't break the code if some feature is deleted. Which one is better to use? Can you give me reasons of your choice?

    @Data
    @NoArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class MyIdESDto {
      private String name;
      private Integer id;
      private Integer age;


     public MyIdESDto(MyDto dto) {
        this.name = dto.getName();
        this.id = dto.getId();
        this.aage = 42;
     }
}




@Data
    @NoArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class MyIdDto {
      private String name;
      private Integer id;
   }

Aucun commentaire:

Enregistrer un commentaire