Abstract
- Data Layer Abstraction separates data access logic from the rest of the application by using an intermediary layer like DO, VO and DAO to interact with databases
Important
Ensures a clear separation between data access and business logic, so schema changes and database switches don’t require significant changes to the business logic.
Centralises security features, such as SQL injection prevention.
DO
id | name | created_at | |
---|---|---|---|
1 | John Doe | [email protected] | 2024-01-01 10:00 |
- Data Object
- Direct mapping to a database table, used in the DAO layer
- We can obtain an instance of DO using Mapper -
UserDO userDO = userMapper.getUserById(1);
DAO Mapper
- Data Access Object Mapper
- A DAO mapper is a component that maps data between different layers, typically between the database and the application’s object model, simplifying interactions with data sources and improving code maintainability
VO
id | name | created_at | |
---|---|---|---|
1 | John Doe | [email protected] | 2024-01-01 10:00 |
- Value Object
- Encapsulates data relevant to the business domain, and used in application logic
- We usually obtain an instance of VO with data from DO -
UserVO userVO = new UserVO(userDO.getName(), userDO.getEmail());
Why no setters?
VO represents immutable business/domain data.
DTO
id | name | created_at | |
---|---|---|---|
1 | John Doe | [email protected] | 2024-01-01 10:00 |
- Data Transfer Object
- Sends formatted data to external systems, often used in APIs
- We usually wrap DO with DAO, so it is formatted and ready to be returned in an API response -
UserDTO userDTO = new UserDTO(userDO);
Why no setters?
DAO transfers formatted, immutable data between layers/systems.