Skip to content

Instantly share code, notes, and snippets.

@biomousavi
Last active January 29, 2025 08:55
Show Gist options
  • Save biomousavi/99e5cb98834421c594f884cf69f08cc2 to your computer and use it in GitHub Desktop.
Save biomousavi/99e5cb98834421c594f884cf69f08cc2 to your computer and use it in GitHub Desktop.
DDD Concepts for begginers

DDD Domain Driven Design

  • The primary goal of DDD is to map real-world systems or processes into software artifacts

Domain:

domain typically refers to the business logic and rules.

  • problem that the project is being developed for, in a e-commerce domains are sales, customer management.

Ubiquitous Language (UL):

terms and definitions used by the entire team, both technical and nontechnical.

  • Ubiquitous Language means to create a language that words and phrases from domain used in the codebase, language that can be understood by business experts and developers

Bounded Context:

Bounded Contexts are used to manage large domain models by dividing them into distinct sections.

  • This approach groups similar components and concepts minimizing confusion
Examples:
  1. E-commerce System

    • Order Management Context: Handles orders, payment processing, and shipping details. Concepts like Order, Payment, and Shipment are grouped here.
    • Product Catalog Context: Manages products, categories, and inventory. Concepts like Product, Category, and Stock belong here.
    • User Management Context: Deals with user accounts, authentication, and profiles. Concepts like User, Role, and Permission are grouped here.
  2. Healthcare System

    • Patient Management Context: Handles patient records, appointments, and histories. Concepts like Patient, MedicalHistory, and Appointment are part of this context.
    • Billing Context: Manages invoices, payments, and insurance claims. Concepts like Invoice, Payment, and Insurance are grouped here.
    • Diagnostics Context: Focuses on lab tests and results. Concepts like LabTest and Result are grouped here.
  3. Banking System

    • Account Management Context: Deals with accounts, balances, and transactions. Concepts like Account, Transaction, and Balance are grouped here.
    • Loan Management Context: Manages loans, repayments, and interest rates. Concepts like Loan, Repayment, and Interest belong to this context.
    • Fraud Detection Context: Handles monitoring and identifying fraudulent activities. Concepts like FraudAlert and RiskScore are grouped here.

By clearly dividing these contexts, each team can work independently without creating conflicts or confusion around shared terms like User or Account. Let me know if you'd like more examples or details!

  • It is the biggest zone/area/place where a term has a consistent meaning (it means the same thing for the business specialists and the developers).

DDD Layers

Domain

domain layer contains the business logic and rules that define the application's behavior.

  • this layer is where abstractions are made, the design of interfaces are included in the domain layer.
  • An important factor is that objects do not have too many technical details.
  • should be written using the ubiquitous language

Application

application layer coordinates the interactions between the domain and infrastructure layers.

  • It is the layer where business process flows are handled
  • Could also be seen as the service layer of your application

Infrastructure

infrastructure layer provides the necessary services and infrastructure for the application, such as databases, message queues, and web servers.

  • The infrastructure layer is responsible for communication with external websites, access to data (persistence storage) or to other resources.

User Interface (UI)

UI layer provides the interface for users to interact with the application.

  • This layer is the part where interaction with external world happens.
  • it can be a cli, website...
One of the key features of Layered Architecture is that the dependencies between layers are inward-facing. This means that higher-level layers can only depend on lower-level layers. For example, the domain layer can only depend on other code within the domain layer. The application layer can depend on the domain layer and other code within the application layer, but it cannot depend on the UI layer.

Aggregate:

group of objects that treated as a single unit, all the object are consistent

  • For example, an Order Line object doesn't make sense without an Order to belong to, so we say that the Order is the aggregate root.

  • Aggregates focus on enforcing business rules and ensuring that all interactions with the domain happen in a controlled way through the Aggregate Root. This avoids inconsistencies and ensures invariants are always respected.

Examples
1. E-commerce System
  • Aggregate: Order
    • Aggregate Root: Order
    • Components: OrderItem, ShippingAddress, PaymentDetails
    • Description: The Order is the Aggregate Root, and it ensures consistency, such as verifying that all OrderItems belong to the same order and the payment is correctly linked. External systems interact only with the Order object to modify or query the order.
2. Banking System
  • Aggregate: Account
    • Aggregate Root: Account
    • Components: Transaction, AccountHolder
    • Description: The Account is the Aggregate Root that enforces rules, such as ensuring transactions do not result in a negative balance unless an overdraft is allowed.

Entity:

Entities are object that have unique identifier and lifecycle that are mutable and not equal to each other, like a user record in DB

Data transfer object (DTO):

is an object that carries data between processes.

    • DTOs are simple objects that should not contain any business logic
    • but may contain serialization and deserialization mechanisms for transferring data over the wire.

Value Objects:

are Immutable and not have unique identifier

Repositories:

used to process and retrieve aggregate, abstraction over data access

Services:

encapsulate domain logic that does not belong to a specific entity

Factory:

used to encapsulated a complex object

Event:

used to communicate and capture domain specific information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment