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?
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.
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.
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.
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With