Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value Object within Value Object [DDD]

I'm having a little bit of difficulty trying to understand how to use Value Objects and Aggregates. I'm going to explain my situation with an example.

I have an entity called Relationship. It holds values such as ID and StartDate. There are two value objects: Boyfriend and Girlfriend. Both the boyfriend and girlfriend objects have addresses so I created another value object Address. Inside the database these are all combined in one table (i.e. ID, StartDate, BoyfriendAddress, GirlfirendState, etc.).

How do I create the Boyfriend and Girlfriend? From my understanding of the definition, they do not qualify as an aggregate because they do not have a root entity. Can a value object have another value object inside of it?

like image 811
AdamDev Avatar asked Nov 07 '12 13:11

AdamDev


People also ask

What is a value object in DDD?

A Value Object is an immutable type that is distinguishable only by the state of its properties. That is, unlike an Entity, which has a unique identifier and remains distinct even if its properties are otherwise identical, two Value Objects with the exact same properties can be considered equal.

Can a value object contain an Entity?

Yes it can. This would be a relatively obscure case but DDD allows for it and it can be useful. From the DDD book by Eric Evans: VALUE OBJECTS can even reference ENTITIES.

Should value objects be immutable?

The values of a value object must be immutable once the object is created.

Can value object have logic?

Value objects are not only containers of data - they can also contain business logic. The fact that the value objects are also immutable makes the business operations both thread-safe and side-effect free.


1 Answers

Yes, you can have value objects inside other value objects. I think the simplest example of this would be the class Money, which contains an amount and a Currency, which is another VO.

Also, if Boyfriend and Girlfriend don't have a global unique id, then they are not aggregate roots, and if they don't have a local unique identity inside a relationship, then they aren't entities either.

I'll be honest, 1st of all your app is not politically correct :) - As there are GLBT relationships. And 2nd, it feels strange that a person (I assume you have their names) is not an entity or aggregate-root.

like image 79
Augusto Avatar answered Oct 14 '22 19:10

Augusto