Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

37 lines (30 sloc) 1.23 KB
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> {
private _domainEvents: IDomainEvent[] = [];
get id (): UniqueEntityID {
return this._id;
}
get domainEvents(): IDomainEvent[] {
return this._domainEvents;
}
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)
}
}
You can’t perform that action at this time.