domain-driven-designvalue-objects

How are Value Objects stored in the database?


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)


Solution

  • 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.