Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Entities

Tyler Brinks edited this page Jan 28, 2014 · 1 revision

Entities

Entities are objects contained within an aggregate that have their own thread of identity. In order to create entities you simply need to create them in response to changes in your domain model with a reference to the aggregate root instance.

Here's an abbreviated copy of the bank account sample in the source code.

public class BankAccount : AggregateRoot
{
    public readonly List<DebitCard> _cards = new List<DebitCard>();

    public void AddDebitCard(Guid cardId, string cardNumber)
    {
        ApplyEvent(new DebitCardAddedEvent(...));
    }

    public void OnDebitCardAdded(DebitCardAddedEvent evt)
    {
        _cards.Add(new DebitCard(this, evt.CardId, evt.CardNumber);
    }
}

public class DebitCard : Entity
{
    public DebitCard(AggregateRoot parent, Guid cardId, string cardNumber) : base(parent, cardId)
    {
        ....
    }
}

The most important aspects to note are

  1. The entity has its own ID. It's required in the base class' constructor.
  2. The entity requires a reference to the aggregate root, also in the base class' constructor.

Entities require a reference to the aggregate because it's the aggregate that handles all events. Even when an Entity fires an event, it's run through the aggregate. That means events are persisted in same order and location as any other domain event. Meanwhile your entity can maintain a unique identity.

Clone this wiki locally