DDD - Value Objects Vs. Entity Objects
Asked Answered
C

2

11

I'm new to DDD and trying hard to understand some of the concepts. How do you determine in your domain what objects are Entity objects and which ones are Value objects, and how exactly are they treated differently?

Cromorne answered 29/1, 2009 at 17:36 Comment(1)
Found a great explanation hereCromorne
C
2

From Jak Charlton's blog:

Entities “this is my Entity, there are many like it, but this one is mine”

The key defining characteristic of an Entity is that it has an Identity – it is unique within the system, and no other Entity, no matter how similar is the same Entity unless it has the same Identity.

Identity can be represented in many ways on an Entity – it could be a numeric identifier (the classic CustomerID), it could be a Guid (the classic … oh never mind), or it could be a natural key (like the CustomerNumber your Customer was given by your CRM system when they first bought from you).

Whichever way you choose to represent it, an Entity is defined by having an Identity.

Value Objects The key defining characteristic of a Value Objects is that it has no Identity. Ok, perhaps a little simplistic, but the intention of a Value Object is to represent something by it’s attributes only. Two VOs may have identical attributes, in which case they are identical. They don’t however have any value other than by virtue of their attributes.

Another aspect common to VOs is that they should probably be immutable, once created they cannot be changed or altered. You can create a new one, and as they have no identity, that is just the same as changing another one.

Think in a Car Class in POO in a car factory system application (no plate). Every car is unique even if the 2 cars are equals (same model, engine, color, weight, etc) and can be differentiated by a identity Vehicle Identification Number.

Two cars can be equals because its attributes Car1.equals(Car2) but not the same car because its VIN Car1 != Car2. If a car change its color it is no other car, it is the same car with other attributes. This is a Entity.

Now think in Color Class (for the car) with name and RGB fields. Cyan color has 'Cyan' in name and R = 0 G = 255 B = 255. No other identity field is needed because its atributes are its identity. Color is a VO and must be inmutable becuase changing name or RBG (or both) represent other color... or a bug in this case if name and RGB doesn't match ;)

Color1.equals(Color) and Color1 == Color2 must always have the same result.

Cly answered 28/3, 2014 at 11:48 Comment(0)
D
1

As I see it domain objects basically represent nouns of your business domain and do have an identity, whereas value objects do not carry any special meaning to the business (think MonetaryAmount) and do not have an identity.

Dudgeon answered 29/1, 2009 at 17:40 Comment(1)
I think that entity == "thingy that as a noun" is a really naive way to model your domain. Value objects can have a very special meaning to the business. MonetaryAmount could mean something very special if you do currency exchange and yet it would still be a value object.Monopteros

© 2022 - 2024 — McMap. All rights reserved.