• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

정적 팩토리 메소드로 변경감지

21.11.18 11:38 작성 조회수 140

0

@Controller


@PostMapping("/items/{itemId}/edit")
public String update(@PathVariable("itemId")Long itemId, @ModelAttribute("form") BookForm form){

itemService.updateItem(itemId, form.getName(), form.getPrice(), form.getStockQuantity(), form.getAuthor(), form.getIsbn());

return "redirect:/items";
}

 

 

@Service

/**
* 다운캐스팅 없이 따로 findBook 메소드를 만들어줌
*/
@Transactional
public void updateItem(Long id, String name, int price, int stockQuantity, String author, String isbn) {

Book findBook = itemRepository.findBook(id);
findBook.updateBook(name, price, stockQuantity, author, isbn);
log.info("update실행");
}

 

 

Book엔티티

@Entity
@DiscriminatorValue("B")
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public class Book extends Item {

private String author;

private String isbn;


private Book(String name, int price, int stockQuantity, String author, String isbn) {
super(name, price, stockQuantity);
this.author = author;
this.isbn = isbn;
}


/**
* 정적 팩토리 메소드 생성
*/
public static Book createItem(String name, int price, int stockQuantity, String author, String isbn) {
Book book = new Book(name, price, stockQuantity, author, isbn);

return book;
}


/**
* update 변경 로직
*/
public void updateBook(String name, int price, int stockQuantity, String author, String isbn) {
addItem(name, price, stockQuantity);
this.author = author;
this.isbn = isbn;

}

}

 

 

Item엔티티


/**
* update 변경 로직
*/
protected void addItem(String name, int price, int stockQuantity) {
this.name = name;
this.price = price;
this.stockQuantity = stockQuantity;
}

 

안녕하세요! 2회독하면서 영한센세가 말씀하신대로 setter는 최대한 쓰지않고 개발하고있습니다.

 

따로 DTO는 만들지않고 정적 팩토리 메소드를 활용해서 변경감지 로직을 짜봤는데 제대로 짠건지 잘 모르겠네요. 여기서 더 수정 하자면 어떤부분을 고치면 좋을까요??

답변 1

답변을 작성해보세요.

0

안녕하세요. 우와아앙님^^

특별히 수정할 점이 보이지는 않습니다.

추가로 죄송하지만 앞으로는 질문 안내에 있는 것 처럼 학습에 관련된 질문을 올려주시길 부탁드립니다.

저도 마음으로는 도움을 드리고 싶지만, 하루에도 수 많은 분들이 질문을 올려주십니다. 그래서 학습과 관련된 질문에 초점을 맞추는 것이 맞다 생각합니다. 다시한번 이해를 부탁드립니다.