Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Should 'country' be a Value Object or an Entity?

'country': Value Object or Entity in DDD?

Opinions either way appreciated.

And, where to store the table of country names/codes? DB? XML? In a class?

Thanks!

like image 955
UpTheCreek Avatar asked Sep 02 '09 17:09

UpTheCreek


3 Answers

If your domain is geographic or political, then it might be an entity, but in the average case, a country is just a value associated with things like addresses. In that case, in the context of your object model, it's just a value.

As for storage, the domain model doesn't really care. You can use the database if it's convenient, XML if you prefer, and a class if you have behavior associated with countries.

like image 134
JasonTrue Avatar answered Oct 16 '22 18:10

JasonTrue


One of the characteristics of an entity is that it has a life cycle, i.e. it changes over time. A value object does not. In fact, value objects should be immutable. So the question to ask yourself is, "Does the country object change over time?"

Another aspect which differentiates entities and value objects is that two value objects with the same properties are the same. So if you have an instance of country with the name "France", it's the same as another instance of country with the name "France", even though they are two distinct instances (assuming that's the only property of country for the sake of this discussion). Think of strings in most languages, the string "fubar" equals another instance of the string "fubar".

Entities, on the other hand, are distinct even if they have the same properties. One customer with the name "John Smith" may not be the same as another customer with the name "John Smith".

So given these characteristics you should be able to decide. Since there can be only one "France" and it doesn't change over time, it's probably a value object - unless your app needs to track more about a country which may change over time.

like image 20
Mike Scott Avatar answered Oct 16 '22 16:10

Mike Scott


Imagine:

You have another entity - Customer.
Customer entity references Country object.
You have 2 entity instances with filled Country objects with same value (i.e. "France")
You are deleting country object from first entity (or first entity object)

  • if you want country to be deleted for 2nd entity object too
    => Country is an entity object
  • if you vant country to be deleted only for 1st entity object
    => Country is a value object
like image 20
Arnis Lapsa Avatar answered Oct 16 '22 16:10

Arnis Lapsa