Question

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)

Was it helpful?

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

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

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top