2
votes

In Domain Driven Design(DDD) a ValueObject has the following definition

A Value Object is an object that describes some characteristic or attribute but carries no concept of identity.

Lets say I have 'Client' entity and an 'Order' entity. Orders are related to Clients, so normally I would add field ClientId in the Order class, because I may not reference the Client entity itself. So far so good...

Now I wonder if it is oke to create a value object ClientInfo, which will include the name of the client, the client status, but also the ClientId? ClientInfo would be immutable with only getters for clientName, clientStatus and clientId.

So there you have it, a value object with an entity identifier. Is this against the definition of a value object, or am I safe here?

1
I don't think that this question is a duplicate of the referenced question. Here, the OP asks specifically about IDs within value objects, not about the general concepts of entities / value objects. - theDmi
This indeed is not a duplicate of referred question, but more specifically targeted to the Value Object and its constraints. The definition talks about ValueObject not having the concept of identity, which means the ValueObject should not have an ID representing the ValueObject. However, the ValueObject may contain identfiers referring to entities - ChrisMir

1 Answers

6
votes

A value object with a reference to an entity identifier is fine. If you had two ClientInfo objects with the same information in, they would be completely interchangeable. They are values just like Strings or integers.