DEV Community

Cover image for TypeScript for Domain-Driven Design (DDD)
Shafayet Hossain Yashfi
Shafayet Hossain Yashfi

Posted on

7 1 1 1

TypeScript for Domain-Driven Design (DDD)

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.

Understanding Domain-Driven Design

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

  • Entities: Objects with a unique identity.
  • Value Objects: Immutable objects defined by their attributes.

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.

Why TypeScript Fits DDD

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.

Practical Implementation

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 };
  }
}
Enter fullscreen mode Exit fullscreen mode



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);
  }
}
Enter fullscreen mode Exit fullscreen mode



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;
  }
}
Enter fullscreen mode Exit fullscreen mode



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);
  }
}
Enter fullscreen mode Exit fullscreen mode



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.`);
}
Enter fullscreen mode Exit fullscreen mode



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
  }
}
Enter fullscreen mode Exit fullscreen mode

7. Working with Bounded Contexts

Leverage TypeScript's modular capabilities to isolate bounded contexts.

  • Use separate directories for each context.
  • Explicitly define interfaces for cross-context communication.

Example structure:

/src
  /sales
    - Product.ts
    - Order.ts
    - ProductRepository.ts
  /inventory
    - Stock.ts
    - StockService.ts
  /shared
    - DomainEvent.ts
Enter fullscreen mode Exit fullscreen mode

Advanced Features

Conditional Types for Flexible Modeling

type Response<T> = T extends "success" ? { data: any } : { error: string };
Enter fullscreen mode Exit fullscreen mode

Template Literal Types for Validation

type Currency = `${"USD" | "EUR" | "GBP"}`;
Enter fullscreen mode Exit fullscreen mode

My personal website: https://shafayet.zya.me


Well, it shows that how active you're in Git-toilet...

Image description


Cover Image was made by using OgImagemaker by

@eddyvinck .Thanks man for gifting us that tool🖤🖤🖤...

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (1)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay