Last active
May 1, 2022 14:42
-
-
Save alexsoyes/2f85e125614d98d8def26fe1a8dddceb to your computer and use it in GitHub Desktop.
Agregate Root demo with invariants.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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