No puedo encontrar ningún ejemplo claro de cómo implementar Aggregate Root (DDD) con su estado interno encapsulado correctamente con la ayuda de JavaScript/TypeScript. A continuación sugiero una idea sobre cómo implementarlo. Tal vez alguien tenga ideas sobre cómo mejorar esta solución:
import cloneDeep from 'clone-deep'; import { deepFreeze } from '../utils'; // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze export class AggregateRoot<TEntityProps> { // TODO EntityId protected readonly props: TEntityProps constructor(props: TEntityProps) { /** Encapsulate aggregate's state from the outside world */ this.props = cloneDeep<TEntityProps>(props) } /** Extract the state from an aggregate. * * Aggregate becomes unchangeable (read-only) after calling the flush method. * */ public flush(): TEntityProps { return deepFreeze(this.props) } } // Usage export class User extends AggregateRoot<UserProps> { constructor(props: UserProps) { super(props) // props validation here } get firstName() { return this.props.firstName } get lastName() { return this.props.lastName } get updatedAt() { return this.props.updatedAt } }Otra variante es reescribir este método:
public flush(): TEntityProps { return deepFreeze(this.props) }con
public snapshot(): TEntityProps { return cloneDeep(this.props) }Pero debemos saber que, en este caso, aumentamos el consumo de memoria al doble. Así que probablemente sea adecuado para agregados pequeños, creo.