AggregateRoot

Represents an aggregate root. An aggregate root is an entity that represents a meaningful concept in the domain. It is the root of an aggregate, which is a cluster of domain objects that can be treated as a single unit.

  
abstract class AggregateRoot<EventBase extends IEvent = IEvent> {
  autoCommit: boolean
  publish<T extends EventBase = EventBase>(event: T)
  publishAll<T extends EventBase = EventBase>(events: T[])
  commit()
  uncommit()
  getUncommittedEvents(): EventBase[]
  loadFromHistory(history: EventBase[])
  apply<T extends EventBase = EventBase>(event: T, optionsOrIsFromHistory: boolean | { fromHistory?: boolean; skipHandler?: boolean; } = {}): void
  protected getEventHandler<T extends EventBase = EventBase>(event: T): Type<IEventHandler> | undefined
  protected getEventName(event: any): string
}

Properties

Property Description
autoCommit: boolean

Gets whether the aggregate root should automatically commit events.

Methods

publish()

Publishes an event. Must be merged with the publisher context in order to work.


publish<T extends EventBase = EventBase>(event: T)

Parameters

Option Type Description
event T

The event to publish.

publishAll()

Publishes multiple events. Must be merged with the publisher context in order to work.


publishAll<T extends EventBase = EventBase>(events: T[])

Parameters

Option Type Description
events T[]

The events to publish.

commit()

Commits all uncommitted events.


commit()

Parameters

There are no parameters.

uncommit()

Uncommits all events.


uncommit()

Parameters

There are no parameters.

getUncommittedEvents()

Returns all uncommitted events.


getUncommittedEvents(): EventBase[]

Parameters

There are no parameters.

Returns

EventBase[] All uncommitted events.

loadFromHistory()

Loads events from history.


loadFromHistory(history: EventBase[])

Parameters

Option Type Description
history EventBase[]

The history to load.

apply()

Applies an event. If auto commit is enabled, the event will be published immediately (note: must be merged with the publisher context in order to work). Otherwise, the event will be stored in the internal events array, and will be published when the commit method is called. Also, the corresponding event handler will be called (if exists). For example, if the event is called UserCreatedEvent, the "onUserCreatedEvent" method will be called.


apply<T extends EventBase = EventBase>(event: T, isFromHistory?: boolean): void

Parameters

Option Type Description
event T

The event to apply.

isFromHistory boolean

Whether the event is from history.


Optional. Default is `undefined`.

Returns

void

Applies an event. If auto commit is enabled, the event will be published immediately (note: must be merged with the publisher context in order to work). Otherwise, the event will be stored in the internal events array, and will be published when the commit method is called. Also, the corresponding event handler will be called (if exists). For example, if the event is called UserCreatedEvent, the "onUserCreatedEvent" method will be called.


apply<T extends EventBase = EventBase>(event: T, options?: { fromHistory?: boolean; skipHandler?: boolean; }): void

Parameters

Option Type Description
event T

The event to apply.

options object

The options.


Optional. Default is `undefined`.

Returns

void

getEventHandler()


protected getEventHandler<T extends EventBase = EventBase>(event: T): Type<IEventHandler> | undefined

Parameters

Option Type Description
event T

Returns

Type<IEventHandler> | undefined

getEventName()


protected getEventName(event: any): string

Parameters

Option Type Description
event any

Returns

string