值对象如何存储在数据库中?
我还没有真正看到任何示例,但我假设它们保存在数据库内的包含实体表中。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出于您所描述的原因,可以将值对象存储在单独的表中。 但是,我认为您误解了实体与 VO - 这不是与持久性相关的问题。
下面是一个示例:
假设公司和个人都具有相同的邮件地址。 这些陈述中哪些被认为是有效的?
自动获取的Person.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?
Person.Address to automatically get
those changes"
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.
大多数开发人员倾向于首先考虑数据库而不是其他事情。 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.