Introduction about Clean Architecture – Part 2

In the previous tutorial, I introduced to you the basic ideas of Clean Architecture. In this tutorial, I will go into detail about how to implement Clean Architecture with a Java application!

For easy understanding, I will take the student management application example mentioned in part 1 to implement follow the Clean Architecture as follows:

Introduction about Clean Architecture

This is the Maven project with many modules.

Module entities

You can see, we have the entities module to define student information:

Introduction about Clean Architecture

For simplicity, I just define 2 basic information of a student in the Student class as follows:

And in this module’s pom.xml file, I don’t declare any library or framework.

Module use-cases

For the use-cases module, for simplicity, I only define one use case, which is to search for student information by name:

Introduction about Clean Architecture

Here, as you can see, I have defined an additional package as adapter. In the idea of Clean Architecture, the adapter layer will be outside the use-cases layer, but here, we can include this adapter layer in the use-cases module, no need to add an adapter module to define interfaces, not necessarily. But if you want to closely follow the idea of Clean Architecture, you can introduce more adapter module too.

StudentAdapter has the following contents:

The content of the pom.xml file in the use-cases module, I also don’t have a library, or framework at all, just a dependency on the entities module:

Module db

We will implement the student information retrieval in the db module.

Introduction about Clean Architecture

Here, I will use spring-data-jpa to do database manipulation!

As you can see, I also added Hibernate dependency for the implementation part of JPA.

StudentModel has the following content:

StudentRepository has the following content:

As you can see here, I define a query method that allows us to get student information from the student’s name.

And now we can implement StudentAdapter in the db module as follows:

Here, as you can see, I have added a class StudentMapper to convert data from database to entity, and then, if this entity is used somewhere, for example module rest, we will have another Mapper class, to convert from the entity to the dto of the rest to return it to the user.

Using this Mapper class will help us reduce dependencies between modules, we can easily add new or remove modules that we will be using for the application, with minimal code changes.

Module rest

After retrieving data from the database, now is the time to implement the rest module, taking on the role of exposing API for users to use.

Introduction about Clean Architecture

The rest module’s pom.xml file has the following contents:

I use spring-web dependency to define RESTful APIs, use-cases dependency to call application use cases.

StudentDto has the following content:

StudentMapper has the following content:

And the StudentController expose API class gets student information by name with the following content:

Here, I am auto wiring the FindStudentByNameUseCase because I am taking advantage of the benefit of this application with the Spring framework, we will define use cases in the Spring container. If your application uses other frameworks then the use of the use cases will depend on those frameworks.

Module configuration

As I said, in order for the application to run, we need to have the configuration module.

Introduction about Clean Architecture

I am using Spring Boot to run the application:

and define the use case FindStudentByNameUseCase in the UseCaseConfiguration class:

The content of pom.xml of the configuration module will look like this:

If you have noticed, I have defined the most generic rest and db modules possible, and the application configuration in the configuration module will determine how our application runs! For example, here, I am using MySQL to run the application, later if I want to switch to another database system like PostgreSQL, for example, what I need to do is just change this configuration module, …

The file pom.xml of the parent project has the following content:

At this point, we have completed our example application.

Suppose I have a student table and data is created in the MySQL database server as follows:

then when running the application and requesting to http://localhost:9090/student/find?name=Khanh, you will see the results as follows:
Introduction about Clean Architecture

3/5 - (1 vote)

Add Comment