1738188042

Domain-Driven Design (DDD): concepts and examples


Domain-Driven Design (DDD) is an approach to software development that emphasizes deep understanding of the business domain and aligns the software model with real-world concepts. It was introduced by Eric Evans in his book Domain-Driven Design: Tackling Complexity in the Heart of Software. ## <br>Core Concepts of DDD DDD is based on a few fundamental principles: **1. Ubiquitous Language** A shared language between developers and domain experts that describes business concepts, reducing misunderstandings. **2. Bounded Context** A self-contained part of the system where a particular domain model is applied. It defines the boundaries within which terms and concepts have specific meanings. **3. Entities** Objects with a distinct identity that persists over time, even if their attributes change. Example in Python: ```py class Customer: def __init__(self, customer_id, name): self.customer_id = customer_id self.name = name def change_name(self, new_name): self.name = new_name ``` Even if the name changes, the `customer_id` remains the same. **4. Value Objects** Objects that describe characteristics but have no identity, meaning they are interchangeable if their values are the same. Example in Python: ```py from dataclasses import dataclass @dataclass(frozen=True) class Address: street: str city: str zip_code: str ``` Two addresses with the same values are considered identical. **5. Aggregates** A group of domain objects that are treated as a single unit, with one entity acting as the aggregate root. Example in Python: ```py class Order: def __init__(self, order_id, customer): self.order_id = order_id self.customer = customer self.items = [] def add_item(self, item): self.items.append(item) ``` The `Order` is the aggregate root, ensuring that modifications happen through it. **6. Repositories** Repositories provide an abstraction for retrieving and persisting aggregates. Example in Python using an in-memory repository: ```py class OrderRepository: def __init__(self): self.orders = {} def save(self, order): self.orders[order.order_id] = order def find_by_id(self, order_id): return self.orders.get(order_id) ``` **7. Domain Services** Services encapsulate business logic that doesn't naturally fit within an entity or value object. Example in Python: ```py class PaymentService: def process_payment(self, order, payment_details): # Payment processing logic here print(f"Processing payment for Order {order.order_id}") ``` **8. Application Services** These services orchestrate workflows, calling domain services and repositories but not containing business logic themselves. Example in Python: ```py class OrderApplicationService: def __init__(self, order_repository, payment_service): self.order_repository = order_repository self.payment_service = payment_service def place_order(self, customer, items): order = Order(order_id=123, customer=customer) for item in items: order.add_item(item) self.order_repository.save(order) self.payment_service.process_payment(order, "credit_card") ``` # <br>Example in PHP Using DDD principles in PHP: **Entity** ```php class Customer { private string $id; private string $name; public function __construct(string $id, string $name) { $this->id = $id; $this->name = $name; } public function changeName(string $newName): void { $this->name = $newName; } } ``` **Repository** ```php class CustomerRepository { private array $customers = []; public function save(Customer $customer): void { $this->customers[$customer->getId()] = $customer; } public function findById(string $id): ?Customer { return $this->customers[$id] ?? null; } } ``` ## <br>When to Use DDD - When working on complex business logic. - When multiple teams need a shared language to collaborate. - When the business rules and interactions evolve over time. ## <br>When to Avoid DDD - When working on small applications with minimal domain complexity. - When the project timeline is very short. - When the team lacks expertise in DDD, as it requires a learning curve. I'd like to see in the comments what these tests would look like applied to other languages such as Javascript, C++ or Luan. Participate

(3) Comments
Davidm8624
Davidm8624
1738192483

did u actually read that book or this concept just learned elsewhere?

amargo85
amargo85
1738219504

I've taken an interest in it and I'm studying up on it. Do you have any knowledge on this subject?

Davidm8624
Davidm8624
1738251529

no, not really. was just curious since it was mentioned at top


Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2025 © Chat-to.dev]