Difference between Aggregate and Entity

9,775 views
Skip to first unread message

Sharas

unread,
Oct 26, 2011, 11:59:42 AM10/26/11
to ddd...@googlegroups.com
Hello,

What is the difference between Aggregate and Entity?


Thanks

Colin Yates

unread,
Oct 26, 2011, 12:05:36 PM10/26/11
to ddd...@googlegroups.com
Entity has meaning (and therefore an id) defined outside of the state of its values as oppose to "value objects" whose identity is defined entirely by its state.

Aggregate a structure of internally consistent and logically related "things" which might be entities or value objects.

(I might be talking nonsense in terms of DDD so believe whoever writes next :))

Sarunas Mazylis

unread,
Oct 26, 2011, 12:09:03 PM10/26/11
to ddd...@googlegroups.com
@Colin: Thanks

@All: Is Aggregate an entity? When entity is not an aggregate? Why difference is important?

Thanks

Colin Yates

unread,
Oct 26, 2011, 1:04:48 PM10/26/11
to ddd...@googlegroups.com
An aggregate is a composite of something else; it is composed of other things.  You could argue a single entity is a composite (and therefore strictly an aggregate) of value objects.

However, in the standard terminology I (others may have different expectations), given:

Street {
    Collection<House> houses
}

House {
  Number houseNumber
  Collection<People> residents
  Collection<Door> doors
}

and People {
    String firstName
    String lastName
}

I would categorise them as:

Aggregates: Street, House, People
Aggregate Root (depends on the domain but probably): Street and House
Value Objects: Door (because I don't care about tracking the history of an instance of a door), String

but please see my first disclaimer :)

Colin Yates

unread,
Oct 26, 2011, 1:05:25 PM10/26/11
to ddd...@googlegroups.com
and of course by "People" I mean "Person"

Michael Brown

unread,
Oct 26, 2011, 1:14:50 PM10/26/11
to ddd...@googlegroups.com

There are four concepts of importance here:

 

Entity: a unique object within the domain that has significance (e.g. Customer) and can change over time

Value Object: an immutable object within the domain that has no significance outside of its properties (e.g. date, address)

Aggregate: a collection of entities or value objects that are related to each other through a root object

Aggregate Root: An Entity that “owns” an Aggregate and serves as a gateway for all modifications within the Aggregate

ashic

unread,
Oct 27, 2011, 5:37:12 AM10/27/11
to DDD/CQRS
Be careful when it comes to entities. DDD-Quickly approaches tend to
violate some important concepts regarding entities.

An Entity is something that has an Id.
A value object does NOT have any identifier - a change in any one
property causes it to become a different value object.
An aggregate is a collection on entity and value objects that are
ONLY accessed through an aggregate root.
An aggregate root (AR) is an entity through which the outside world
interacts with an aggregate.

An aggregate is a consistency boundary. All of the aggregate is loaded
and stored together.
Any entity may hold a reference to an AR,
No entity outside of an aggregate may hold a reference to any entity
within the aggregate that is not the AR.

Look at the last three lines again. Be careful about those collections
that "hang off" an aggregate. If you wish to load and save them
independently, then they're no longer part of the aggregate. Nobody
outside an aggregate is allowed to depend on the internal structure of
an aggregate (i.e. what entities it has and how they relate to each
other). All interaction with those "inner" entities are done via
operations on the AR.

In addition, an AR is an entity with a global id. Other entities
within an aggregate have a local id (i.e. an id that's unique only
within the aggregate).

On Oct 26, 6:14 pm, Michael Brown <mbr...@kharasoft.com> wrote:
> There are four concepts of importance here:
>
> Entity: a unique object within the domain that has significance (e.g. Customer) and can change over time
> Value Object: an immutable object within the domain that has no significance outside of its properties (e.g. date, address)
> Aggregate: a collection of entities or value objects that are related to each other through a root object
> Aggregate Root: An Entity that "owns" an Aggregate and serves as a gateway for all modifications within the Aggregate
>
> From: ddd...@googlegroups.com [mailto:ddd...@googlegroups.com] On Behalf Of Sarunas Mazylis
> Sent: Wednesday, October 26, 2011 12:09 PM
> To: ddd...@googlegroups.com
> Subject: Re: [DDD/CQRS] Difference between Aggregate and Entity
>
> @Colin: Thanks
>
> @All: Is Aggregate an entity? When entity is not an aggregate? Why difference is important?
>
> Thanks
>
> On Wed, Oct 26, 2011 at 7:05 PM, Colin Yates <colin.ya...@gmail.com<mailto:colin.ya...@gmail.com>> wrote:
> Entity has meaning (and therefore an id) defined outside of the state of its values as oppose to "value objects" whose identity is defined entirely by its state.
>
> Aggregate a structure of internally consistent and logically related "things" which might be entities or value objects.
>
> (I might be talking nonsense in terms of DDD so believe whoever writes next :))
>

Michael Brown

unread,
Oct 27, 2011, 1:28:16 PM10/27/11
to ddd...@googlegroups.com
"A value object does NOT have any identifier - a change in any one property causes it to become a different value object."
I disagree. I would argue that a value object cannot have its properties changed. You can copy a VO to a new one and change the new VO's values. But you cannot change a VO.
I think we are saying the same thing regarding Aggregates and Roots. You just stated it more explicitly than I had.

-----Original Message-----
From: ddd...@googlegroups.com [mailto:ddd...@googlegroups.com] On Behalf Of ashic
Sent: Thursday, October 27, 2011 5:37 AM
To: DDD/CQRS
Subject: [DDD/CQRS] Re: Difference between Aggregate and Entity

Be careful when it comes to entities. DDD-Quickly approaches tend to violate some important concepts regarding entities.

An Entity is something that has an Id.

Sarunas Mazylis

unread,
Oct 27, 2011, 1:36:01 PM10/27/11
to ddd...@googlegroups.com
@Ashic: Thanks for the good response. I had a feeling what is the difference there, but wanted to polish it against others' interpretations. It's interesting that in a blue book Aggregates are under "Life cycle of objects" chapter. Mostly transactional consistency gets emphasized when talking about aggregates though... 

ashic

unread,
Oct 28, 2011, 4:17:43 AM10/28/11
to DDD/CQRS
Yes...just wanted to clarify a few bits.
As for value object, I'm not saying that you can change a property -
I'm saying changing a property means it becomes a new one. For
example:

public class Rectangle
{
public readonly int width;
public readonly int height;
public Rectangle(int w, int h) {...}
public Rectangle SetWidth(int w) { return new Rectangle(w,
this.height);
}

Here's calling SetWidth doesn't change the existing rectangle. It
creates a new immutable rectangle. i.e. a rectangle with dimensions
(2,4) and another with dimensions (3,4) are different objects and you
cannot change a single property on one to become the other. By
"change" a meant logical difference, not mutation through code.

ashic

unread,
Oct 28, 2011, 4:18:59 AM10/28/11
to DDD/CQRS
The ownership and coupling elements are not less important that
consistency boundaries though.

Moon K

unread,
Mar 27, 2017, 6:38:34 AM3/27/17
to DDD/CQRS
Hi Colin,

I found this thread while searching on google. It's pretty old thread, but I've got a question.

Why is People (Person) an aggregate? Isn't this just an entity? 

andho

unread,
Mar 30, 2017, 4:05:28 AM3/30/17
to DDD/CQRS
Hi Moon K,

"Person" is both an Aggregate Root (global identity) and an Entity (identity, global or local). They are not mutually exclusive. The Aggregate Root concept in DDD is introduced so that we will think about which Entities are effected by a single Transaction and DESIGN our model so that this Transactional Boundary can be maintained by an Aggregate Root (which by itself is an Entity).

Regards,
Amjad

Moon K

unread,
Mar 31, 2017, 7:16:51 PM3/31/17
to DDD/CQRS
Hi andho,

Thank you for the reply.

Probably I have a miss understanding of what an aggregate is in DDD.

Person has string values. What kind of transactional boundary does it have? That's where I get confused.

It seems to me that many examples call an entity as an aggregate when there is no transactional boundary. 

Best,
Moon
Reply all
Reply to author
Forward
0 new messages