值对象如何存储在数据库中?

发布于 2024-07-16 07:34:42 字数 217 浏览 8 评论 0原文

我还没有真正看到任何示例,但我假设它们保存在数据库内的包含实体表中。

IE。 如果我有一个 Person 实体/聚合根和一个相应的 Person 表,如果它有一个名为 Address 的值对象,则地址值将保存在该 Person 表中!

对于我拥有其他实体(例如公司等)且拥有地址的域,这是否有意义?

(我目前正在编写一个项目管理应用程序并尝试进入 DDD)

I haven't really seen any examples, but I assume that they are saved inside the containing entity table within the database.

Ie. If I have a Person entity/aggregate root and a corresponding Person table, if it had a Value Object called Address, Address values would be saved inside this Person table!

Does that make sense for a domain where I have other entities such as Companies etc. that have an Address?

(I'm currently writing a project management application and trying to get into DDD)

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

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

发布评论

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

评论(2

月棠 2024-07-23 07:34:42

出于您所描述的原因,可以将值对象存储在单独的表中。 但是,我认为您误解了实体与 VO - 这不是与持久性相关的问题。

下面是一个示例:

假设公司个人都具有相同的邮件地址。 这些陈述中哪些被认为是有效的?

  1. “如果我修改 Company.Address,我想要
    自动获取的Person.Address
    这些更改”
  2. “如果我修改 Company.Address,它
    不得影响 Person.Address”

如果 1 为 true,Address 应该是一个实体,因此具有它的自己的表

如果2为真,地址应该是一个值对象它可以存储为内部的组件。如

您所见,Address 的持久化方式与 Entity/VO 语义无关。

It's ok to store Value Objects in a separate table, for the very reasons you've described. However, I think you're misunderstanding Entities vs VOs - it's not a persistence related concern.

Here's an example:

Assume that a Company and Person both have the same mail Address. Which of these statements do consider valid?

  1. "If I modify Company.Address, I want
    Person.Address to automatically get
    those changes"
  2. "If I modify Company.Address, it
    must not affect Person.Address"

If 1 is true, Address should be an Entity, and therefore has it's own table

If 2 is true, Address should be a Value Object. It could be stored as a component within the parent Entity's table, or it could have its own table (better database normalisation).

As you can see, how Address is persisted has nothing to do with Entity/VO semantics.

情定在深秋 2024-07-23 07:34:42

大多数开发人员倾向于首先考虑数据库而不是其他事情。 DDD 不知道如何处理持久性。 这由存储库来处理。 您可以将其保存为 xml、sql、文本文件等。实体/聚合/值对象是与域相关的概念。

维杰·帕特尔(Vijay Patel)的解释非常完美。

Most developers tend to think in the database first before anything else. DDD does not know about how persistence is handled. That's up to the repository to deal with that. You can persist it as an xml, sql, text file, etc etc. Entities/aggregates/value objects are concepts related to the domain.

Explanation by Vijay Patel is perfect.

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