Tuesday, November 14, 2017

Data access object design pattern



This is another very important design pattern. Before you learn about this design pattern, it is better to understand about following points. 

What is DAL (Data Access Layer) ?

  • Simply data access layer is used to handle database access in applications.
  • In Layered Architecture, it is a best practice to use a separate layer for data access.
  • Data access layer is being used to achieve this.
  • DAO design pattern and Repository design pattern can be used to implement that Data access layer.


What is DAO (Data Access Object) ?
  • Usually this is used to connect both service layer and database layer as I mentioned above.
  • The main purpose of using DAO is to hide implementation data with the usage of interfaces.
  • Normally we define DAOs for each and every entity. Eg: UserDao.java
  • DAOs return data as object state.

What is DTO (Data Transfer Object) ?
  • Simply this can be a JSON object that can be used to get request data and send response data. 
  • If you design APIs, JSON objects are being used as request and response objects. Eg: UserRegRequestJson.java, UserRegResponseJson.java

Look at the following UML diagram to understand about the way of using DAO design pattern. 




Advantages of using DAO 

  • Hide implementation data.
  • Reduce code complexity.
  • Centralized data access to separate layer called Data Access Layer.

Disadvantages of DAO

  • This is not the smartest way to achieve those advantages, if you are using a framework like Spring with Spring Data JPA, it can simplify everything than using DAO.