Data Access Object

A Data Access Object (DAO) is an object that provides an abstraction over some type of database or other persistence mechanism. A DAO provides methods for CRUD (common database) operations.

Following the Open/Closed Principle, we use the following design pattern to decouple the persistence mechanism and logic into a separate layer.

The idea is to introduce an abstraction between the database (persistence) and the rest of your application. Let's implement this pattern together.

Add the following interface to your application:

import java.util.List;

/**
 * Data Access Object for Course.
 */
public interface CourseDao {

  /**
   * Create a course.
   *
   * @param offeringName The course alphanumeric code.
   * @param title The course Title.
   * @return The course object created.
   * @throws DaoException A generic exception for CRUD operations.
   */
  Course create(String offeringName, String title) throws DaoException;

  /**
   * Read a course provided its offeringName.
   *
   * @param offeringName The course alphanumeric code.
   * @return The course object read from the data source.
   * @throws DaoException A generic exception for CRUD operations.
   */
  Course read(String offeringName) throws DaoException;

  /**
   * Read all courses from the database.
   *
   * @return All the courses in the data source.
   * @throws DaoException A generic exception for CRUD operations.
   */
  List<Course> readAll() throws DaoException;

  /**
   * Read all courses from the database with title containing titleQuery.
   *
   * @param titleQuery A search term.
   * @return All courses retrieved.
   * @throws DaoException A generic exception for CRUD operations.
   */
  List<Course> readAll(String titleQuery) throws DaoException;

  /**
   * Update the title of a courses provided its offeringName.
   *
   * @param offeringName The course alphanumeric code.
   * @param title The course Title.
   * @return The updated course object.
   * @throws DaoException A generic exception for CRUD operations.
   */
  Course update(String offeringName, String title) throws DaoException;

  /**
   * Delete a courses provided its offeringName.
   *
   * @param offeringName The course alphanumeric code.
   * @return The course object deleted from the data source.
   * @throws DaoException A generic exception for CRUD operations.
   */
  Course delete(String offeringName) throws DaoException;
}

Notice there is a read method and a readAll. The readAll method is overloaded. Moreover, all the operations are throwing the DaoException; add it to your project.

/**
 * A generic exception for CRUD operations.
 */
public class DaoException extends RuntimeException {

  /**
   * Constructs a new DaoException.
   *
   * @param message the detail message (which is saved for later retrieval by the getMessage() method).
   * @param cause the cause (which is saved for later retrieval by the getCause() method).
   */
  public DaoException(String message, Throwable cause) {
    super(message, cause);
  }
}

The DaoException is a custom runtime exception which wraps around the underlying exceptions that might be thrown as the result of implementing CRUD operations. For example, if we use relational databases and SQL in an implementation of CourseDao, the CRUD operations may throw SQLException. We must catch this exception and instead throw DaoException (with an relevant message). We do this to provide a higher level of encapsulation, shielding the client from the implementation details of the interface.