Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should DDD entities compare by reference or by ID?

When I started using DDD, I created Equals() methods in my entities that compared the ID of the entity. So two entity objects with the same ID would be considered equal.

At some point I thought about that and found that two entities in different states should not be considered equal, even when they describe the same thing (i.e. have the same ID). So now I use reference equality for my entities.

I then stumbled over this answer by Mark Seemann, where he writes

Entities are equal if their IDs equal each other.

Now, of course, I'd like to know which approach is better.

Edit: Note that the question is not whether having two instances of the same entity at the same time is a good idea. I'm aware that in most situations it is probably not.

like image 711
theDmi Avatar asked Jul 21 '15 07:07

theDmi


People also ask

Should domain entities have ID?

There's nothing wrong in it if you have a simple CRUD-like application. But if your application is large and complex, you should definitely choose rich model instead of anemic one, and thus, don't use Ids in your domain classes.

How is entity defined in DDD?

A domain entity in DDD must implement the domain logic or behavior related to the entity data (the object accessed in memory). For example, as part of an order entity class you must have business logic and operations implemented as methods for tasks such as adding an order item, data validation, and total calculation.

What is the difference between entity and value object in DDD?

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.

What is aggregate in DDD?

Aggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster of domain objects that can be treated as a single unit. An example may be an order and its line-items, these will be separate objects, but it's useful to treat the order (together with its line items) as a single aggregate.


1 Answers

The question is twofold. First, what you really want to know is

How to handle terms that the X language (or Y framework) impose when I code a domain model with it?

C# for example imposes you that any new concept you define inherit a certain set of public methods. Java includes even more methods.

I've never heard a domain expert talking about hash codes or instance equality, but this is one of those situations when the (often misunderstood) quote "don't fight the framework" from Evans apply: just teach developers to not use them when they do not belong to domain's interfaces.

Then, what you want to know is

What is an entity? How it relates to its own identity?

Start with why! You know that entities are terms of the ubiquitous language that are identifiable.

But WHY?

Plain simple: entities describe concepts whose evolution in time is relevant in the context of the problem we are solving!

It is the relevance of the evolution that defines the entity, not the other way around! The identity is just a communication tool to keep track of the evolution, to talk about it.

As an example think about you: you are a person with a name; we use your name to communicate about your interactions with the rest of the world during your life; still, you are not that name.

Ask yourself: why I need to compare domain entities? Is the domain expert talking this way? Or I'm just using a DDD parlance to describe a CRUD application that interact with a relational database?
To me, the need to actually implement Equals(object) or GetHashCode() into an entity looks like a smell of an inadequate infrastructure.

like image 196
Giacomo Tesio Avatar answered Oct 23 '22 23:10

Giacomo Tesio