@bitloops/bl-boilerplate-core
Version:
TypeScript boilerplate code for Bitloops Language generated projects
52 lines (51 loc) • 1.99 kB
JavaScript
/**
* ISC License
* Copyright (c) 2019, [Khalil Stemmler](https://khalilstemmler.com)
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
import { Entity } from './Entity';
export class AggregateRoot extends Entity {
_domainEvents = [];
_aggregateVersion;
get id() {
return this._id;
}
get aggregateVersion() {
return this._aggregateVersion;
}
set aggregateVersion(version) {
this._aggregateVersion = version;
}
get domainEvents() {
return this._domainEvents;
}
addDomainEventClass(domainEventClass) {
const domainEvent = new domainEventClass(this);
this.addDomainEvent(domainEvent);
}
addDomainEvent(domainEvent) {
// Add the domain event to this aggregate's list of domain events
this._domainEvents.push(domainEvent);
// Log the domain event
this.logDomainEventAdded(domainEvent);
}
clearEvents() {
this._domainEvents.splice(0, this._domainEvents.length);
}
logDomainEventAdded(domainEvent) {
const thisClass = Reflect.getPrototypeOf(this);
const domainEventClass = Reflect.getPrototypeOf(domainEvent);
console.info(`[Domain Event Created]:`, thisClass?.constructor?.name, '==>', domainEventClass?.constructor?.name);
}
}