184
  1. Why do we use DTO and DAO, and when should we use them. I am developing a GUI Java software to do with inserting, editing, deleting data. But I am struggling to distinguish between DTO/DAO and Model, View, Controller (MVC) structure? Are they similar, which is better to use when interacting with database through Java GUI.

  2. One thing I'm really curious about is whether it is a good practice to have View and Controller in one class. If we think about NetBeans, you can create GUI Frame class and add components like JButton onto the frame, double clicking the button will take you to the actionListener method (Controller) which appears to be in the frame the data is to be displayed to the user (View). So they're in the same class. Is that completely going against the concept then or not?

Here is what I'm talking about

Is it bad practice to have view and controller in one class?

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
Hoody
  • 2,942
  • 5
  • 28
  • 32
  • 1
    @RomanC have a database table which holds Events (eventId,name,date.etc), so DTO and MVC, same or different? – Hoody Jan 16 '13 at 19:24
  • At the database you haven't DTO but database objects. – Roman C Jan 16 '13 at 19:27
  • @RomanC in the Java classes sorry, using DTO/DAO structure or should be using MVC? what is the difference – Hoody Jan 16 '13 at 19:29
  • read [this](http://en.wikipedia.org/wiki/Data_Transfer_Object) and [that](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) check the difference – Roman C Jan 16 '13 at 19:32
  • did you read about database at the second def? – Roman C Jan 16 '13 at 19:35
  • if not, the second question about if you use a second choice how it would be related to the first one? – Roman C Jan 16 '13 at 19:40
  • if couldn't find answer check [this](http://en.wikipedia.org/wiki/Value_object#Value_Objects_in_Java) too for easy explanation. – Roman C Jan 16 '13 at 19:54
  • 1
    1) Seems like you have a lot of different concepts and patterns that, while are often used together in applications, should be tackled one-by-one. DTO and DAO are quite different concepts, and it's really not an "either/or" when contrasted with MVC. – Philip Tenn Jan 16 '13 at 20:10
  • related https://stackoverflow.com/questions/1612334/difference-between-dto-vo-pojo-javabeans – Ahmed Nabil May 23 '19 at 09:11

1 Answers1

371

DTO is an abbreviation for Data Transfer Object, so it is used to transfer the data between classes and modules of your application.

  • DTO should only contain private fields for your data, getters, setters, and constructors.
  • DTO is not recommended to add business logic methods to such classes, but it is OK to add some util methods.

DAO is an abbreviation for Data Access Object, so it should encapsulate the logic for retrieving, saving and updating data in your data storage (a database, a file-system, whatever).

Here is an example of how the DAO and DTO interfaces would look like:

interface PersonDTO {
    String getName();
    void setName(String name);
    //.....
}

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

The MVC is a wider pattern. The DTO/DAO would be your model in the MVC pattern.
It tells you how to organize the whole application, not just the part responsible for data retrieval.

As for the second question, if you have a small application it is completely OK, however, if you want to follow the MVC pattern it would be better to have a separate controller, which would contain the business logic for your frame in a separate class and dispatch messages to this controller from the event handlers.
This would separate your business logic from the view.

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
Petr
  • 5,999
  • 2
  • 19
  • 25
  • First question was about the difference betweet DTO/DAO vs MVC, I think. – madth3 Jan 16 '13 at 19:48
  • If I wanted to add triggers to enable/disable something like the `PersonDAO.save()`, would I put it directly in the `save()` method? Also, for database performance counters, would I add timers directly to the `save()` method? Also, is it acceptable to have a PersonDAO ref inside PersonDTO? – BenR Oct 26 '15 at 16:25
  • 5
    I'm not sure what you mean by "separate controller". From my readings, the controller in MVC should be as skinny as possible and your business logic should be encapsulated in your models. – Paul Carlton Nov 08 '16 at 01:03
  • 11
    Using DTO's to interface Object model is a terrible practice. void save(PersonDTO person) is pure cancer. – Ben Jun 09 '17 at 12:45
  • 6
    why do we need DTO when we have Class objects like POJO? arent they both the same? – Bhargav Jun 20 '17 at 14:08
  • 8
    @Bhargav DTOs are used to return combined results of multiple entities(POJOs) or limited resources from an entity. – The Coder Feb 21 '19 at 18:44
  • 1
    @Bhargav also you can consider DTOs are Request/Response Payloads which of course can be different of POJO or Entity Domain Model – Ahmed Nabil May 24 '19 at 21:53
  • If I have one model Person (name, age) and I want to persist it in 2 different databases (postgres & mongodb) does that mean I will have 1 DTO and 2 DAOs ? – furiabhavesh Dec 04 '19 at 07:59
  • Or will it be 1 DTO class, 1 DAO interface and 2 DAO Implementations ? – furiabhavesh Dec 04 '19 at 08:31
  • 3
    @Ben I know it's too but this is interesting .late.how would you write `void save(PersonDTO person)` in a better way ? – Madhawa Priyashantha Feb 19 '21 at 09:02