Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with a value object that needs to look up data in the database

Tags:

I am just starting out with the study of domain driven design and it is quite possible that my understanding of the Entities/Values divide is faulty so if this is so please let me know.

From my understanding, since its identity is completely defined by its properties an Address is the quintessential value object. From my understanding, this means among other things that there should not be a separate repository or data-access object for addresses.

This creates a dilemma for me since in my case an Address contains a Country where a Country has a name and a country-code and the list of country-codes is supposed to be loaded in from the database.

My question is, how do I design this? I want people to be able to create an address using the new operator but I don't want to create a data access object for country and if I do I certainly don't want to put a reference to it in the address object.

I have some ideas but I'd like to hear any suggestions anyone might have.

like image 390
George Mauer Avatar asked Feb 15 '09 15:02

George Mauer


People also ask

Can a value object reference an Entity?

A value object can reference other entities. For example, in an application that generates a route that describes how to get from one point to another, that route would be a value object.

Why do we need value objects?

Value Objects can be especially useful as a means for describing concepts in an application that have intrinsic rules but which are not themselves entities. In many applications, some concepts that are described as entities would be better off implemented as value objects.

What is the difference between Entity and value object?

The main difference between entities and value objects lies in the way we compare their instances to each other. The concept of identifier equality refers to entities, whereas the concept of structural equality - to value objects. In other words, entities possess inherent identity while value objects don't.


1 Answers

there is nothing in DDD precluding value objects from holding references to entities. Therefore your address would have a reference to a country entity.

like image 164
Simon Laroche Avatar answered Sep 30 '22 15:09

Simon Laroche