ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring Boot - DTO & VO & Entity
    Framework & Library/Spring Boot 2023. 2. 4. 18:03

    DTO & VO & Entity

    DTO(Data Transger Object)

     

    ㆍ DTO는 Data Transfer Object의 약자로, 데이터를 전달하기 위한 객체이다.

    ㆍ 여러 레이어 간 데이터를 주고받을 때 사용할 수 있는데, 주로 View와 Controller 사이에서 활용된다.

    ㆍ DTO는 getter(), setter()  메서드를 포함한다. 하지만, 이외의 다른 비즈니스 로직은 포함하지 않는다.

    ㆍ DTO는 어떻게 구현하느냐에 따라 가변 객체로 활용할 수도 있고, 불변 객체로 활용할 수도 있다.

     

    가변 객체로써의 DTO

    public class PersonDto {
        private String name;
        private int age;
        
        public String getName() {
            return name;
        }
        
        public int getAge() {
            return age;
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public void setAge(int age) {
            this.age = age;
        }
    }

    가변 객체로써 DTO 클래스를 만들 때는 setter() 메서드를 이용하여 객체를 생성할 수 있다.

     

    불변 객체로써의 DTO

    public class PersonDto {
        private String name;
        private int age;
        
        public PersonDto(String name, int age) {
            this.name = name;
            this.age = age;
        }
        
        public String getName() {
            return name;
        }
        
        public int getAge() {
            return age;
        }
    }

    불변 객체로써 DTO 클래스를 만들 때는 생성자를 이용해서 객체를 생성하며, getter() 메서드만 구현한다. 이렇게 구현함으로써 데이터를 전달하는 과정에서 데이터의 불변성을 보장할 수 있다.

     

    VO(Value Object)

    ㆍ VO는 Value Object의 약자이며, DTO와 비슷한 개념이지만 값 자체를 표현하는 객체이다.

    ㆍ VO는 getter() 메서드를 포함해서, 이외의 비즈니스 로직을 포함할 수 있다. 하지만, 오로지 읽는 용도로만 사용하기 때문에 setter() 메서드는 구현하지 않는다.

     

    VO 클래스의 예제

    public class Money {
        private int value;
        
        public Money(int value) {
            this.value = value;
        }
        
        public String getValue() {
            return value;
        }
        
        public String printValue() {
            return this.value + "원";
        }
    
    }

    일반적인 VO 클래스는 getter() 메서드 이외의 비즈니스 로직을 포함할 수 있다. 

     

    VO 클래스의 hashCode(), equals() 메서드

    class Test {
        
        @Test
        public void sameTest() {
            Money money01 = new Money(1000);
            Money money02 = new Money(1000);
            
            Assertions.assertEquals(money01, money02);   // money01과 money02는 서로 다른 객체
        }
    }

    위 코드의 money01과 money02는 서로 다른 객체인 결과가 나온다.

     

    public class Money {
        private int value;
        
        public Money(int value) {
            this.value = value;
        }
        
        public String getValue() {
            return value;
        }
        
        public String printValue() {
            return this.value + "원";
        }
        
        @Override
        public int hashCode() {
            return Objects.hash(value);
        }
        
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || this.getClass() != obj.getClass()) {
                return false
            }
            
            Money money = (Money) obj;
            
            if (this.value == money.value) {
                return true;
            } else {
                return false;
            }
        }
        
    
    }

    VO 클래스는 객체의 주소가 다르더라도 속성 값이 같은 경우 동일한 객체로 판단한다. 따라서, VO 클래스 내에 hashCode()와 equals() 메서드를 오버라이딩 해줘야 한다.

     

    Entity

    ㆍ Entity는 실제 DB 테이블과 매핑이 되는 클래스이다. 이에 따라, Entity를 기준으로 테이블이 형성되고, 칼럼이 변경된다.

    ㆍ Entity는 DB 테이블과 매핑이 되는 클래스이기 때문에, 데이터를 전달하는 용도로 사용해서는 안된다.

     

    Entity 클래스의 예제

    public class Person {
        private final Long id;
        
        private final String name;
        
        private final int age;
        
        public Person(Long id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
    }

    Entity 클래스는 setter() 메서드를 포함하여 비즈니스 로직을 구현할 수 있다. 하지만, DB 테이블과 매핑되는 클래스이기 때문에, 클래스의 속성값을 변경시키는 메서드는 가급적 구현해서는 안된다.

     

    정리

     

      DTO VO Entity
    용도 레이어 간 데이터를 전송하기 위한 객체 값을 표현하기 위한 객체 DB 테이블과 매핑되는 객체
    상태 변경 여부 가변 혹은 불변 불변 불변
    로직 포함 여부 gettter(), setter() 메서드만 포함 가능 비즈니스 로직 포함 가능 비즈니스 로직 포함 가능

    출처

    https://today-retrospect.tistory.com/142

     

    728x90

    댓글

Designed by Tistory.