Skip to content

Instantly share code, notes, and snippets.

@alexsoyes
Last active May 1, 2022 14:42
Show Gist options
  • Save alexsoyes/2f85e125614d98d8def26fe1a8dddceb to your computer and use it in GitHub Desktop.
Save alexsoyes/2f85e125614d98d8def26fe1a8dddceb to your computer and use it in GitHub Desktop.
Agregate Root demo with invariants.
/**
* Entity
*/
class Line {
constructor(private description: string, private amount: number, private vat: number) { }
public isEmpty(): boolean {
return this.description === '' && this.amount === 0 && this.vat === 0;
}
}
/**
* Aggregate Root
*/
class Bill {
private lines: Line[] = [];
public addLine(): void {
const previousLinesProperlyFilled = this.lines.every(line => !line.isEmpty());
if (previousLinesProperlyFilled) {
this.lines.push(new Line('', 0, 0));
} else {
throw new Error('You must fill all the previous lines before adding a new one');
}
}
}
const bill = new Bill();
bill.addLine();
bill.addLine(); // "Executed JavaScript Failed:" You must fill all the previous lines before adding a new one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment