4

The question might not be very clear in the title, let me explain:

In my model, I have an Person, that has an Address. However, many Persons can share the same Address.

As I was defining my model, I assumed that Person is an Entity, but Address a Value-Object since if you change a single property of the Address, well it's not the same Address anymore.

Since multiple Persons can share an Address, if I jump right into the database implementation, and naively assume that person has some address_xxxx fields, wouldn't it generate too many duplicates in the database ? Isn't it better that person has an address_id field, related to an address table ? If so, then Address is an Entity right ?

Lucio
  • 632
  • 5
  • 21
  • 2
    Don't confuse the persistence model (aka database) with your domain model. If Address is a value object in your domain then treat it as such. Persistence is just an implementation detail. – Cerad Jan 20 '17 at 15:26
  • @cerad sure, but sooner or later I will have to deal with it. I'm starting to think more and more about making Address an entity, since at some point in the domain I will have to present a list of unique addresses. Would that make Address an Entity ? – Lucio Jan 20 '17 at 15:28
  • 2
    If you need to deal with an address outside of a person then sure, by definition, it becomes a domain entity. Just be careful about trying to apply domain driven design techniques unless your application really needs them. Most don't and you can quickly tie yourself up in knots. – Cerad Jan 20 '17 at 15:40
  • Thanks for the advice, I am not completely using DDD actually, more like using some of its principles. The application I am creating is not big and complex enough for domain design to apply – Lucio Jan 20 '17 at 15:44
  • @Lucio What happens when an address that was shared changes? Should the location change reflect to everyone linked with that address? – plalx Jan 20 '17 at 16:43
  • @plalx yes it should – Lucio Jan 22 '17 at 19:24
  • @Lucio Well in that case like I said in my answer address would be an entity. – plalx Jan 22 '17 at 22:48

2 Answers2

5

Is a Value Object that is used many times an entity?

No, but it depends...

It is often the case that a value object is actually a proxy identifier for an entity, that you may not have explicitly realized in your model.

For example:

1600 Pennsylvania Ave NW
Washington, DC 
20500

If you look at that carefully, you'll see embedded in it

  • The name of a street
  • The name of a city

If those are references to a street/city entities in your model, then "address" is the representation of the current state of some entity (ex: "The White House").

Complicating things further - you want suitable abstractions for your model.

Consider money:

{USD:100}

That's a value type, we can replace any USD:100 with a "different" USD:100

{USD:100, SerialNumber:KB46279860I}

That's still a value (it's state), but it the state of a specific bill that exists in circulation (somewhere). What we have here is an information resource that is describing an entity out in the real world, somewhere.

You also need to be careful about coincident properties. For example; the name of the street changes -- should the value of address change? If the model cares about the current identifier of a location, then perhaps it should. If the model is tracking what information you put on an envelope two months ago, then it certainly shouldn't. (In other words, when we changed the label for the street entity, the label already printed on the envelope entity didn't change).

It's an important question, but the answer changes depending on what you are modeling at the time.

VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
3

In my model, I have an Person, that has an Address. However, many Persons can share the same Address.

Isn't it better that person has an address_id field, related to an address table ? If so, then Address is an Entity right?

You have to recognize that there are two distinct models, a domain model and a persistence model and both may not agree on whether a concept is an entity or a value.

The first thing you have to do is ask yourself what is an address from the domain perspective? Is your domain interested in the lifecycle of addresses or they are just immutable values? For instance, what happens if there is a typo in an address? Do you simply discard the incorrect one and replace it or would you rather modify the original address details to track it's continuity? These questions will help you to determine whether an address is an entity or a value from the domain perspective.

Now, a concept may be a value in the domain while being an entity in the persistence model. For instance, let's say that you aren't interested in the lifecycle of addresses in the domain, but you are very concerned about optimizing the storage space. In that case, you could give identifiers to unique addresses in the DB and use that for relationships rather than copying the same address details multiple times.

However, doing so would introduce additional tensions between your models, so you must be sure that there are real benefits to do so.

plalx
  • 42,889
  • 6
  • 74
  • 90