Data access object
   HOME

TheInfoList



OR:

In
software Software is a set of computer programs and associated documentation and data. This is in contrast to hardware, from which the system is built and which actually performs the work. At the lowest programming level, executable code consist ...
, a data access object (DAO) is a pattern that provides an abstract
interface Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Int ...
to some type of
database In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases ...
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 The single-responsibility principle (SRP) is a computer programming principle that states that "A module should be responsible to one, and only one, actor." The term actor refers to a group (consisting of one or more stakeholders or users) that ...
. 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 stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases ...
(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" boo ...
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 ser ...
applications and with
relational databases A relational database is a (most commonly digital) database based on the relational model of data, as proposed by E. F. Codd in 1970. A system used to maintain relational databases is a relational database management system (RDBMS). Many relatio ...
(accessed via the
JDBC Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. I ...
API because of its origin in
Sun Microsystems Sun Microsystems, Inc. (Sun for short) was an American technology company that sold computers, computer components, software, and information technology services and created the Java programming language, the Solaris operating system, ZFS, t ...
' best practice guidelines "Core J2EE Patterns".


Advantages

The primary advantage of using data access objects is the rigorous separation between two parts of an application that have no need to know anything about each other, and which therefore can evolve frequently and independently. Changing business logic can rely on a constant DAO interface, while changes to persistence logic do not 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 In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures&md ...
code is facilitated by substituting a
test double In computer programming and computer science, programmers employ a technique called automated unit testing to reduce the likelihood of bugs occurring in the software. Frequently, the final release software consists of a complex set of objects or ...
for the DAO in the test, thereby making the tests independent of the persistence layer. In the context of the
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
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 Jakarta Persistence (JPA; formerly Java Persistence API) is a Jakarta EE application programming interface specification that describes the management of relational data in enterprise Java applications. Persistence in this context covers three ...
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 Oracle TopLink is a mapping and persistence framework for Java developers. TopLink is produced by Oracle and is a part of Oracle's OracleAS, WebLogic, and OC4J servers. It is an object-persistence and object-transformation framework. TopLink p ...
are available based on object-relational mapping (ORM). Popular open source ORM software includes
Doctrine Doctrine (from la, doctrina, meaning "teaching, instruction") is a codification of beliefs or a body of teachings or instructions, taught principles or positions, as the essence of teachings in a given branch of knowledge or in a belief syste ...
, Hibernate,
iBATIS iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs ( Plain Old Java Objects). The mappings are decoupled from the application logic b ...
and JPA implementations such as
Apache OpenJPA OpenJPA is an open source implementation of the Java Persistence API specification. It is an object-relational mapping (ORM) solution for the Java language, which simplifies storing objects in databases. It is open-source software distributed und ...
.


Disadvantages

Potential disadvantages of using DAO include
leaky abstraction In software development, a leaky abstraction is an abstraction that leaks details that it is supposed to abstract away. As coined by Joel Spolsky, the Law of Leaky Abstractions states: This statement highlights a particularly problematic cause o ...
, code duplication, and
abstraction inversion In computer programming, abstraction inversion is an anti-pattern arising when users of a construct need functions implemented within it but not exposed by its interface. The result is that the users re-implement the required functions in terms o ...
. 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.


Tools and frameworks

*
ODB ODB may refer to: People * Ol' Dirty Bastard (1968–2004), American rapper and founding member of the Wu-Tang Clan * ODB (wrestler) (born 1978), Stage name of American professional wrestler Jessica Kresa * Original David Baker (born 1972), a monik ...
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 family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
* TuxORM: Simple object-relational mapping (ORM) library in Java for JDBC


See also

*
Create, read, update and delete In computer programming, create, read, update, and delete (CRUD) are the four basic operations of persistent storage. CRUD is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information ...
(CRUD) *
Data access layer A data access layer (DAL) in computer software is a layer of a computer program which provides simplified access to data stored in persistent storage of some kind, such as an entity-relational database. This acronym is prevalently used in Microsof ...
*
Service Data Objects Service Data Objects is a technology that allows heterogeneous data to be accessed in a uniform way. The SDO specification was originally developed in 2004 as a joint collaboration between Oracle ( BEA) and IBM and approved by the Java Community ...


References

, url=https://www.oracle.com/java/technologies/data-access-object.html {{Design Patterns Patterns Architectural pattern (computer science) Software design patterns