DTOを使用して、プレゼンテーション層との間でデータを送受信します。次のようなレイヤーがあります。
- ファサード
- appService
- ドメイン
また、エンティティをdtoに変換するためにDozerを使用しています。しかし、私は今2つの質問があります:
- エンティティからdtoまではドーザーを使用できますが、dtoからエンティティまではドーザーを使用できますか?はいの場合、どのように?
- どこでエンティティを作成する必要がありますか?ファサードまたはDTOAssemblerで?
たとえば、本を登録する必要があります。本のエンティティは次のようになります。
Book{
public Book(BookNumber number,String name){
//make sure every book has a business number,
//and the number can't change once the book is created.
this.bookNumber = number;
..
}
}
そして私たちはDTOAssemblerを持っています:
BookDTOAssembler{
BookDTO toDAO(bookEntity){
...
}
BookEntiy fromDTO(book DTO,BookRepository bookRepository){
//1.Where should i create book entity?
//2.Is there any effective way to convert dto to entity in java world?
}
}
オプション1
the BookManagedFacade has a registerBook function:
public registerBook(bookDTO){
Book book = BookDTOAssembler.fromDTO(book DTO);
}
//Create book in BookDTOAssembler.fromDTO
public static BookEntiy fromDTO(BookDTO bookDTO,BookRepository bookRepository){
//book is never registered
if (0==bookDTO.getBookID()){
Book book = new Book(bookRepository.generateNextBookNumber(),bookDTO.getName());
}else{
//book is been registed so we get it from Repository
book = bookRepository.findById(bookDTO.getBookID());
}
book.setAuthor(bookDTO.getAuthor);
...
return book;
}
オプション2
the BookManagedFacade has a registerBook function:
public registerBook(bookDTO){
Book book = new Book(bookRepository.generateNextBookNumber(),bookDTO.getName());
book = BookDTOAssembler.fromDTO(book DTO,book);
}
//add another function in BookDTOAssembler.fromDTO
public static BookEntiy fromDTO(BookDTO bookDTO,Book book){
book.setAuthor(bookDTO.getAuthor);
...
return book;
}
1つで良いですか?またはそれはより良い方法で実装することができます..?