谁应该将对象保存到持续的内存

发布于 2025-01-22 13:15:00 字数 92 浏览 2 评论 0原文

我目前正在学习UML。我到处看,但找不到答案。对象的创建者是否应该是始终通过数据访问对象或专家将其保存在持久内存中的人,是否是通过持久内存中的数据访问对象保存自身的对象?

I am currently learning UML. I looked everywhere but couldn't find an answer. Should the creator of an object be the one to always saves it in persistent memory via a data access object or by an expert, is an object better to save itself via a data access object in persistent memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

若沐 2025-01-29 13:15:00

在这方面,主要替代方案

不可知。这取决于您的体系结构选择:

  • 一种流行的方法是 repository repository objects 那是隐藏数据库的一种集合。您会在存储库中插入,检索,抑制或更新元素,并且存储库负责数据库。
  • 另一种流行的方法是主动记录。每个持久对象都负责在数据库中插入,更新或删除自身。
  • 还有其他几种方法。

如何选择?

您描述的场景对应于第二个方案。这是针对小域逻辑的CRUD导向应用的诱人方法。但是,它有几个缺点,可以限制其适用性:

  • 它没有执行正确的关注点分开(另请参见Songle责任原则)。
  • 它将您的系统紧密地耦合到数据库,在以后的阶段可能很难更改inderlying数据库。

此外,您还需要照顾交易逻辑(完成一组相关更改或取消所有更改),或者避免内存中的几个对象与数据库中的同一对象相对应。虽然不是不可能的,但是有效记录这更困难。

如何加深您对这些选择的理解?

关于该主题的最全面的书是福勒的“ 企业应用程序架构的模式”。另一本值得投资加深存储库背后的书是埃文斯(Evans)' ddd bible 。 (IMHO,您将通过阅读这两本书来节省多年的工作实验和发现。然后,您可以利用节省的时间来加深您在其他创新域上的技能)。

Main alternatives

UML is agnostic in this regard. It depends of your architectural choices:

  • A popular approach is Repository objects that act as a kind of collection that hides the database. You’d insert, retrieve, suppress or update elements in the repository and the repository takes care of the database.
  • Another popular approach is the active record. Each persistent object is in charge of inserting, updating or deleting itself in the database.
  • There are several other approaches.

How to chose?

The scenario you describe corresponds to the second one. It is a tempting approach for CRUD oriented application with little domain logic. But it has several drawbacks that limit their suitability:

  • it does not enforce proper separation of concerns (see also Songle Responsibility Principle).
  • it couples your system tightly to the database and it might be difficult at a later stage to change the inderlying database.

Moreover, you’d need to care also for transactional logic (i.e.either complete a set of related changes or cancel them all), or to avoid that several objects in memory correspond to the same object in the database. While not impossible, this is more difficult with active records.

How to deepen your understanding of these alternatives?

The most comprehensive book on this topic is Fowler’s “Patterns of enterprise application architecture”. Another book worth to invest in for deepening what’s behind repositories, is Evans’ DDD bible. (Imho, you will save years of on the job experimentation and discovery by reading both books. You can then use the saved time to deepen your skills on other innovative domains).

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