Lädt...


🔧 TypeScript for Domain-Driven Design (DDD)


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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 };
  }
}



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
  }
}

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

Advanced Features

Conditional Types for Flexible Modeling

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

Template Literal Types for Validation

type Currency = `${"USD" | "EUR" | "GBP"}`;

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🖤🖤🖤...

...

🔧 trong mo hinh ddd cac domain lien ket voi anhu the nao


📈 28.67 Punkte
🔧 Programmierung

📰 softwarearchitektur.tv: Domain Prototyping – Iterative Entwicklung mit DDD


📈 28.67 Punkte
📰 IT Nachrichten

🔧 Tactical Design Patterns in DDD: Modeling Complex Domains Effectively


📈 25.35 Punkte
🔧 Programmierung

📰 heise-Angebot: Heise-Workshops: Fachliches API-Design mit DDD und Einstieg in Webanwendungen


📈 25.35 Punkte
📰 IT Nachrichten

🔧 Mastering NestJS: Unleashing the Power of Clean Architecture and DDD in E-commerce Development — part 1


📈 20.26 Punkte
🔧 Programmierung

🔧 Como funciona o DDD e a Clean Architecture - De maneira simples para inciantes .


📈 20.26 Punkte
🔧 Programmierung

📰 DDD-DOOM angespielt: Ab durch die Hölle


📈 20.26 Punkte
📰 IT Nachrichten

🔧 DDD and Microservices


📈 20.26 Punkte
🔧 Programmierung

🔧 Digging Deep to Find the Right Balance Between DDD, Clean and Hexagonal Architectures


📈 20.26 Punkte
🔧 Programmierung

📰 DDD-DOOM angespielt: Ab durch die Hölle


📈 20.26 Punkte
📰 IT Nachrichten

🔧 Architecture Approach: Domain-Driven Design (DDD)


📈 20.26 Punkte
🔧 Programmierung

🔧 10 Things To Avoid in Domain-Driven Design (DDD)


📈 20.26 Punkte
🔧 Programmierung

🔧 Arquitetura de Software para devs: MVC, Hexagonal, DDD


📈 20.26 Punkte
🔧 Programmierung

🔧 Unraveling the Complexities of Software Design with Domain-Driven Design (DDD)


📈 20.26 Punkte
🔧 Programmierung

🔧 Domain-Driven Design (DDD) in AWS. Find Your Business Domains.


📈 20.26 Punkte
🔧 Programmierung

🔧 Introduction to Domain-Driven Design (DDD) in C#


📈 20.26 Punkte
🔧 Programmierung

🔧 Ddd Python


📈 20.26 Punkte
🔧 Programmierung

📰 software-architektur.tv: Funktionale Programmierung und DDD mit Mike Sperber


📈 20.26 Punkte
📰 IT Nachrichten

🔧 Introduction to Domain-Driven Design (DDD) in C# with Microsoft Orleans


📈 20.26 Punkte
🔧 Programmierung

🔧 DDD da massa


📈 20.26 Punkte
🔧 Programmierung

🔧 DDD : Authentication and Authorisation, How to achieve?


📈 20.26 Punkte
🔧 Programmierung

🔧 Updating Audit Columns in Aggregate Roots and Child Entities with EF Core and DDD


📈 20.26 Punkte
🔧 Programmierung

🔧 DDD da massa


📈 20.26 Punkte
🔧 Programmierung

🔧 Using DDD with Recoil


📈 20.26 Punkte
🔧 Programmierung

🔧 Heroes of DDD: Software Developer == business partner?


📈 20.26 Punkte
🔧 Programmierung

🔧 DDD Wales, 2018


📈 20.26 Punkte
🔧 Programmierung

🔧 Domain Driven Design (DDD) with stakeholders: using content production contracting as an example


📈 20.26 Punkte
🔧 Programmierung

🔧 TypeScript for Domain-Driven Design (DDD)


📈 20.26 Punkte
🔧 Programmierung

📰 Neue Training-Events der Entwickler-Akademie: DDD, IT Security, ML & Python


📈 20.26 Punkte
📰 IT Security Nachrichten

🔧 Heroes of DDD: BEHAVING perspective. What do I do?


📈 20.26 Punkte
🔧 Programmierung

🔧 Onion Architecture in Domain-Driven Design (DDD)


📈 20.26 Punkte
🔧 Programmierung

📰 Was ist Domain-Driven Design (DDD)?


📈 20.26 Punkte
📰 IT Nachrichten

🔧 Domain Driven Design (DDD) Practice: Live Streaming App Example


📈 20.26 Punkte
🔧 Programmierung

🐧 DDD tips and tricks


📈 20.26 Punkte
🐧 Linux Tipps

matomo