Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One table DAO vs multiple table DAO

I am designing some "blocks" of data in JSF and I am getting my data from a Java bean per block. My problem is that my blocks get their data from more than one table. For example:

A block that describes personal data of a customer consists of:

  • customer name (in table customer)
  • customer surname (in table customer)
  • customer phone (in table customer)
  • customer address (in table address)
  • customer company (in table company)
  • customer phone at work (in table company)

I will have to access 3 separate tables in order to fill this block. Rather than constructing 3 different DAOs (one for each table), isn't it better to construct one DAO per block? I understand that this way has a disadvantage regarding consistency, because if one table will be accessed by more than one DAOs, each change of the table will have to be applied to every one of them. But my code will be much more understandable according to my specific business logic. What are other downsides here? Is it generally advised to create DAOs with access to many tables?

like image 666
Stefanos Kargas Avatar asked Oct 17 '13 12:10

Stefanos Kargas


1 Answers

My solution to this problem is implementing a new method called getCustomerWithAddressAndCompanyInfo (or something else shorter) in CustomerDAO. This method runs a single query joining customer, address and company tables and returns the result by a single access to the database.

If you choose accessing 3 DAOs, you have to execute 3 distinct queries on the database which may create a performance issue. As you said, this can cause an inconsistency also.

Another approach may be collecting such complex query methods in a separate class like CustomerQueries rather than implementing them in existing DAO classes.

like image 102
ovunccetin Avatar answered Nov 03 '22 01:11

ovunccetin