DTO, VO, Entity

stoph·2022년 9월 4일
0

비슷하면서도 다른 DTO, VO, Entity의 차이에 대해서 알아보자.

DTO

@Getter
@Setter
public class MonitorDto {
    
    private String name;
    private Integer price;
    private Integer count;

    public MonitorDto(String name, Integer price, Integer count) {
        this.name = name;
        this.price = price;
        this.count = count;
    }
}
  • Data Transfer Object의 약자로, 계층 간에 데이터를 전달하기 위한 객체이다.
  • 데이터 접근 메서드와 데이터 표현을 위한 메서드 이외의 비즈니스 로직은 포함 할 수 없다.
  • 값을 수정 및 변경할 수 있는 가변 객체이다.

VO

@Getter
public class Monitor {

    private final String name;
    private final Integer price;
    private final Integer count;

    public Monitor(String name, Integer price, Integer count) {
        this.name = name;
        this.price = price;
        this.count = count;
    }

    public Integer getTotal() {
        return price * count;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Monitor monitor = (Monitor) o;
        return Objects.equals(name, monitor.name) && Objects.equals(price, monitor.price) && Objects.equals(count, monitor.count);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, price, count);
    }
}
  • Value Object의 약자로, 다양한 속성 값을 가지는 객체이다.
  • 비즈니스 로직을 포함할 수 있다.
  • 값을 수정 및 변경할 수 없는 불변 객체이다.
  • 모든 속성 값이 같으면 동일한 인스턴스이다.
    equals()와 hashCode() 오버라이딩 필수

Entity

@Entity
public class Monitor {

    @Id @GeneratedValue
    private Long id;
    @Column
    private String name;
    @Column
    private Integer price;
    @Column
    private Integer count;

    public Monitor() {
    }

    public Monitor(String name, Integer price, Integer count) {
        this.name = name;
        this.price = price;
        this.count = count;
    }

    public Integer getTotal() {
        return price * count;
    }
}
  • 실제 DB의 테이블과 매핑되는 객체이다.
  • ID 값을 가지고 있으며 ID 값으로 각 객체가 구분된다.
  • 비즈니스 로직을 포함할 수 있다.
  • 값을 수정 및 변경할 수 있는 가변 객체이다.
  • 기본 생성자를 필수로 생성해주어야 한다.

참고

우아한Tech - 10분 테코톡 (1)
우아한Tech - 10분 테코톡 (2)

0개의 댓글