DAO Best practices : Querying Parent / Children
Given you have a domain model with a simple parent / child relationship, and each entity has it's own associated DAO, eg:
Author
AuthorDAO
Book
BookDAO
If I want to add a DAO method for retrieving the books by a specific author, what's the best place for it?
class AuthorDAO
{
List<Book> getBooks(Author autho开发者_如何学编程r);
}
or
class BookDAO
{
List<Book> getBooks(Author author)
}
I'd place a Book
finder on the BookDAO
. This is where I would expect it to be, regardless of its parameters. If you want to find a book, you don't want to scan all DAOs to find where the right finder is. Putting it somewhere else won't help the users of the API.
The BookDAO
, because you want to get information about books. The Book
is the main subject here.
精彩评论