Domain-Driven Design (DDD) is a powerful approach for tackling complex software systems by focusing on the core business domain and its associated logic. TypeScript, with its strong typing and modern features, is an excellent tool to implement DDD concepts effectively. This article explores the synergy between TypeScript and DDD, offering practical insights, strategies, and examples to bridge the gap between design and code.
Core Concepts
1. Ubiquitous Language
Collaboration between developers and domain experts using a shared language to reduce miscommunication.
2. Bounded Contexts
Clear separation of different parts of the domain, ensuring autonomy and clarity within specific contexts.
3. Entities and Value Objects
4. Aggregates
Clusters of domain objects treated as a single unit for data changes.
5. Repositories
Abstracts the persistence logic, providing access to aggregates.
6. Domain Events
Signals emitted when significant actions occur within the domain.
7. Application Services
Encapsulate business workflows and orchestration logic.
1. Static Typing: Strong type checking helps model domain logic explicitly.
2. Interfaces: Enforce contracts between components.
3. Classes: Represent entities, value objects, and aggregates naturally.
4. Type Guards: Ensure type safety at runtime.
5. Utility Types: Enable powerful type transformations for dynamic domains.
1. Modeling Entities
Entities have unique identities and encapsulate behavior.
class Product { constructor( private readonly id: string, private name: string, private price: number ) {} changePrice(newPrice: number): void { if (newPrice <= 0) { throw new Error("Price must be greater than zero."); } this.price = newPrice; } getDetails() { return { id: this.id, name: this.name, price: this.price }; } }
2. Creating Value Objects
Value Objects are immutable and compared by value.
class Money { constructor(private readonly amount: number, private readonly currency: string) { if (amount < 0) { throw new Error("Amount cannot be negative."); } } add(other: Money): Money { if (this.currency !== other.currency) { throw new Error("Currency mismatch."); } return new Money(this.amount + other.amount, this.currency); } }
3. Defining Aggregates
Aggregates ensure data consistency within a boundary.
class Order { private items: OrderItem[] = []; constructor(private readonly id: string) {} addItem(product: Product, quantity: number): void { const orderItem = new OrderItem(product, quantity); this.items.push(orderItem); } calculateTotal(): number { return this.items.reduce((total, item) => total + item.getTotalPrice(), 0); } } class OrderItem { constructor(private product: Product, private quantity: number) {} getTotalPrice(): number { return this.product.getDetails().price * this.quantity; } }
4. Implementing Repositories
Repositories abstract data access.
interface ProductRepository { findById(id: string): Product | null; save(product: Product): void; } class InMemoryProductRepository implements ProductRepository { private products: Map<string, Product> = new Map(); findById(id: string): Product | null { return this.products.get(id) || null; } save(product: Product): void { this.products.set(product.getDetails().id, product); } }
5. Using Domain Events
Domain Events notify the system of state changes.
class DomainEvent { constructor(public readonly name: string, public readonly occurredOn: Date) {} } class OrderPlaced extends DomainEvent { constructor(public readonly orderId: string) { super("OrderPlaced", new Date()); } } // Event Handler Example function onOrderPlaced(event: OrderPlaced): void { console.log(`Order with ID ${event.orderId} was placed.`); }
6. Application Services
Application services coordinate workflows and enforce use cases.
class OrderService { constructor(private orderRepo: OrderRepository) {} placeOrder(order: Order): void { this.orderRepo.save(order); const event = new OrderPlaced(order.id); publishEvent(event); // Simulated event publishing } }
Leverage TypeScript's modular capabilities to isolate bounded contexts.
Example structure:
class Product { constructor( private readonly id: string, private name: string, private price: number ) {} changePrice(newPrice: number): void { if (newPrice <= 0) { throw new Error("Price must be greater than zero."); } this.price = newPrice; } getDetails() { return { id: this.id, name: this.name, price: this.price }; } }
Conditional Types for Flexible Modeling
class Money { constructor(private readonly amount: number, private readonly currency: string) { if (amount < 0) { throw new Error("Amount cannot be negative."); } } add(other: Money): Money { if (this.currency !== other.currency) { throw new Error("Currency mismatch."); } return new Money(this.amount + other.amount, this.currency); } }
Template Literal Types for Validation
class Order { private items: OrderItem[] = []; constructor(private readonly id: string) {} addItem(product: Product, quantity: number): void { const orderItem = new OrderItem(product, quantity); this.items.push(orderItem); } calculateTotal(): number { return this.items.reduce((total, item) => total + item.getTotalPrice(), 0); } } class OrderItem { constructor(private product: Product, private quantity: number) {} getTotalPrice(): number { return this.product.getDetails().price * this.quantity; } }
My personal website: https://shafayet.zya.me
Well, it shows that how active you're in Git-toilet...
Cover Image was made by using OgImagemaker by
@eddyvinck .Thanks man for gifting us that tool???...
The above is the detailed content of TypeScript for Domain-Driven Design (DDD). For more information, please follow other related articles on the PHP Chinese website!