'Where to implement method which includes saving Embedded data? Repository or Service?
I have collection like these:
class Book
{
@Id String id;
Publisher embeddedPublisher;
...
}
To create Book initially, I need to access on Publisher collection. For doing this, I think I have two choices:
Method 1. Implement CustomRepository method like using find or lookup
class BookCustomRepositoryImpl extends ... {
MongoTemplate template;
Book createBookWithPublisher(String bookName, String publisherName) {
// With MongoTemplate, find Publisher by publisherName
// With MongoTemplate, save Book with retrieved Publisher data
}
}
Method 2. Implement logic on @Service
@Service
class BookService {
BookRepository bookRepo;
PublisherRepository publisherRepo;
BookDto createBookWithPublisher(String bookName, String publisherName) {
// With PublisherRepo, find Publisher by publisherName
// With BookRepo, save Book with retrieved Publisher data
}
}
What would be better practice regarding logic separation? Share your opinions!
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
