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?
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.
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.
The values of a value object must be immutable once the object is created.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With