价值对象如何存储在数据库中? [英] How are Value Objects stored in the database?
问题描述
我还没有真正看到任何示例,但是我假设它们已保存在数据库内的包含实体表中.
即.如果我有一个Person实体/集合根和一个对应的Person表,如果它具有一个名为Address的值对象,则地址值将保存在此Person表中!
这对我拥有其他拥有公司地址的其他实体(例如公司等)的域名有意义吗?
(我目前正在编写一个项目管理应用程序,并尝试使用DDD)
出于您所描述的原因,可以将Value Objects存储在单独的表中.但是,我认为您对实体与VO误解了-这与持久性无关.
这是一个例子:
假设公司和人员都具有相同的邮件地址.这些语句中的哪一个被认为是有效的?
- "如果我修改Company.Address,我要 要自动获取的Person.Address 这些变化"
- "如果我修改Company.Address, 不得影响Person.Address"
如果 1 为true,则地址应该是 实体 ,因此具有自己的表>
如果 2 为true,则地址应为 值对象 .它可以作为组件存储在父实体表中,也可以具有自己的表(更好的数据库规范化).
如您所见,地址的持久化与实体/VO语义无关.
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)
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?
- "If I modify Company.Address, I want Person.Address to automatically get those changes"
- "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 it's own table (better database normalisation).
As you can see, how Address is persisted has nothing to do with Entity/VO semantics.
这篇关于价值对象如何存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!