Learn, Build, Deploy

Building a RESTful CRUD API using Spring Boot – Part 3

Table of Contents

Creating the Model Object

What are Model Objects?

Model Objects (a.k.a Data Transfer Objects (DTO’s)) are objects that are specific to your application, which can be manipulated by business logic. We use DTO’s as objects that can passed around easily, and have business logic associated with them.

Wait… DTO’s sound a lot like DAO’s… What’s the difference? πŸ€”

DTO’s are used to transfer your data between classes and modules of your application. As the DTO passes between various layers, the data inside the object may change based on the business logic applied to it.

A Data Access Object (DAO) is responsible for interacting with the database and doing nothing else. Examples include the Reservation Entity and the Reservation Repository, which we can consider to be a part of the DAO layer.

As the DAO is used to interact with the database, we should first convert our DTO object into a DAO. This is because DAO objects should be the only things that interact with the database. By creating the separation between the DTO and DAO, we really starting to separate our concerns!
Your DAO may contain additional metadata about the record that needs to be stored e.g. the createdDate, updatedAt, lastModified fields of the record. Even if our DTO’s do not need any knowledge of this metadata, other teams such as Data Analysis teams may find this extra metadata helpful.

Implementing the Reservation Model object

First, create a new package called model, then a class called Reservation.

package com.rms.reservationservice.model;

import lombok.Builder;
import lombok.Data;
import java.time.LocalDateTime;

@Data
@Builder(toBuilder = true)
public class Reservation {

    private String id;

    private String firstName;
    private String lastName;
    private LocalDateTime reservationTime;
    private int numberOfGuests;
    private int duration;
}

@Data – Provides toString, equals and hashCode, getters and setters methods for our class. (see more here)

@Builder – Utilises the Builder design pattern to create immutable objects. (see more here)

toBuilder = true – Creates a new builder that starts out with all the values of this instance so that you can insert, override or remove elements without modifying the original object, maintaining object immutability. πŸ™Œ

Remember, the more immutable we make our objects and functions, the more deterministic our service will become! πŸ˜‰

If you have had any trouble with this, fork the project and checkout branch part-3-creating-the-reservation-model and compare your code against mine.

You made to the end of Part 3! Great Job!

πŸŽ‰ πŸŽ‰πŸŽ‰

We are now half way to the RMS being built, you should be pretty freakin’ proud of yourself! As we have the Reservation DTO created, we can now move onto creating the service layer which is where all the business magic happens πŸ˜‰. Checkout Part 4: Creating the Reservation Service Layer to see how we can start including some business logic within our application!