Implementing Repository
Start course

In this course, we will learn the concepts of microservice and spring framework with a focus on Domain-Driven Design.

Learning Objectives

  • Domain-Driven Design

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello there. In this video, we will talk about the implementation of the repository through a little example. So, if you're ready, let's get started. If you remember, we talked about what a repository is in our previous lessons. So, let's go over it. As per Eric Evans book, "Domain-Driven Design," the repository is a mechanism for encapsulating storage, retrieval, and such behavior which emulates a collection of objects. Likewise, according to "Patterns of Enterprise Application Architecture," Martin Fowler's book, it mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. In other words, a repository also deals with data and hides queries similar to a Data Access Object or DAO. However, it sits at a higher level closer to the business logic of an app. Consequently, a repository can use a DAO to fetch data from the database and populate a domain object or it can prepare the data from a domain object and send it to a storage system using a DAO for persistence.

Now, let's examine the DAO and then the repository using a single example. Imagine we have a simple domain class named user. This class will have an integer type id  and a string type userName. You can also add different fields, for example, address, email, phone number, etc. We also create getters and setters in this class. Then we create the UserDAO interface that provides simple CRUD;  create, read, update, delete operations for the user domain like this. Lastly, we create the UserDAOImplementation class that implements the UserDAO interface, like this. Now, let's implement the repository. First, we create an interface called UserRepository as shown in the slide. Here, we've added a few common methods like get, add, update, and remove to work with the collection of objects.

Then we create the UserRepositoryImplementation class which provides an implementation of the UserRepository interface like this. Here, we use the UserDAOImplementation to send and retrieve data from the database. So far, we can say that the implementations of DAO and repository look very similar because the user class is an anemic domain and a repository is just another layer over the data access layer or DAO. As a result, DAO seems a perfect candidate to access the data and a repository is an ideal way to implement a business use case. So, that's how the repository is used. Let's take a short break here and I'll see you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.