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 and book about the basics of software design and architecture with TypeScript by Khalil Stemmler, Developer Advocate @ Apollo GraphQL .

This book’s mission is to teach developers the essential skills and practices to write testable, flexible, and maintainable code.

You can read more about the learning journey in the "Software Design and Architecture Roadmap 🖼️".

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 (it's currently on pre-sale for 33% off)! For recent updates, click here. 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 Twitter or khalil@khalilstemmler.com.