2023.04.17

Jay Ji·2023년 4월 17일
0

I write TIL I close my Eyes

목록 보기
12/28
post-thumbnail

What I learned Today

이번주부터 Spring을 시작하였다. Spring을 이용하여 Entity를 그대로 반환하지 않고 DTO에 담아서 백엔드 서버 작성하는 프로젝트를 진행하였다. 여기서 DTO란 과연 무엇일까? 정말 간략하게 Data Transfer Objects, 즉 데이터를 전송하는 객체인 것은 알고 있었지만 오늘 조금 더 깊게 공부해보기로 했다.

What is DTO?

DTO란 프로세스 사이에서 데이터를 전송하는 객체를 의미하고, DTO 기법을 사용하면 중요한 정보를 노출시키지 않고 두 시스템(API와 서버 등) 간 통신을 원활하게 촉진할 수 있다.

Why do we use DTO?

We create a Data Transfer Object (DTO) that can hold all the data for the call that we receive from the front-end. Usually an assembler is used on the server side to transfer data between DTO and any domain objects.

Although the main reason for using a Data Transfer Object is to batch up what would be multiple remote calls into a single call, it's worth mentioning that another advantage is to encapsulate the serialization mechanism for transferring data over the wire. By encapsulating the serialization like this, the DTOs keep this logic out of the rest of the code and also provide a clear point to change serialization should you wish.

How do we use DTO?

DTO에는 비지니스 로직이 아닌 데이터만 저장해야한다. 또한 용량이 작은 단순한 환경으로, 한가지 작업만 수행하는 것이어야 한다.
효과적인 DTO는 아래와 같다.

  1. 사용구 코드를 최소화 한다.
  2. 작성이 용이해야 한다.
  3. 읽을 수 있어야 한다.
profile
Think out of the Box

0개의 댓글