Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD - Value Objects Vs. Entity Objects

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?

like image 720
Micah Avatar asked Jan 29 '09 17:01

Micah


People also ask

What are entities and value objects in DDD?

Entities and Value Objects in C# for DDD 1 Domain Driven Design. The term was coined by Eric Evans. ... 2 Entities. An object fundamentally defined not by its attributes, but by a thread of continuity and identity. ... 3 Value Objects. An object that describes some characteristic or attribute but carries no concept of identity. ... 4 Json.NET. ...

What is difference between entity and value object in 3D programming?

3 distinction between Entities and Value Objects Identifier vs structural equality: Entities have identifier,entities are the same if they have the same identifier. Value Objects on beyond the hand have structural equality, we consider two value objects equal when all the fields are the same.

Does Entity Framework have dbsets for value objects?

You’ll notice there are no DbSets or configuration for the value objects. Being able to break apart the configuration for value objects would have been nice, but I was unable to find a way to do so. The ReferenceOwnershipBuilder that Entity Framework Core uses to map value objects has constructors that are for internal use only.

What is the difference between entity and address value object?

Unlike entities, which have an Id, our Address value object has no identity, and the equality implementation is done entirely on the properties. If we need to update the address of an entity then we will need to create a new Address value object.


2 Answers

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.

like image 64
Anton Gogolev Avatar answered Oct 21 '22 21:10

Anton Gogolev


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.

like image 25
jlvaquero Avatar answered Oct 21 '22 21:10

jlvaquero