Skip to content

Events in nested entities #10

@darbio

Description

@darbio

Using this library, how do you suggest handling events in child entities?

For example, in the following (made up) banking domain, a bank account (aggregate root) is registered in a person’s name (entity) and can have multiple bank cards (entities).

class BankAccount {
    public IEnumerable<BankCard> Cards { get; }
    public Person Owner { get; }
    
    public void ChangeOwnerName() {
        this.Owner.SetName(...);
    }

    public void CancelBankCard(id) {
        ... get card by id
        card.Cancel();
    }
}

class BankCard {
    ...
    public void Cancel() {
        ... raise event 
    }
}

class Person {
    ...
    public void ChangeName() {
        ... raise event
    }
}

When a person changes their name, the entity raises an event to say that they have changed their name.

When a bank card is cancelled, the bank card entity raises an event to say it has been cancelled.

Should these bubble up to the root? Or should the root know how to apply events to an entity? If so, how does the root know such entity to apply this to?

I’ve seen other libraries suggest that all events remain on the aggregate root (e.g. BankAccountCardCancelledEvent), others on both (CardCancelledEvent on the card, BankAccountCardCancelledEvent on root), and others using routing from the root to the entity.

Any tips?

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions