In this article, we will discuss a few best practices that we need to follow while developing the Persistence/DAO layer.
Presentation layer: This is the user interface of the application that presents the application’s features and data to the user.
Business logic (or Application) layer: This layer contains the business logic that drives the application’s core functionalities. Like making decisions, calculations, evaluations, and processing the data passing between the other two layers.
Data access layer (or Data) layer: This layer is responsible for interacting with databases to save and restore application data.
For example:
USER -> UserDao.java
CUSTOMER -> CustomerDao.java
ROLES -> RolesDao.java
ACCOUNT_DETAILS -> AccountDetailsDao.java
USER -> UserDao.java
UserDaoImpl.java
CUSTOMER -> CustomerDao.java
CustomerDaoImpl.java
ROLES -> RolesDao.java
RolesDaoImpl.java
ACCOUNT_DETAILS -> AccountDetailsDao.java
AccountDetailsDaoImpl.java
3. At least one primary key per table
4. Auditing columns:
-> CREATED_DATE
-> CREATED_BY
-> UPDATED_DATE
-> UPDATE_BY
The Data Access Object (DAO) pattern is a structural pattern that allows us to isolate the application/business layer from the persistence layer (usually a relational database, but it could be any other persistence mechanism) using an abstract API.
Check out DAO pattern at https://www.javaguides.net/2018/08/data-access-object-pattern-in-java.html
Persistence Operations (CRUD Operations)
These are the commonly used database table operations for every table so we can make these operations generic in the DAO layer.- Inserting data
- Updating data
- Deleting data
- Retrieving data
Different Options to Develop Persistence or DAO Layer
In the Java community, we have different options to develop Persistence/DAO layer- Java JDBC
- Spring JDBC
- ORM Frameworks ( Hibernate, MyBatis etc)
- Spring Data ( Spring Data JPA, Spring Data MongoDB, Spring Data REST, Spring Data Elasticsearch etc)
Spring MVC Three Layer Architecture
The above diagram shows a 3 layer architecture in any typical Spring MVC web applications.Presentation layer: This is the user interface of the application that presents the application’s features and data to the user.
Business logic (or Application) layer: This layer contains the business logic that drives the application’s core functionalities. Like making decisions, calculations, evaluations, and processing the data passing between the other two layers.
Data access layer (or Data) layer: This layer is responsible for interacting with databases to save and restore application data.
Best Practice to Develop Persistence or DAO Layer
1. Separate Layer for Persistence logic or database-related code (We should maintain persistent logic as a separate layer ( DAO Layer or Persistence Layer)
2. In persistence logic, we should follow the table per DAO class.
For every table, we will create a dedicated DAO class and this DAO class is responsible to perform operations with That table ( which improves readability)
For example:
USER -> UserDao.java
CUSTOMER -> CustomerDao.java
ROLES -> RolesDao.java
ACCOUNT_DETAILS -> AccountDetailsDao.java
USER -> UserDao.java
UserDaoImpl.java
CUSTOMER -> CustomerDao.java
CustomerDaoImpl.java
ROLES -> RolesDao.java
RolesDaoImpl.java
ACCOUNT_DETAILS -> AccountDetailsDao.java
AccountDetailsDaoImpl.java
3. At least one primary key per table
4. Auditing columns:
-> CREATED_DATE
-> CREATED_BY
-> UPDATED_DATE
-> UPDATE_BY
YouTube Video - Best Practice to Develop Persistence or DAO Layer
These best practices are explained with an example in below youtube video:
Comments
Post a Comment
Leave Comment