Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it fine to have several DAOs?

Tags:

java

mysql

dao

Let's say I have one database with two tables: companies and employees. Should I create companyDAO and employeeDAO If I select, update, add and remove from those two tables?

Or I should create one DAO in which I'll write methods for CRUD operations for every table in the database?

like image 352
THE Waterfall Avatar asked Jan 28 '23 01:01

THE Waterfall


2 Answers

Creating one DAO for the entire database doesn't scale well. The more database tables you add the larger it gets. In industry you'll almost always see a one to one DAO to table relationship. As always there are exceptions and caveats.

like image 113
Robert Garrett Avatar answered Jan 30 '23 16:01

Robert Garrett


As the name suggest, DAO that stands for Data Access Object, is an entity to access ONE object or class. It is standard software engineering and enterprise industry (esp. in Java) best practices to have one DAO for each business entity.

If you are reusing similar behavior and data among several DAOs it is often handy to create a base DAO and extends it by other DAOs.

Also read https://en.wikipedia.org/wiki/Data_access_object

like image 28
royalghost Avatar answered Jan 30 '23 15:01

royalghost