开发者

DAO Best practices : Querying Parent / Children

开发者 https://www.devze.com 2022-12-12 12:21 出处:网络
Given you have a domain model with a simple parent / child relationship, and each entity has it\'s own associated DAO, eg:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消