I was going through a document and I came across a term called DAO
. I found out that it is a Data Access Object. Can someone please explain me what this actually is?
I know that it is some kind of an interface for accessing data from different types of sources, in the middle of this little research of mine I bumped into a concept called data source or data source object, and things got messed up in my mind.
I really want to know what a DAO
is programmatically in terms of where it is used. How it is used? Any links to pages that explain this concept from the very basic stuff is also appreciated.
What is DATA ACCESS OBJECT (DAO) - It is a object/interface, which is used to access data from database of data storage. WHY WE USE DAO: To abstract the retrieval of data from a data resource such as a database. The concept is to "separate a data resource's client interface from its data access mechanism."
DAO is a class that usually has the CRUD operations like save, update, delete. DTO is just an object that holds data. It is JavaBean with instance variables and setter and getters. The DTO is used to expose several values in a bean like fashion.
In software, a data access object (DAO) is a pattern that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, the DAO provides some specific data operations without exposing details of the database.
DAO design pattern allows the JUnit test to run faster as it allows to create Mock and avoid connecting to the database to run tests. It improves testing because it's easy to write a test with Mock objects, rather than an Integration test with the database.
The Data Access Object is basically an object or an interface that provides access to an underlying database or any other persistence storage.
That definition from: http://en.wikipedia.org/wiki/Data_access_object
Check also the sequence diagram here: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html
Maybe a simple example can help you understand the concept:
Let's say we have an entity to represent an employee:
public class Employee { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
The employee entities will be persisted into a corresponding Employee
table in a database. A simple DAO interface to handle the database operation required to manipulate an employee entity will be like:
interface EmployeeDAO { List<Employee> findAll(); List<Employee> findById(); List<Employee> findByName(); boolean insertEmployee(Employee employee); boolean updateEmployee(Employee employee); boolean deleteEmployee(Employee employee); }
Next we have to provide a concrete implementation for that interface to deal with SQL server, and another to deal with flat files, etc.
What is DATA ACCESS OBJECT (DAO) -
It is a object/interface, which is used to access data from database of data storage.
WHY WE USE DAO:
To abstract the retrieval of data from a data resource such as a database.
The concept is to "separate a data resource's client interface from its data access mechanism."
The problem with accessing data directly is that the source of the data can change. Consider, for example, that your application is deployed in an environment that accesses an Oracle database. Then it is subsequently deployed to an environment that uses Microsoft SQL Server. If your application uses stored procedures and database-specific code (such as generating a number sequence), how do you handle that in your application? You have two options:
The DAO Pattern consists of the following:
- Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s).
- Data Access Object concrete class -This class implements above interface. This class is responsible to get data from a datasource which can be database / xml or any other storage mechanism.
- Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.
See examples
I hope this has cleared up your understanding of DAO!
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