关于 DAO 的正确使用
DAO 是否只保留用于基本的 CRUD 操作,或者它还可以包含一些复杂的搜索逻辑?
想象一下两个实体类 Book 和 Author 以及它们之间的多对多关系。此时,单个 DAO 就足够了,因为两个实体的基本 CRUD 操作是相同的。
然而,随着应用程序变得越来越复杂,新的要求就会出现,例如: 1. 查找给定作者的第一本书 2.查找某个作者在一段时间内写过的书 3. 查找特定作者所写的特定主题的书籍 4. ....等等
所有这些应该去哪里?我现在是否为每个实体创建一个单独的 DAO 类,并将它们放在那里?它们到底属于 DAO 层吗?
我有这样的困境: 以下方法应该放在哪里: 查找书籍(作者作者) findBooks(出版商出版商) 两者都在 BookDAO 中,还是一个在 AuthorDAO 中,另一个在 PublisherDAO 中?
或者也许我应该考虑一个全新的抽象,它位于 DAO 之上,并将它们留给基本的 CRUD 操作 - 例如 SearchService,它列出了所有可能的搜索?
Is DAO only reserved for the basic CRUD operations, or it could also contain some complex search logic?
Imagine the two entity classes Book and Author and the many-to-many relationship between them. At this point, a single DAO could be more than enough, as the basic CRUD operations for both entities are the same.
However, as the app gets more complex, new requirements appear, for example:
1. find the first book for a given author
2. find the books that an author has written during a period of time
3. find the books on a specific topic that a given author has written
4. .... etc
Where should all these go? Do I create now a separate DAO class for each entity, and put them there? Do they belong to the DAO layer at all?
I have this dilemma:
where should the following methods go:
findBooks(Author author)
findBooks(Publisher publisher)
both in the BookDAO, or one in AuthorDAO, the other PublisherDAO?
Or maybe I should think of a completely new abstraction, which is on top of DAOs and leaves them to basic CRUD ops - for example a SearchService, which lists all possible searches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
否。请随意将所有数据访问逻辑放在 DAO 中。
不要将 DAO 与 DAL:
No. Feel free to put all your data accessing logic in DAOs.
Don't confuse DAOs with DALs:
这绝对是 DAO(层)的工作和要求。你所拥有的一切并不重要。具体方法去它所属的DAO中。
这里有两件事。
Definitely the job of and call for DAO (layer). What you have aove it is immaterial. The specific method should go to the DAO which it belongs to.
Two things here.
这些都应该去哪里呢?我现在是否为每个实体创建一个单独的 DAO 类,并将它们放在那里?它们到底属于 DAO 层吗?
我遇到了这样的困境:以下方法应该去哪里: findBooks(Author 作者) findBooks(Publisher 出版商) 都在 BookDAO 中,或者一个在 AuthorDAO 中,另一个在 PublisherDAO 中
或者也许我应该考虑一个全新的抽象,它位于顶部DAO 并将它们留给基本的 CRUD 操作 - 例如 SearchService,它列出了所有可能的搜索?
Where should all these go? Do I create now a separate DAO class for each entity, and put them there? Do they belong to the DAO layer at all?
I have this dilemma: where should the following methods go: findBooks(Author author) findBooks(Publisher publisher) both in the BookDAO, or one in AuthorDAO, the other PublisherDAO
Or maybe I should think of a completely new abstraction, which is on top of DAOs and leaves them to basic CRUD ops - for example a SearchService, which lists all possible searches?