There is a model of objects, DAO interfaces for each class of objects, service classes with the implementation of each DAO interface. You want to implement a DTO layer. What is the difference between DTO and DAO implementation? And how to properly organize in this case DTO?

  • one
    dto is a simplified model for returning somewhere. Simple POJOs with getters and setters without logic. Usually without circular references. dao has nothing to do with it. - Nofate

1 answer 1

DTO is an abbreviation for transferring object data, so it is used to transfer data between the classes and modules of your application. DTO must contain only private fields for your data, getters, setters and constructors. It is not recommended to add business logic methods of such classes, but it is normal to add some utilities.

DAO is an abbreviation for a data access object, so it must encapsulate the logic for extracting, saving, and updating data in the data warehouse (databases, file systems, whatever). Below is an example of how the DAO and DTO interfaces would look like this:

interface PersonDTO { String getName(); void setName(String name); //..... } interface PersonDAO { PersonDTO findById(long id); void save(PersonDTO person); //..... } 

Conclusion

DTO is subject to data transfer. It is basically an object value used to transfer structured data between layers / layers.

DAO is a data access entity. It is responsible for hiding implementation details about how your data is stored and how it is retrieved.

  • DTO, too, how do I see the interface? And the implementation of data transfer, for example, in the UI, that is, where should the interface be implemented? - Alexander Dermenzhi
  • @AlexanderDermenge implement the Dto model you need on the server that will be used on the client. - Senior Pomidor
  • Can I ask to check what I did? Or is it better to create a separate topic for review? There is a project with objects, DAO, DTO, services. Since I’m not sure how DTO is associated with real objects: github.com/Dermenji/WeatherOOP/tree/master/src/com/sirma - Alexander Dermenzhi
  • @AlexanderDermengee is better to create a separate topic, so as not to litter this issue - Senior Pomidor
  • one
    @AlexanderDermengee already see one comment on DAO - Senior Pomidor