Sobes.tech
Junior — Middle

Can you explain the concept of domain-driven development?

sobes.tech AI

Answer from AI

Development focused on the domain model (Domain-Driven Design, DDD) emphasizes a deep understanding of the subject area and building software around the model of this domain. The main idea is to create a clear, expressive model that reflects business logic, which becomes the foundation for the code.

Key elements in DDD include:

  • Entities — objects with a unique identity important to the system.
  • Value Objects — objects without identity, characterized by properties.
  • Aggregates — groups of related objects considered as a single unit.
  • Repositories — interfaces for accessing aggregates.
  • Services — operations that do not belong to specific entities.

Example in Python (simplified):

from dataclasses import dataclass

@dataclass(frozen=True)
class Money:
    amount: float
    currency: str

class Account:
    def __init__(self, account_id: str, balance: Money):
        self.account_id = account_id
        self.balance = balance

    def deposit(self, money: Money):
        if money.currency != self.balance.currency:
            raise ValueError("Currency mismatch")
        self.balance = Money(self.balance.amount + money.amount, self.balance.currency)

# Repository for account access
class AccountRepository:
    def __init__(self):
        self._storage = {}

    def save(self, account: Account):
        self._storage[account.account_id] = account

    def get(self, account_id: str) -> Account:
        return self._storage.get(account_id)

Thus, DDD helps create code that reflects business logic and simplifies system maintenance and development.