Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between an entity and an aggregate in domain driven design

Please what is the main difference between entities and aggregate roots in domain driven design. For example in entity framework, what is the use of aggregates if I can ensure data integrity entities?

like image 662
D_Edet Avatar asked Sep 02 '15 12:09

D_Edet


People also ask

Is an aggregate an entity?

An aggregate is a collection of one or more related entities (and possibly value objects). Each aggregate has a single root entity, referred to as the aggregate root. The aggregate root is responsible for controlling access to all of the members of its aggregate.

What is an aggregate in Domain-Driven Design?

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.

Can an entity be part of multiple aggregates?

As you rightly pointed out, Entity instances shouldn't be shared between aggregates, as one aggregate wouldn't be aware of changes to the entity made through another aggregate and couldn't enforce its invariants.

What is the difference between entity and value object?

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.


1 Answers

From domain driven design perspective DbContext is the implementation of UnitOfWork and a DbSet<T> is the implementation of a repository.

This is the point where DDD and EntityFramework contrast. DDD suggests to have a Repository per aggregate root but EntityFramework creates one per Entity.

So, what is an aggregate root?

Assume that we have a social network and have entities like Post, Like, Comment, Tag. (I believe you can imagine the relations between these entities) Some of the entities are "Aggregate Root"

To find the aggregate root(s) I try to find which entities cannot live without the other. For instance, Like or Comment cannot live without a Post. Then Post is an aggregate root and we need a PostRepository or turn the Post entity into a Repository (the famous collection like interface thing). CRUD operations for Comment and Like (as well as the Post) should remain on this repository.

like image 137
Mehmet Ataş Avatar answered Sep 22 '22 23:09

Mehmet Ataş