• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

Editor 에 대한 질문..

22.09.06 15:21 작성 조회수 304

1

안녕하세요. 강의를 듣다가 Editor 클래스에 대해 궁금한점이 생겨서 질문을 남기게 되었습니다.

  1. Editor 클래스를 사용하면 수정 시 content 나 title 값이 빈 값 또는 NULL 값이 들어오는 것을 방지하기 위한 목적도 있다하셨는데, 해당부분은 요청받는 컨트롤러에서 @Valid 로 검증할 수 있다고 생각되는데, 어떻게 생각하시는지 궁금합니다.

  2. Editor 클래스를 새로 생성하게 되면 Post 엔티티 클래스가 Editor 클래스에 의존하게 되는게 아닌가 생각이 드네요. 또 궁금한것이 Editor 클래스를 혹시 Post 엔티티 클래스 내부에서 선언해서 사용하는 것과 호돌맨님이 구현하신 것 처럼 외부에 클래스를 생성해서 사용하는 것과 다른부분이있을까요? 내부클래스로 사용한다면 확실하게 의도를 알 수 있을거 같아서 질문드립니다!

     

     

    1.  

      package com.blog.api.domain; 
      import lombok.AccessLevel; 
      import lombok.Builder; 
      import lombok.Getter; 
      import lombok.NoArgsConstructor;
       import javax.persistence.*; 
      import javax.validation.constraints.NotBlank; 
      
      @Entity
      @Getter 
      @NoArgsConstructor(access = AccessLevel.PROTECTED) 
      public class Post { 
      
      @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id; 
      private String title; 
      @Lob // DB에는 TEXT 타입으로 생성. 
      private String content; 
      
      @Builder 
      public Post(String title, String content) {
      this.title = title; 
      this.content = content; 
      } 
      
      public void edit(PostEditor postEditor) {
      this.title = postEditor.getTitle();
      this.content = postEditor.getContent();
      } 
      
      public PostEditor.PostEditorBuilder toEditor() { 
      return PostEditor.builder() 
      .title(this.title) 
      .content(this.content); 
      } 
      
      @Getter 
      public static class PostEditor { 
      
      private String title; 
      private String content; 
      
      @Builder 
      public PostEditor(String title, String content) { 
      this.title = title; this.content = content; 
        } 
       } 
      }

답변 1

답변을 작성해보세요.

2

안녕하세요. 호돌맨입니다.
질문을 남겨주셔서 감사합니다.

1. Editor

만약 클라이언트에서 변경된 값만 전송한다고 하면 어떨까요? 예를들어 title이 수정된 경우 content를 제외한 title만 전송한다고 하는겁니다.

2. 의존

Post가 Editor에 의존하게 되어도 괜찮다고 생각합니다. Post와 Editor는 같은 레이어 com.hodolog.api.domain 에 위치하고 있기 때문입니다. 데이터가 한 방향으로 흐른다고 상관없다고 생각합니다. (controller->service->editor->entity)

감사합니다.