Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Help me further understand Value Objects and Entities

There are several questions on this, and reading them isn't helping me. In Eric Evans DDD, he uses the example of address being a value type in certain situations. For a mail order company, the address is a value type because it doesn't really matter if the address is shared, who else lives at the address, simply that the package arrives at the address.

This makes sense to me until I start thinking about how this would be designed. Given the diagram on page 99, he has it like this:

+------------+
|Customer    |
+------------+
|customerId  |
|name        |
|street      |
|city        |
|state       |
+------------+

This changes to:

+------------+
|Customer    | (entity)
+------------+
|customerId  |
|name        |
|address     |
+------------+

+------------+
|Address     | (value object)
+------------+
|street      |
|city        |
|state       |
+------------+

If these were tables, Address would have its own Id in order to have a relationship with the customer, turning it into an entity.

Is the idea that in a relational database these would stay in the same table, such as in the first example, and that you'd use features of the ORM to abstract address as a value object (such as nHibernate's component features)?

I realize that a couple of pages later he talks about denormalization, I'm just trying to make sure I'm understanding the concept correctly.

like image 858
user500038 Avatar asked Feb 15 '11 21:02

user500038


People also ask

What is the difference between entity and 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.

What is a DDD value object?

Value Object is an object that represents a concept from your problem Domain. It is important in DDD that Value Objects support and enrich Ubiquitous Language of your Domain. They are not just primitives that represent some values - they are domain citizens that model behaviour of your application.

What are entities in DDD?

A domain entity in DDD must implement the domain logic or behavior related to the entity data (the object accessed in memory). For example, as part of an order entity class you must have business logic and operations implemented as methods for tasks such as adding an order item, data validation, and total calculation.

How do you identify the value of a object?

A value object should always belong to one or several entities, it can't live by its own. Value objects should be immutable; entities are almost always mutable. To recognize a value object in your domain model, mentally replace it with an integer. Value objects shouldn't have their own tables in the database.


1 Answers

When Eric Evans talks about "entities have identity, Value Objects do not", he's not talking about an ID column in the database - he's talking about identity as a concept.

VOs have no conceptual identity. That doesn't mean that they shouldn't have persistence identity. Don't let persistence implementation cloud your understanding of Entities vs VOs.

You can create separate table for address or in same table in Customer

like image 63
kamal Avatar answered Sep 24 '22 02:09

kamal