DAO | Abstraction of the Application/Business layer from the persistence layer

Reading Time: 3 minutes

DAO…. In this blog we’ll be learning about data access objects , it’s pros and cons and implementation in Scala Language.

What is DAO …. ?

It’s an abbreviation of Data Access Object. It is a structural pattern that provides an abstract API to isolate the application/business layer from the persistence layer . This Layer could be a database or any other persistence mechanism

The main purpose of this API is to conceal all the complexities from the application layer. These complexities involved in performing the CRUD operations in the underlying storage mechanism. This allows both the layers to evolve separately without being known to each other. Let’s discuss it’s pros and cons before implementing it.

Pros of using DAO –

  • While changing a persistence mechanism, service layer doesn’t even have to know from where the data comes from.
  • Once you have the general CRUD flow set, the general layout can be repeated for other DAOs.
  • As we work with traits in DAO pattern, it also emphasises the style of “work with traits instead of implementation” . It is an excellent OOP’s style of programming.

Cons of DAO –

  • It is not the flexible thing as it seems like.
  • The code become tedious and repetitive while implementing the it.

Let us quickly create a logical diagram before implementing the DAO Pattern.

Implementing the Data Access Object Pattern using Scala Language

With the above Logical diagram, let’s try to implement the DAO pattern using the following components.

1. Book Model-

The Book model is a model which will be transferred from one layer to the other layer.

2. BookDao –

The BookDao is a trait that will provide a flexible design and API to be implemented.

3. BookRepository –

It will be a concrete class an implementation of the “BookDao” trait.

Creating the Book Model


Creating the BookDao Trait


Implementing the DAO in BookRepository


Creating Services to perform the CRUD Operations


Main class for User Interface


Conclusion –

In this blog we have learnt about DAO, it’s prose and cons and logical diagram. We have also this implemented using SCALA Language.

References –


Written by 

Kuldeepak Gupta is a passionate software consultant at Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. His current passions include utilizing the power of Scala, Akka, and Play to make Reactive and Big Data systems. He is a self-motivated, enthusiastic person who is recognized as a good team player, dedicated, responsible professional, and a technology enthusiast. His hobbies include playing hockey, participating in Political debates, Reading Tech blogs, and listening to songs.