DDD:帮助我进一步理解值对象和实体

发布于 2024-10-17 09:52:58 字数 798 浏览 5 评论 0原文

关于此有几个问题,阅读它们对我没有帮助。在 Eric Evans DDD 中,他使用了地址在某些情况下作为值类型的示例。对于邮购公司来说,地址是一种值类型,因为地址是否共享、还有谁住在该地址并不重要,只要包裹到达该地址即可。

这对我来说是有意义的,直到我开始思考如何设计它。给定第 99 页上的图表,他是这样的:

+------------+
|Customer    |
+------------+
|customerId  |
|name        |
|street      |
|city        |
|state       |
+------------+

这将更改为:

+------------+
|Customer    | (entity)
+------------+
|customerId  |
|name        |
|address     |
+------------+

+------------+
|Address     | (value object)
+------------+
|street      |
|city        |
|state       |
+------------+

如果这些是表,Address 将拥有自己的 Id,以便与客户建立关系,将其转变为实体。

在关系数据库中,这些内容会保留在同一个表中(例如第一个示例),并且您会使用 ORM 的功能将地址抽象为值对象(例如 nHibernate 的组件功能),这一想法是否是这样的?

我意识到几页后他谈到了非规范化,我只是想确保我正确理解了这个概念。

There are several questions on this, and reading them isn't helping me. In Eric Evans DDD, he uses the example of address being a value type in certain situations. For a mail order company, the address is a value type because it doesn't really matter if the address is shared, who else lives at the address, simply that the package arrives at the address.

This makes sense to me until I start thinking about how this would be designed. Given the diagram on page 99, he has it like this:

+------------+
|Customer    |
+------------+
|customerId  |
|name        |
|street      |
|city        |
|state       |
+------------+

This changes to:

+------------+
|Customer    | (entity)
+------------+
|customerId  |
|name        |
|address     |
+------------+

+------------+
|Address     | (value object)
+------------+
|street      |
|city        |
|state       |
+------------+

If these were tables, Address would have its own Id in order to have a relationship with the customer, turning it into an entity.

Is the idea that in a relational database these would stay in the same table, such as in the first example, and that you'd use features of the ORM to abstract address as a value object (such as nHibernate's component features)?

I realize that a couple of pages later he talks about denormalization, I'm just trying to make sure I'm understanding the concept correctly.

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

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

发布评论

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

评论(4

巾帼英雄 2024-10-24 09:52:58

当 Eric Evans 谈论“实体具有身份,而值对象没有”时,他并不是在谈论数据库中的 ID 列,而是在谈论身份作为一个概念。

VO 没有概念上的同一性。这并不意味着他们不应该具有持久性身份。不要让持久性实现蒙蔽了您对实体与 VO 的理解。

您可以为地址创建单独的表,也可以在客户的同一个表中创建

When Eric Evans talks about "entities have identity, Value Objects do not", he's not talking about an ID column in the database - he's talking about identity as a concept.

VOs have no conceptual identity. That doesn't mean that they shouldn't have persistence identity. Don't let persistence implementation cloud your understanding of Entities vs VOs.

You can create separate table for address or in same table in Customer

眉目亦如画i 2024-10-24 09:52:58

这个想法是在关系中
数据库这些将保留在相同的
表,例如第一个示例中,
并且您会使用 ORM 的功能
将地址抽象为值对象
(比如nHibernate的组件
功能)?

是的,一般来说,就是这个想法。

或者(如果您的 ORM 不直接支持值对象),您可以让 VO 表有一个 ID,但将其隐藏在域模型中。

Is the idea that in a relational
database these would stay in the same
table, such as in the first example,
and that you'd use features of the ORM
to abstract address as a value object
(such as nHibernate's component
features)?

Yes, generally, that is the idea.

Alternatively (if your ORM doesn't support Value Objects directly), you can let the VO tables have an ID, but hide that within your domain model.

心安伴我暖 2024-10-24 09:52:58

我个人并不关心值对象上有 ID,只要它们正确地覆盖相等比较即可(因为值对象的不同之处在于它们的值而不是身份)。

有时,将值对象映射到数据库是技术问题(例如,将 props 标记为虚拟,以便 ORM 可以在下面爬行),您只需要稍微牺牲域模型的纯度即可。或者让您的基础设施更加智能 - 使用 nhib 组件或其他东西。

I personally don't give a damn about having ID on value objects as long as they override equality comparison properly (cause value objects differs by their value not identity).

Mapping value objects to database is technical concern, sometimes (e.g. marking props virtual so ORM could crawl underneath) You just need to sacrifice purity of domain model a bit. Or make Your infrastructure smarter - usage of nhib components or something.

浅忆 2024-10-24 09:52:58

是的,通常地址会保留在同一个表中。地址将被映射为如下所示:

+-----------------+
|Customer         |
+-----------------+
|customerId       |
|name             |
|address_street   |
|address_city     |
|address_state    |
+-----------------+

如果地址是一个实体,那么它将位于一个单独的表中,正如您所说。如果两个相同的客户链接到同一地址实体,则更改该地址的属性将影响这两个客户。然而,VO 的实现只会影响其中之一。

Yes, generally Address would stay in the same table. Address would be mapped something like this:

+-----------------+
|Customer         |
+-----------------+
|customerId       |
|name             |
|address_street   |
|address_city     |
|address_state    |
+-----------------+

If Address was an entity, then it would be in a separate table, as you said. If two of the same Customers linked to the same Address entity, then changing an attribute of that Address would affect both Customers. However, a VO implementation would only affect one or the other.

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