Let's assume I have the following classes:
@Entity
public class Book {
...
}
@Repository
public class BookRepositoryImpl implements BookRepository {
...
}
@Service
public class BookServiceImpl implements BookService {
...
}
@Entity
public class Author{
...
}
@Repository
public class AuthorRepositoryImpl implements AuthorRepository {
...
}
@Service
public class AuthorServiceImpl implements AuthorService {
...
}
Now i want to save a book with an author. To my BookService i pass somehow BookDto with all information except author, and as second parameter i pass author Id. Now to save my book i have to assemble Book entity. Which layer is the correct one to create it? i see few possibilities but im not sure which approach is the correct one:
- in BookingService.createBook i can call AuthorService.findAuthorById and with this assemble Book, then pass it to BookRepository
- in BookingService.createBook i can call AuthorRepository.findAuthorById and with this assemble Book, then pass it to BookRepository
- i can pass all informations to repository and let it handle entity, creation by this i mean: in BookingService.createBook call BookRepository.saveBookWithAuthor(Book book, Long authorId) and inside this method I can call AuthorRepository.findAuthorById or i can call AuthorService.findAuthorById, assemble Book entity and persist it
Which approach is the correct one and why? It would seem to me that a logical solution is the first one, however i don't like the idea of mixing entity assemble with some business logic that might be inside of my services. Is there a pattern to separate business logic from technical solutions?