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.
DAO (Data Access Object) is a very used design pattern in enterprise applications. It basically is the module that is used to access data from every source (DBMS, XML and so on). I suggest you to read some examples, like this one:
DAO Example
Please note that there are different ways to implements the original DAO Pattern, and there are many frameworks that can simplify your work. For example, the ORM (Object Relational Mapping) frameworks like iBatis or Hibernate, are used to map the result of SQL queries to java objects.
Hope it helps, Bye!
Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services. Following are the participants in Data Access Object Pattern.
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.
Sample code here..
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:
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:Next we have to provide a concrete implementation for that interface to deal with SQL server, and another to deal with flat files, etc.
It is a object/interface, which is used to access data from database of data storage.
it abstracts 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:
Its in all referred as DAO Pattern, It consist of following:
Example
I assume this things must have cleared your understanding of DAO up to certain extend.
I am going to be general and not specific to Java as DAO and ORM are used in all languages.
To understand DAO you first need to understand ORM (Object Rational Mapping). This means that if you have a table called "patient" with columns "name" and "age", then you would create object-template for for that table:
Now with help of DAO instead of writing some specific queries, to fetch all persons, for what ever type of db you are using (which can be error-prone) instead you do:
You do not write the DAO abstraction yourself, instead it is usually part of some opensource project, depending on what language and framework you are using.
Now to the main question here. "..where it is used..". Well usually if you are writing complex business and domain specific code your life will be very difficult without DAO. Of course you do not need to use ORM and DAO provided, instead you can write your own abstraction and native queries. I have done that in the past and almost always regretted it later.
I think the best example (along with explanations) you can find on the oracle website : here. Another good tuturial could be found here.