프로그래밍/Nest.js

Nest.JS DTO,DAO and Pojo

Terry Cho 2025. 4. 14. 15:30

Nest.js에서 사용하는 DTO의 개념이 Spring DAO와 헷갈렸는데.

 

DTO는 단순히 Value Object라고 보면 된다. 

Data Transfer Object로 데이터를 저장하는  복합체 구조이다. 

DAO는 Data Access Object로, ValueObject의 내용을 DB에 저장하는 로직을 가지고 있다. 

 

참고 : 아래는 Java 코드 예제임. 

즉 DTO는 데이터에 대해서 아래와 같이  get/set 만을 가지고 있고, 

public class UserDto {

    private int id;

    // @NotEmpty // Example validation: name cannot be empty
    // @Size(min = 2, max = 50) // Example validation: name length between 2 and 50
    private String name;

    // @NotEmpty
    // @Email // Example validation: must be a valid email format
    private String email;

    // Constructors
    public UserDto() {
    }

    public UserDto(int id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

// 중략
}

 

DAO는 findById, List findAll()과 같이 데이터에 대한 직접적인 접근 기능을 가지고 있다. 

interface UserDao {
    Optional<UserDto> findById(int id); // Find user by ID (Optional handles null)
    List<UserDto> findAll();            // Find all users
    UserDto save(UserDto user);         // Save or update a user
    void deleteById(int id);            // Delete a user by ID
}

'프로그래밍 > Nest.js' 카테고리의 다른 글

Nest.JS Controller, Module, Service 의 관계  (0) 2025.04.14