HOME

TheInfoList



OR:

In
software Software consists of computer programs that instruct the Execution (computing), execution of a computer. Software also includes design documents and specifications. The history of software is closely tied to the development of digital comput ...
, a data access object (DAO) is a pattern that provides an abstract interface to some type of
database In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and a ...
or other persistence mechanism. By mapping application calls to the persistence layer, the DAO provides data operations without exposing database details. This isolation supports the single responsibility principle. It separates the data access the application needs, in terms of domain-specific objects and data types (the DAO's public interface), from how these needs can be satisfied with a specific
DBMS In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and ana ...
(the implementation of the DAO). Although this
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" ...
is applicable to most programming languages, most software with persistence needs, and most databases, it is traditionally associated with
Java EE Jakarta EE, formerly Java Platform, Enterprise Edition (Java EE) and Java 2 Platform, Enterprise Edition (J2EE), is a set of specifications, extending Java SE with specifications for enterprise features such as distributed computing and web serv ...
applications and with relational databases (accessed via the
JDBC Java Database Connectivity (JDBC) is an application programming interface (API) for the Java (programming language), Java programming language which defines how a client may access a database. It is a Java-based data access technology used for Java ...
API because of its origin in
Sun Microsystems Sun Microsystems, Inc., often known as Sun for short, was an American technology company that existed from 1982 to 2010 which developed and sold computers, computer components, software, and information technology services. Sun contributed sig ...
' best practice guidelines "Core J2EE Patterns". This object can be found in the Data Access layer of the 3-Tier Architecture. There are various ways in which this object can be implemented: * One DAO for each table. * One DAO for all the tables for a particular DBMS. * Where the SELECT query is limited only to its target table and cannot incorporate JOINS, UNIONS, subqueries and Common Table Expressions (CTEs) * Where the SELECT query can contain anything that the DBMS allows.


Advantages

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. All details of storage are hidden from the rest of the application (see
information hiding In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
).
Unit testing Unit testing, component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior. Unit testing describes tests that are run at the unit-level to contrast testing at the Integration ...
code is facilitated by substituting a
test double A test double is software used in software test automation that satisfies a dependency so that the test need not depend on production code. A test double provides functionality via an interface that the software under test cannot distinguish fro ...
for the DAO in the test, thereby making the tests independent of the persistence layer. In the context of the
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
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 Jakarta Enterprise Beans (EJB; formerly Enterprise JavaBeans) is one of several Java APIs for modular construction of enterprise software. EJB is a server-side software component that encapsulates business logic of an application. An EJB web ...
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 Doctrine (from , meaning 'teaching, instruction') is a codification (law), codification of beliefs or a body of teacher, teachings or instructions, taught principles or positions, as the essence of teachings in a given branch of knowledge or in a ...
, Hibernate, iBATIS and JPA implementations such as
Apache OpenJPA OpenJPA is an open source software, open source implementation of the Java Persistence API specification. It is an object-relational mapping (ORM) solution for the Java (programming language), Java language, which simplifies storing objects in dat ...
.


Disadvantages

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. 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.


Tools and frameworks

* ODB compiler-based object–relational mapping (ORM) system for C++ * ORMLite: Lightweight object–relational mapping (ORM) framework in Java for JDBC and Android * Microsoft Entity Framework * DBIx::Class object–relational mapping (ORM) module for
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
* TuxORM: Simple object–relational mapping (ORM) library in Java for JDBC * Persist (Java tool) Java-based object–relational mapping and data access object tool


See also

* Create, read, update and delete (CRUD) * Data access layer * Service Data Objects * Object–relational mapping


References

{{Design Patterns Patterns Architectural pattern (computer science) Software design patterns