How are Value Objects stored in the database?

半城伤御伤魂 提交于 2019-11-26 07:57:10

问题


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)


回答1:


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 it's own table (better database normalisation).

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




回答2:


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.




回答3:


I've started to learn DDD with Eric Evans book and the excellent dddsample Cargo project as example. http://dddsample.sourceforge.net/

So for those (like me) who wants to materialize the difference in the code implementation of this nuance in the Domain Model layer, I would say :

The overidded method Equals or/and sameIdentityAs / SameValueAs (from interface Entity and ValueObject) are, I think, the place of their expression.

It's just my feelin' :)

I think interesting to read this too:

http://martinfowler.com/bliki/ValueObject.html



来源:https://stackoverflow.com/questions/679005/how-are-value-objects-stored-in-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!