Using data access objects (DAOs) offers a clear advantage: it separates two parts of an application that don't need to know about each other. This separation allows them to evolve independently. If business logic changes, it can rely on a consistent DAO interface. Meanwhile, modifications to persistence logic won't affect DAO clients.23
All details of storage are hidden from the rest of the application (see information hiding). Unit testing code is facilitated by substituting a test double for the DAO in the test, thereby making the tests independent of the persistence layer.
In the context of the Java programming language, DAO can be implemented in various ways. This can range from a fairly simple interface that separates data access from the application logic, to frameworks and commercial products.
Technologies like Java Persistence API and Enterprise JavaBeans come built into application servers and can be used in applications that use a Java EE application server. Commercial products such as TopLink are available based on object–relational mapping (ORM). Popular open source ORM software includes Doctrine, Hibernate, iBATIS and JPA implementations such as Apache OpenJPA.4
Potential disadvantages of using DAO include leaky abstraction, code duplication, and abstraction inversion. In particular, the abstraction of the DAO as a regular Java object can obscure the high cost of each database access. Developers may inadvertently make multiple database queries to retrieve information that could be returned in a single operation. If an application requires multiple DAOs, the same create, read, update, and delete code may have to be written for each DAO.5
Note that these disadvantages only appear when you have a separate DAO for each table and the SELECT query is prevented from accessing anything other than the target table.
"Core J2EE Patterns - Data Access Objects". Sun Microsystems Inc. 2007-08-02. http://www.oracle.com/technetwork/java/dataaccessobject-138824.html ↩
"Data Access Object(DAO) Design Pattern". DigitalOcean. 2022-08-03. Retrieved 2024-07-08. https://www.digitalocean.com/community/tutorials/dao-design-pattern ↩
"Data Access Object(DAO) Design Pattern". GeeksforGeeks. 2017-08-26. Retrieved 2024-07-08. https://www.geeksforgeeks.org/data-access-object-pattern/#8-advantages-of-the-data-access-object-designdao-pattern ↩
"Data Access Object(DAO) Design Pattern". GeeksforGeeks. 2017-08-26. Retrieved 2024-01-29. https://www.geeksforgeeks.org/data-access-object-pattern/ ↩
See http://www.ibm.com/developerworks/java/library/j-genericdao/index.html for workarounds http://www.ibm.com/developerworks/java/library/j-genericdao/index.html ↩
Hodgson, Kyle; Reid, Darren (2015-01-23). ServiceStack 4 Cookbook. Packt Publishing Ltd. p. Chapter 4. ISBN 9781783986576. Retrieved 22 June 2016. 9781783986576 ↩