DTO의 기본 정보

PLC·2024년 9월 5일

정의 및 장점

Data transfer Object
주로 데이터 전송에 사용되는 객체
엔티티는 DB에 저장-관리되는 데이터를 의미, DTO는 비즈니스 로직에서 사용되는 데이터 전송 객체
service와 controller에서 원하는 데이터의 종류가 다른 경우 각 계층에서 필요로하는 것들만 DTO에서 묶어서 각 계층에 전달할 수 있다
1. 서버와의 통신에서 네트워크 오버헤드를 줄여준다
2. encapsulation of the serialization's logic

사용

POJO를 주로 만든다 (Plain Old Java Object- flat data structure that contain no business logic)
주로 domain model에서 DTO로 매핑한다 - mapper component (presentation 또는 facade layer에 있는)를 통해 됨 - DTO와 domain model이 서로를 알 필요가 없다

사용시 일반적 실수

  1. create different DTOs for every occasion
    -> 클래스와 매퍼의 개수를 늘려 유지보수 힘들게 됨
  2. use a single class for many senarios
    -> 너무 많은 연결 (높은 의존성)은 유지보수 힘듦, 객체지향 어긋남
  3. add business logic to those classes
    -> 비지니스 로직은 domain layer에 위치해야함
  4. LocalDTOs, where DTOs pass data across domains
    -> 매퍼들을 위한 비용 높아짐

참고
1. https://cobi-98.tistory.com/61#google_vignette
2. https://www.baeldung.com/java-dto-pattern

profile
jusqu'au dernier silence

0개의 댓글