설명: Entity 클래스에 DTO를 인자로 받는 생성자를 정의하여, DTO의 데이터를 기반으로 Entity 객체를 생성한다. Entity 클래스가 DTO의 필드에 의존성을 가지게 되어 구현이 간결해진다.
이러한 구조에서 Entity는 DTO에 과하게 의존하게 된다. Entity는 해당 DTO 없이는 생성될 수 없을 뿐만 아니라, DTO의 변경 사항들에 직접적으로 영향을 받게된다. Entity의 변경은 최소화되어야 하며, DTO는 비교적 자유롭게 Data를 변경할 수 있다. 현재 구조에서는 일관성과 안전성을 보장받아야 하는 Entity가 일관성 없는 DTO에 의존하여 생성되고 있다.
예시 코드:
DTO (PostDto):
public class PostDto {
private String title;
private String content;
// getters, setters, constructors
}
Entity (Post):
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
// DTO를 매개변수로 받는 생성자
public Post(PostDto dto) {
this.title = dto.getTitle();
this.content = dto.getContent();
}
// getters, setters
}
Service (PostService):
public class PostService {
public Post createPost(PostDto dto) {
Post post = new Post(dto); // DTO 주입을 통한 Entity 생성
// repository.save(post) 호출 등 추가 로직
return post;
}
}
설명: Entity 클래스에서 DTO 객체 대신 DTO의 데이터를 개별 인자로 받는 생성자를 정의한다. Entity가 DTO와의 의존성이 없으며, DTO 데이터만 필요한 경우 간결하게 사용할 수 있다.
하지만 추가적인 리소스를 필요로 한다는 단점이 있다.Service 계층에서 어떤 비즈니스 로직을 구현하기 위해 Data를 수정해야하는 경우라면 문제가 없지만, DTO의 Data를 그대로 집어넣어도 괜찮은 경우 일일이 DTO의 Data를 새로운 변수에 저장해주어야 한다.
예시 코드:
DTO (PostDto):
public class PostDto {
private String title;
private String content;
// getters, setters, constructors
}
Entity (Post):
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
// DTO의 개별 데이터로 Entity 생성
public Post(String title, String content) {
this.title = title;
this.content = content;
}
// getters, setters
}
Service (PostService):
public class PostService {
public Post createPost(PostDto dto) {
Post post = new Post(dto.getTitle(), dto.getContent()); // DTO 데이터로 Entity 생성
// repository.save(post) 호출 등 추가 로직
return post;
}
}
설명: DTO 클래스에 Entity 객체를 생성하는 메서드를 추가하여 Entity 생성 로직을 DTO 내부에서 처리하도록 한다. Service 계층에서 DTO 객체의 메서드를 호출해 Entity를 생성하며, 변환 로직이 DTO에 있어 Service 코드가 간결해진다.
Entity는 DTO에 의존 관계가 성립되지 않으며, Entity는 생성 이후 DTO의 변경 사항들에 전혀 영향을 받지 않는다. 또한 Entity의 생성자에 DTO Data를 주입하는 방식의 단점 또한 간단하게 해결 가능하다.
예시 코드:
DTO (PostDto):
public class PostDto {
private String title;
private String content;
// Entity를 생성하는 메서드
public Post toEntity() {
return new Post(this.title, this.content);
}
// getters, setters, constructors
}
Entity (Post):
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
public Post(String title, String content) {
this.title = title;
this.content = content;
}
// getters, setters
}
Service (PostService):
public class PostService {
public Post createPost(PostDto dto) {
Post post = dto.toEntity(); // DTO에서 Entity 생성
// repository.save(post) 호출 등 추가 로직
return post;
}
}