Skip to content

Data Access Object

According to Wikipedia

Quote

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.

The same Wikipedia article says:

Quote

The primary advantage of using data access objects is the relatively simple and rigorous separation between two important parts of an application that can but should not know anything of each other, and which can be expected to evolve frequently and independently. Changing business logic can rely on the same DAO interface, while changes to persistence logic do not affect DAO clients as long as the interface remains correctly implemented.=

Let's get more practice with this pattern. Create a new Gradle Java project in IntelliJ. Bring your Course class over and place it in a model package.

Now, let's make a simple DAO interface for Course (add this in a new package dao):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package dao;

import exception.DaoException;
import model.Course;

import java.util.List;

public interface CourseDao {
  void add(Course course) throws DaoException;
  List<Course> findAll();
}

Note

  • The DaoException is a Runtime Exception; implement it and place it in exception package.

  • We have limited the operations of CourseDao to add and findAll for simplicity.