This project is a TypeScript implementation of the Domain-Driven Design (DDD) Value Object pattern. It provides a set of reusable, immutable value objects and a base class to create new ones. The focus is on ensuring type safety data integrity through validation, and reliable value-based equality.
- Purpose: The cornerstone of the pattern. It provides a generic
equals()method that compares two value objects based on their constituent parts. - Key Method:
getEqualityComponents(): any[]. Each concrete class must implement this method to return an array of its properties that define its identity. Theequals()method uses this array for comparison.
The project contains several examples of value objects:
Email(src/domain/email.vo.ts): A simple value object representing a validated and normalized email address.Amount(src/domain/amount.vo.ts): Represents a monetary value usingBigNumber.jsto avoid floating-poi inaccuracies. It enforces non-negativity and provides methods for safe arithmetic (add,times).Currency(src/domain/currency.vo.ts): A type-safe object representing a currency code (e.g., "USD", "EUR based on a predefined list.Money(src/domain/money.vo.ts): A composite value object that combinesAmountandCurrency. It demonstrates how value objects can be composed and include business logic (e.g., preventing addition of different currencies).
src/app.ts: A demonstration file to show how to create and compare value objects.src/domain/: Contains all DDD domain-layer constructs.abstractions/: Home to the baseValueObjectclass.types/: Contains shared type definitions, likeCurrencyCode.*.vo.ts: Concrete value object implementations.
- Language: TypeScript
- Runtime: Node.js
- Package Manager: pnpm
- Core Logic:
- Immutability is key. Constructors are private, and creation is handled by static
create()methods. - Validation is performed within the
create()method, throwing an error for invalid data. BigNumber.jsis used for all monetary calculations to ensure precision.
- Immutability is key. Constructors are private, and creation is handled by static
- Development Scripts:
pnpm dev: Runs the application in watch mode usingtsx.pnpm build: Compiles TypeScript to JavaScript in thedistdirectory.pnpm start: Builds and runs the compiled application.
Directive: The LLM agent for this project acts as an architectural consultant and pair programmer, not as an automated code modifier. The primary goal of the interaction is to discuss architecture, design patterns, and best practices to arrive at a well-reasoned solution, which the user will then implement manually.
Rules of Engagement:
- Source Code Modification: The LLM must not use tools (
write_file,replace) to modify source code files (e.g.,.ts,.json,.sql). All suggestions for code changes must be provided as text or code snippets in the chat response. - Documentation File Modification: The LLM may only use file modification tools (
write_file,replace) for documentation and context files (e.g.,.gemini.md,README.md), and only when explicitly instructed to do so by the user. - Shell Commands: The LLM should avoid running shell commands (
run_shell_command) unless explicitly asked, especially for commands that modify the file system or git state.