关于 DAO 的正确使用

发布于 2024-12-29 00:30:09 字数 501 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺渁∝ 2025-01-05 00:30:09

DAO 是否只保留用于基本的 CRUD 操作

否。请随意将所有数据访问逻辑放在 DAO 中。

不要将 DAODAL

Is DAO only reserved for the basic CRUD operations

No. Feel free to put all your data accessing logic in DAOs.

Don't confuse DAOs with DALs:

要走就滚别墨迹 2025-01-05 00:30:09

这些应该去哪里?我现在是否为每个实体创建一个单独的 DAO 类,并将它们放在那里?它们到底属于 DAO 层吗?

这绝对是 DAO(层)的工作和要求。你所拥有的一切并不重要。具体方法去它所属的DAO中。

或者也许我应该考虑一个全新的抽象,它位于 DAO 之上,并将它们留给基本的 CRUD 操作 - 例如 SearchService,它列出了所有可能的搜索?

这里有两件事。

  1. 看起来您正在使用 Hibernate 或某些 ORM 工具。因此我相信你有合适的对象。您可以在 DAO 层中使用这些对象而不是 Map、List 或 Primitives,没有任何问题。无论如何,在不偏离DAO层的情况下,你仍然可以在这里拥有DAO层。并根据需要验证业务规则。
  2. 看起来你的意思是 AbstractDAO 模式(搜索/谷歌相同),Hibernate 和 Spring 也提供了这样的东西。 Spring 也为此提供了一种 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?

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.

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?

Two things here.

  1. Looks like you are using Hibernate or some ORM tool here. Hence you have proper objects I believe. And you can use those objects in DAO layer instead of Map or List or Primitives without any issue. Anyways, not deviating from DAO layer, you still can have DAO layer here. And do business rules validationd as you want.
  2. Looks You mean the AbstractDAO pattern (search/google for the same), again Hibernate and Spring do provide such things. Spring do provide a SearchService kind of tool (classes actually) for this too.
最后的乘客 2025-01-05 00:30:09

这些都应该去哪里呢?我现在是否为每个实体创建一个单独的 DAO 类,并将它们放在那里?它们到底属于 DAO 层吗?

见下文。

我遇到了这样的困境:以下方法应该去哪里: findBooks(Author 作者) findBooks(Publisher 出版商) 都在 BookDAO 中,或者一个在 AuthorDAO 中,另一个在 PublisherDAO 中

这要看情况,我认为这不是企业软件架构的问题,而是 OOD 的问题。当你的逻辑变得复杂时,你必须记住增加软件组件之间的内聚性并减少耦合,并分别从重构目录中提取一个新类(Extact Class from Refactoring Catalog http://martinfowler.com/refactoring/catalog/extractClass.html) 与这些方法被认为属于同一业务上下文,例如 AuthorDAO 用于所有有关 Author 功能的方法和 Books 用于所有 Books 调用,等等,以使您的代码更具可读性、可重用性、可测试性、可维护性……

或者也许我应该考虑一个全新的抽象,它位于顶部DAO 并将它们留给基本的 CRUD 操作 - 例如 SearchService,它列出了所有可能的搜索?

如果您指的是业务逻辑,则该抽象存在于多层模型中,称为业务层。
您可以将业务逻辑与业务对象相关联。但是 findXXX 方法属于位于持久层和业务层之间的 DAO。

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?

see below.

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

It depends and i think it's not a question of enterprise software architecture, instead, OOD. As your logic is getting complicated, you must keep in mind to increase cohesion and reduce coupling between software compontents, and respectively extract a new class (Extact Class from Refactoring Catalog http://martinfowler.com/refactoring/catalog/extractClass.html) with these methods which is considered to be belong to the same business context, like AuthorDAO for all methods about Author functionality and Books for all Books calls, so on to make your code more readable, reusable, testable, maintable, ...

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?

That abstraction exists in multitier model, is called business layer, if you mean business logic.
You can associate your business logic with business objects. But the findXXX methods belong to your DAO which lies between persistence and business layer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文