Using DAO, DTO pattern as MVC

    public abstract class BaseDAO<T extends BaseDTO> {

        public Integer create(T dto) {

        }
        public Integer update(T dto) {

        }
        public Integer delete(T dto) {

       }

     }


    public class JobDAO extends BaseDAO<JobDTO> {

        public JobDAO(Connection conn) {
            super(conn);
        }

        @Override
        public String getDBTableName() {
            return "JobTABLE";
        }
    }


    public class BaseDTO {

        protected Integer ID;

        public Integer getID() {
            return ID;
        }

        public void setID(Integer ID) {
            this.ID = ID;
        }
    }


    public class JobDTO extends BaseDTO {

        Integer employerID;
        //getter
        //setter

     }



   public class Job_GUI extends javax.swing.JFrame {
    //GUI properties
    }

      

I am trying to understand the Model, View, Controller

Convention and I want to apply MVC

to the structures of the above class that consist of data transfer and access objects. What I don't understand is my structure over MVC? if so, which model? I assume the DTOs are themselves Model

. Job_GUI is the one View

I already know, but what is it Controller

?

I want to write codes directly actionPerformed

in the Job_GUI document itself, something like this snippet, to create a job in the db:

JobDAO jdao = new JobDAO(conn);
           //create object jobDTO to hold all form values to be passed to JobDAO
           final JobDTO jobDTO = new JobDTO();
           //populating JobDTO with values from form
           jobDTO.setEmployerID(id);
           jobDTO.setDescription(description.getText());
           jobDTO.setTitle(txtTitle.getText());
           jdao.create(jobDTO);

      

but if the above should be in class Job_GUI

or somewhere else. If I were pasting the above snippet in Job_GUI

, then am I moving away from the MVC convention as such? Where would you put the above snippet? The confusion lies in determining what is a class Controller

from whatever I have, if I have to use DTO

, DAO

Design Pattern

to interact with the database.

+3


source to share


1 answer


       JobDAO jdao = new JobDAO(conn);
       //create object jobDTO to hold all form values to be passed to JobDAO
       final JobDTO jobDTO = new JobDTO();
       //populating JobDTO with values from form
       jobDTO.setEmployerID(id);
       jobDTO.setDescription(description.getText());
       jobDTO.setTitle(txtTitle.getText());
       jdao.create(jobDTO);

      

In the above code, you are basically doing the Controller's work because you assign values ​​to your DTO and then call the create method of your DAO. This is what the controller is supposed to do. So, you are correct on this part, except for one small correction:

    JobDAO jdao = new JobDAO(conn);  //not preferable
    JobDAO jdao = new JobDAO();  //preferred inside Controller class

      

Prompts you to save the connection related code in your DAO. Your controller class doesn't have to know about your DB connection. You can accomplish this in your DAO create / update / delete methods.



DAO where you actually interact with DB is part of MODEL in MVC.

Just keep a few things in mind:

  • MVC (Architectural Design Pattern) emphasizes a separation of concerns. So make sure you have well defined classes for View, Controller and Model.
  • The class names must indicate the components to which it belongs (BaseDAO, etc.)
  • Controller controls View and Model. This way, your gaze and your model don't need to know about each other.

And as you mentioned, you already figured it out with View. So I didn't explain it.

+3


source







All Articles