When do I need to add a DTO to a class name? For example, I have the Country entity (i.e., in fact, the COUNTRY table in the database), in Java I create a class that maps to this table via ORM.

@Entity @Table(name = "COUNTRY") public class CountryDTO implements java.io.Serializable { private static final Long serialVersionUID = 2475653940063260199L; @Id @Column(name="ID") private Long countryId; ... } 

The question is how to call this class CountryDTO or just Country. I have seen both versions in different sources.

  • one
    In essence, DTO is a pattern. Someone for appending the name of the pattern to the name of the class, someone against. If SomeClassFactory is written, then immediately it is clear that this is a factory, and it is necessary to dig from there. With DTO, I think the same thing. My opinion is to write unequivocally. I was digging around so much in the code, I thought that for an idiotic architecture, and when I figured out, I built a class diagram, the Linker pattern turned out. - Anton Feoktistov

1 answer 1

The data transfer object ( DTO pattern usually involves data transfer between application layers (for example, between business logic and presentation). Such objects may correspond to entities of the data layer, may have additional calculated fields, may have a reduced set of fields, which is necessary in a particular case. Moreover, DTO objects can be exclusively service and not at all correspond with one essence of the data model. So, for such objects, the DTO suffix hints at their purpose and allows them to be distinguished from data model entities.

In this example, we are talking about the data storage layer. If you plan to further convert the entities of the data model to transfer somewhere, then the DTO suffix is ​​out of place, in my opinion.