Author: Jordy Hamwijk
Created: 2024-04-04
Last updated: 2024-05-20
A model library reduces file duplication by centralizing reusable components, resulting in fewer files in the project.
A Data Transfer Object (DTO) is an object used to encapsulate data and send it from one subsystem of an application to another. Here’s why we use DTOs:
- Reducing Data Overhead:
- In N-Tier applications, DTOs are commonly used by the Services layer to transfer data between itself and the UI layer.
- By using DTOs, we reduce the amount of data sent across the wire in distributed applications.
- Modeling in MVC:
- DTOs also serve as great models in the Model-View-Controller (MVC) pattern.
- They help separate concerns and improve code organization.
- Parameter Encapsulation:
- DTOs can encapsulate parameters for method calls.
- Useful when a method takes more than a few parameters.
- Domain Object Conversion:
- DTOs are used with DTO assemblers to convert between Domain Objects and DTOs.
- Conversion between Domain Object and DTO can be costly, but it’s essential for distributed applications.
This library can be implemented in your project by following the steps below
Go to Spring and create a new Spring Boot project.
Add the following dependencies:
This Spring Framework dependency will provide us with all the necessary functionality to create and manage our REST endpoints.
Because it is just that easy to use. Want to know more about Project Lombok? Click this link
This Spring Validation dependency will provide us with all the necessary functionality to validate our DTOs.
This Spring Hateoas dependency will provide us with all the necessary functionality to create hypermedia-driven REST endpoints.
This Spring Swagger dependency will provide us with all the necessary functionality to create API documentation.
Let's create a DTO class that will represent the data that we want to expose to the client.
ModelDTO.java
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ModelDTO {
@Null
private String url;
@Null
private String uuid;
@Size(max = 40)
private String name;
@Size(max = 15)
private String code;
}
@Getter
is a Lombok annotation that generates a getter method for each field in the class.
@Setter
is a Lombok annotation that generates a setter method for each field in the class.
@Builder
is a Lombok annotation that produces complex builder APIs for the class it is applied to unit tests.
@NoArgsConstructor
is a Lombok annotation that generates a no-argument constructor.
@AllArgsConstructor
is a Lombok annotation that generates a constructor with one parameter for each field in the class.
@Null
is a Spring Validation annotation that validates that the annotated element must be null.
@Size
is a Spring Validation annotation that validates that the annotated element must be within the specified size.
To implement the model library in your project, follow these steps:
Step 1 - Go to your Pom.xml
of the main project and add the following dependency:
<dependency>
<groupId>dev.jda</groupId>
<artifactId>demo-model-libary</artifactId>
<version><your-version>-SNAPSHOT</version>
</dependency>
Step 2 - Remove SNAPSHOT
from the version when you are done developing " remove snapshot from the version",
<dependency>
<groupId>dev.jda</groupId>
<artifactId>demo-model-libary</artifactId>
<version><your-version></version>
</dependency>
Step 3 - Before using the library, you must reload and install your Maven dependencies.
mvn clean install
Feedback is always welcome. If you have any questions, comments, or suggestions, please do not hesitate to reach out.
-
Star the repository to show your support.
-
Follow me for more interesting repositories!