Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
DDD: encapsulación raíz agregada con JavaScript

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 } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!