Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One DAO per 'container' class or one DAO per table?

I have a 'container' class with fields that are contained in several database tables, and I use the DAO pattern for accessing the data.

The question is, should I create a single DAO for this 'container' class, or is it better to have one DAO per table and combine their data?

like image 221
Anna Avatar asked Aug 03 '15 23:08

Anna


People also ask

What is a DAO class?

Accessing a database with a DAO. A Data Access Object class can provide access to a particular data resource without coupling the resource's API to the business logic.

Why we use DAO class in spring?

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate, JPA or JDO in a consistent way.

What is DAO class in spring boot?

DAO stands for data access object. Usually, the DAO class is responsible for two concepts: encapsulating the details of the persistence layer and providing a CRUD interface for a single entity.

What is DAO in database?

DAO is an object-based data access interface that provides access to SQL data sources through VBA. Using DAO, you can: Retrieve, add, change, or delete data in a database. Create a new database or change the design of an existing database.


1 Answers

You should design your DAO according to your application needs, not the layout of your database. Start of with one DAO, and if it becomes too large, then refactor it into multiple DAOs in a way that makes sense to your code.

The whole point of a DAO is to hide any database concepts (like tables) from your application. Your application should just view it as a service with some useful methods.

For example, if your application needs some user data that comes from both the Users table and the EmailAddresses table, your application code should not have to coordinate two DAOs - it should call one DAO method getUserDetails() and the DAO will hide the fact that multiple tables need to be called.

I am recommending the first of the options in your question, but I wouldn't restrict yourself to the rule "one DAO per container class".

like image 183
user1886323 Avatar answered Sep 25 '22 07:09

user1886323