DEV Community

Shelner
Shelner

Posted on

DDD (Domain-Driven Design) tutorial

What is Domain-Driven Design (DDD)?

Domain-Driven Design (DDD) is a way to design software by focusing on the real-world problem you're trying to solve (called the domain).

Instead of starting with code or databases, you first try to understand the business rules and talk to experts in that area. Then you build your code to match that understanding.

Key Ideas in Simple Terms

1. Domain

The domain is the real-world area your app is about.
For example:

  • In an e-commerce app: the domain is "shopping"
  • In a hospital system: the domain is "healthcare" #### 2. Ubiquitous Language Everyone (developers, managers, clients) should use the same worlds to describe things. For example, say "Order", "Product", or "Customer" - not different technical names. #### 3. Entities These are things in your app that have an identity. Example: A User or Order - even if their details change, they are still the same user or order. #### 4. Value Objects These are small objects that have to identity. They are just values. Example: An address (street, city, zip). If two address have the same data, they are equal. #### 5. Aggregates A group of objects that belong together, and you treat them as one unit. Example: An Order and Order Items from one aggregate. #### 6. Repositories These are like libraries for getting and saving objects (like from a database). For example, a UserRepository might help you find users by ID. #### 7. Services If a business rule doesn't belong to one specific object, you can put it in a domain service. Example: PaymentService to handle payment logic. #### 8. Bounded Context Large systems are broken into smaller parts (contexts). Each part has its own models and rules, even if they use the same words. Example: The word "Order" might mean different things in "Shipping" and "Billing" contexts.

Why use DDD?

  • It helps write code that matches the business logic.
  • It makes your app easier to understand, change, and maintain.
  • It improves communication between developers and business people.

Top comments (0)