Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data transfer object in dao design pattern

I am bit confused about what data should a DTO contain. For example let's assume that we have two tables: User, and Orders. Orders table contains id_users, which is foreign key to user table.

Obviously I have two DAOs, MysqlUserDao and MysqlOrdersDao, with crud operations, and two transfer objects User, and Order, in which I store jdbc rowset.

If I want to get the list of users and for each user all his orders how should I do:

1) In my MysqlUserDao create a function: getUsersAndOrders(select users.,orders. from users join orders) And my User DTO should have a OrderList property in where i put orders ?

2) In my MysqlUserDao i create a function getAllUsers(select * from users), and foreach user I use MysqlOrdersDao function getOrder(id_user);

And some clarifications:

1) For each table in database I need to create a DAO object? or just for complex ones? For example products and images, should be 2 dao or just one?

2) a DTO object should have only properties and setter getter, or it is possible to have other methods like convertEuroToUsd etc.

thanks

like image 356
Catalin Avatar asked Jul 12 '13 21:07

Catalin


People also ask

What are data transfer objects in Java?

A Data Transfer Object is, essentially, like a data structure. It should not contain any business logic but should contain serialization and deserialization mechanisms. DTOs can either contain all the data from a source, or partial data. They can hold data from single or multiple sources as well.

What is difference between POJO and DTO?

DTO: "Data transfer objects " can travel between seperate layers in software architecture. VO: "Value objects " hold a object such as Integer,Money etc. POJO: Plain Old Java Object which is not a special object.

What is DTO used for?

A data transfer object (DTO) is an object that carries data between processes. You can use this technique to facilitate communication between two systems (like an API and your server) without potentially exposing sensitive information. DTOs are commonsense solutions for people with programming backgrounds.

What is DAO design pattern?

DAO stands for Data Access Object. DAO Design Pattern is used to separate the data persistence logic in a separate layer. This way, the service remains completely in dark about how the low-level operations to access the database is done. This is known as the principle of Separation of Logic.


1 Answers

In your scenario #1 is the best option because #2 generates too much overhead.

1) In my MysqlUserDao create a function: getUsersAndOrders(select users.,orders. from users join orders) And my User DTO should have a OrderList property in where i put orders ?

Clarifications: 1: If your database has a good Design, then a DAO for each table is a good approach. There some cases where you can merge DAOs together (e.g: inheritance).

2: Yes. It should be a plain bean (or POJO if you want). I suggest creating another layer where you can define your workflow. I've seem people calling this extra layer as model, sometimes DataManager, sometimes just Manager.

For instance: When creating a order you should insert a record in Order table and also insert a record in the Notification table (because end users will be notified via email every time a order is created)

class OrderManager {
   private OrderDAO oDao;
   private NotificationDao nDao;

   public saveOrder(OrderDTO o) {
      Long orderId = oDao.save(o);
      NotificationDTO n = new NotificationDTO();
      n.setType(NotificationType.ORDER_CREATED);
      n.setEntityId(orderId);
      nDao.save(n);
   }
}

UPDATE: In most cases we can say that:

  • "Managers" may handle many DAOs;
  • DAOs should not contain other DAOs and are tied to a DTO;
  • DTOs can contain other DTOs

There is an important idea of LAZY or EAGER load when it comes to handling collections. But this is another subject :D

like image 89
Rafa Avatar answered Oct 02 '22 08:10

Rafa