Building a Domain Events Subject

What happens when we say, aggregate.addDomainEvent(event: IDomainEvent) from an Aggregate?

Here's the base AggregateRoot class that we're using in DDDForum.

// shared/domain/AggregateRoot.ts

import { Entity } from "./Entity"
import { IDomainEvent } from "./events/IDomainEvent"
import { DomainEvents } from "./events/DomainEvents"
import { UniqueEntityID } from "./UniqueEntityID"

export abstract class AggregateRoot<T> extends Entity<T> {
  /**
   * All of the domain events for a subclass of AggregateRoot<T>
   * get added to this private array.
   */

  private _domainEvents: IDomainEvent[] = []

  get id(): UniqueEntityID {
    return this._id
  }

  get domainEvents(): IDomainEvent[] {
    return this._domainEvents
  }

  /**
   * @method addDomainEvent
   * @protected
   * @desc Called by a subclass in order to add a Domain Event
   * to the list of Domain Events currently on this aggregate
   * within a transactional boundary. Also notifies the DomainEvents
   * subject that the current aggregate has at least one Domain Event
   * that we will need to publish if the transaction completes.
   */

  protected addDomainEvent(domainEvent: IDomainEvent): void {
    // Add the domain event to this aggregate's list of domain events
    this._domainEvents.push(domainEvent)
    // Add this aggregate instance to the domain event's list of aggregates who's
    // events it eventually needs to dispatch.
    DomainEvents.markAggregateForDispatch(this)
    // Log the domain event
    this.logDomainEventAdded(domainEvent)
  }

  public clearEvents(): void {
    this._domainEvents.splice(0, this._domainEvents.length)
  }

  private logDomainEventAdded(domainEvent: IDomainEvent): void {
    const thisClass = Reflect.getPrototypeOf(this)
    const domainEventClass = Reflect.getPrototypeOf(domainEvent)
    console.info(
      `[Domain Event Created]:`,
      thisClass.constructor.name,
      "==>",
      domainEventClass.constructor.name
    )
  }
}

So the real magic that happens with this subclass is within the addDomainEvent() method. Not only do we add the IDomainEvent to a list of Domain Events currently on the Aggregate for the transaction, but we notify the DomainEvents subject that the current Aggregate should be marked for dispatch. This means that when the transaction for this Aggregate completes, we should publish the Domain Events attached to the Aggregate

Howdy 👋

This is an online wiki about the main topics over the last 40 years of software design, architecture & testing. It was created by Khalil Stemmler.

This wiki's mission is to help eliminate the unknown unknowns, helping you get up to speed on the best practices and principles to write testable, flexible, and maintainable code.

I created this wiki while I was connecting the dots on my own path. You can read more about that learning journey in the introduction

Already bought it?

If you’ve already purchased the book, click here to re-send your link. You can read the online wiki or download a copy of the book in PDF, EPUB, and Kindle versions.

Want access?

You can read the intro to the book for free and visit solidbook.io to buy the book/wiki! To get an idea of my writing, read some of my best free content here and here.

Need help?

Something not working? Have a question? You can reach me on at khalil@khalilstemmler.com.