Skip to content

Instantly share code, notes, and snippets.

@breezhang
Last active August 29, 2015 13:56
Show Gist options
  • Save breezhang/8822417 to your computer and use it in GitHub Desktop.
Save breezhang/8822417 to your computer and use it in GitHub Desktop.
DDD

pic

Entity

“This is my Entity, there are many like it, but this one is mine.”

The key defining characteristic of an Entity is that it has an Identity – it is unique within the system, and no other Entity, no matter how similar is, the same Entity unless it has the same Identity.

For example, consider a Person concept. If I have two Person objects, with the same Name, are they same Person? Bob Smith from Cheyenne, Wyoming and Bob Smith from Tallahassee, Florida might not agree. A popular gimmick I’ve seen is interviewing a Person with a famous name (but different identity). So if Name isn’t a Person’s distinguishing attribute, what is? Address? Social Security Number? Not for non-US citizens, what about a Kiwi Bob Smith?

In each of these examples, a Person is identified by more than their attributes, such as Name, Address, PhoneNumber, etc. A Person has a unique identity that manifests itself if different ways in different systems. Each system has their own attributes they’re concerned with, but the Person is always the same entity (not class, that’s different).

My “litmus test” for Entities is a simple question:

If two instances of the same object have different attribute values, but same identity value, are they the same entity?

If the answer is “yes”, and I care about an identity, then the class is indeed an entity. I model entities with reference objects (classes), and I give them a surrogate identity (i.e., probably a GUID). Additionally, my model must include what it means to have the same identity. That means overriding Equals, looking solely at the identity and not attributes.

Value Object

The key defining characteristic of a Value Object is it has no Identity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment