3

From How are Value Objects stored in the database? :

Assume that a Company and Person both have the same mail Address. Which of these statements do consider valid?

   1."If I modify Company.Address, I want Person.Address to automatically get those changes"

   2."If I modify Company.Address, it must not affect Person.Address"

If 1 is true, Address should be an Entity

If 2 is true, Address should be a Value Object.

Shouldn't in the above model the mail Address be a Value Object, since even if Company and Person have same mail, this mail still doesn't have a conceptual identity?

In other words, if initially Company and Person share initial.address@gmail.com, but then get new mail new.address@gmail.com, then we can argue that mail address initial.address@gmail.com itself didn't changed, instead Company and Person replaced it by new.address@gmail.com ?

Thus to my understanding a mere fact that Address is shared shouldn't be enough to give it personality (ie identity)?!

Thank you

Community
  • 1
  • 1
user1483278
  • 929
  • 1
  • 9
  • 17
  • In what context would a person and company share the same mail address, such that one would care about the duplication you mention? The reason for modelling as a value object would be the recurrence of the address concept and behavior that floks towards it. – Yves Reynhout Jul 11 '12 at 20:46
  • I just began learning patterns and design, and at this point I'm not able to come up with any meanigfull examples. My question was inspired by the quote from another thread, where poster ( much more experienced than me ) suggested mail Address is Entity if both Company and Person share it. Anyways, you seem to be suggesting that Address should be Entity, and that I should have a very good reason to make it a VO instead, but you don't elaborate on why. To my knowledge "object should be a value object until proven otherwise" – user1483278 Jul 11 '12 at 21:26
  • No, I'm not suggesting modelling address as an entity (not trying to confuse you here). I was suggesting that only a proper requirement/story/use case/scenario can give you the feedback you seek. – Yves Reynhout Jul 11 '12 at 21:53

3 Answers3

7

Yes, your understanding is correct. Address should almost always be a value object, since in most domains, the address is indeed just a value.

The fact that a Company and a Person have the same Address today does not mean that if one changes, the other should change too. If such a relationship exists, it should be modeled through an explicit constraint rather than by making Address an entity.

Eric Evans talks about this in his excellent book on Domain-Driven Design and even provides a specific example where Address might be an entity -- the postal service, whose domain revolves around addresses, and where the identity of individual addresses is important.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • You already answered my initial question, so I will mark it as answered regardless (in a day), but if you do find some time,could you also help me with the following question: I assume user1494736 is correct about Contact Information object being an Entity, since to my understanding Contact Information object itself can be modified? If answer is yes, can you think of a situation where Contact Information object could be treated as a Value Object? – user1483278 Jul 12 '12 at 18:20
  • 1
    @user1483278: Again, it depends on your requirements. Contact information can certainly be a value object if you don't need to identify it; it can also be an entity if it will be shared by multiple people, for example. – casablanca Jul 13 '12 at 04:44
1

Actually, the mail has a conceptual identity. The problem is that you aren't really modeling the e-mail address, but the Contact Information of a Person and/or the Contact Information of a Company. Continuing with the topic, value object vs identity object is more an implementation decision rather than a "absolute truth".

You could use an immutable value object and when you tell the system "change address a for address b" search for all instances of address A in both person and the company, and update them to point to address b now. (or you could just update a single one of them).

Using a non-value Contact Information object is more powerful. With:

Contact Information
{
    string email;
}

You could actually have both person and company pointing to the same Contact Information object, so when you update one, you update the other too. Or you could have each of them point to a different Contact Information object, so when you update one, you don't modify the other...

BTW: e-mail has conceptual identity, since changing the e-mail address is actually what google did to me last week, when they changed my e-mail address from ending in @googlemail.com to @gmail.com... So if someone had my e-mail for both me and my company, just one update just change both instances, since in that ocassion my e-mail address changed itself... If on the other hand, I start using a different e-mail address, what changes is my contact information... My old e-mail address would still exist and be the same.

My advise is to model everything with identity, unless it's an extremely well object of a domain which you want to optimize an use as a value object for whatever reason (such as numbers, strings, etc...). But remember that it's usually a implementation decision, not something of the domain.

user1494736
  • 2,425
  • 16
  • 8
  • 1
    "My advise is to model everything with identity..." Hi, according to http://tech.groups.yahoo.com/group/domaindrivendesign/message/6290 , it should be just the other way around – model everything as a value object until proven otherwise – user1483278 Jul 12 '12 at 18:27
1

This is a classic case of taking something out of context.

The original question (How are Value Objects stored in the database) wasn't questioning the validity of the model, and my example wasn't to highlight these issues either. My answer was about Persistence vs Entity/VO.

I used the example of Customer, Person, and Address purely so that I could share the same Ubiquitous Language with the OP (I didn't have time to think up a better example).

I would follow casablanca's advice on this one (upvoted)

Community
  • 1
  • 1
Vijay Patel
  • 17,094
  • 6
  • 31
  • 35
  • I apologize for taking it out of context, but I simply used it because it was a good example on which I was able to build up my questions – user1483278 Jul 13 '12 at 15:03