Understanding Domain-Driven Design (DDD) with a Hospital Management Project
Let's walk through a concrete example using a hospital management system to understand how different layers interact with each other in the context of Domain-Driven Design (DDD).
Note: This content is currently under review and subject to updates as I continue to refine my understanding and gather more insights. Please consider this when reading, and feel free to contribute any suggestions or corrections you may have. Your feedback is highly appreciated and will help improve the accuracy and depth of this discussion.
Introduction to Domain-Driven Design (DDD)
Domain-Driven Design (DDD) is a software development approach focused on creating a rich understanding of the business domain (the problem space) and building software that reflects that understanding. The core idea of DDD is to break down complex problems by modeling software around the business domain.
DDD promotes a layered architecture, where each layer has a distinct responsibility, making the system easier to understand, develop, and maintain.
Let's walk through a concrete example using a hospital management system to understand how different layers interact with each other in the context of Domain-Driven Design (DDD).
Scenario: Scheduling a Patient Appointment
Key Components of DDD in a Hospital Management System: “Terminologies”:
Ubiquitous Language: Establishing a common language between developers, domain experts (e.g., doctors, nurses), and other stakeholders. This language should reflect the hospital's operations and terminology, ensuring everyone involved understands the system's functionality and requirements.
Ubiquitous Language: Establishing a common language between developers, domain experts (e.g., doctors, nurses), and other stakeholders. This language should reflect the hospital's operations and terminology, ensuring everyone involved understands the system's functionality and requirements.
Entities and Value Objects: Identifying entities like patients, doctors, and appointments, and value objects such as medical histories and treatment plans. These elements form the backbone of the domain model, capturing the essential characteristics and behaviors of the hospital's operations.
Aggregates: Grouping related entities and value objects into aggregates to ensure transactional consistency. For example, a patient's medical history might be an aggregate that includes their medical records, diagnoses, and treatments.
Repositories: persistence layer, whether it's a relational database, NoSQL database, or another storage solution.
Services: Encapsulating business logic that doesn't naturally fit within an entity or aggregate. Services might handle operations like scheduling appointments, generating bills, or coordinating patient transfers between departments.
Step-by-Step Workflow
Request Initiation
A patient wants to schedule an appointment with a doctor.
Interface Layer
The patient makes a request via an API call to the system, which captures necessary information (e.g., patient ID, desired date and time).
The PatientResolver handles this request and uses a DTO (Data Transfer Object) to structure the incoming data.
Application Layer
The PatientResolver calls a method in the AppointmentService.
The AppointmentService orchestrates the scheduling process by applying business rules.
Domain Layer
An
Appointment
entity is created, encapsulating the business logic for scheduling. This includes validating the appointment details, checking for conflicts, and updating the patient's schedule.
Infrastructure Layer
The
Appointment
entity is persisted using a repository, layer interacts with the database to store the appointment details.Response
The AppointmentService returns a success response, which is sent back to the PatientResolver.
The PatientResolver sends the response back to the patient via the API.
Diagram from : Resource book : Learning Domain-Driven Design -Aligning Software Architecture and Business Strategy
Explanation of the Diagram
Presentation Layer: This is the outermost layer where users interact with the system. It includes interfaces like web pages, mobile apps, or APIs. In the context of a Hospital Management System, this could be the patient portal, doctor dashboard, or administrative tools.
Application Layer: Acts as a bridge between the presentation layer and the domain layer. It contains business logic that doesn't belong to the domain layer but is still crucial for the application's operation. For example, it might contain logic for handling user authentication, authorization, and data validation.
Domain Layer: Represents the heart of the system, where the business domain logic resides. It defines entities, value objects, and aggregates that capture the core business concepts. In a Hospital Management System, this would include entities like
Patient
,Doctor
,Appointment
, andTreatment
.
Infrastructure Layer: Handles external concerns such as data access, messaging, and external service integration. It provides implementations of repositories, services, and other technical concerns that support the domain layer. For instance, it might include classes for interacting with databases, calling external APIs, or managing file storage.
Flow of Information
From Presentation to Application: User actions trigger events in the presentation layer, which are handled by the application layer. For example, a patient booking an appointment through the patient portal triggers a series of validations and business rules in the application layer.
From Application to Domain: The application layer delegates decisions and business logic to the domain layer.
For instance, scheduling an appointment involves checking the doctor's availability, which is a concern of the domain layer.
From Domain to Infrastructure: Once the domain layer has processed the request (e.g., validated the appointment details), it communicates with the infrastructure layer to persist data or perform other side effects. For example, saving the appointment details to a database.
Example Interaction of Layers
1. Interface Layer
PatientResolver:
Receives the API request for scheduling an appointment.
Uses AppointmentDTO to structure the incoming data.
Calls the
scheduleAppointment
method of AppointmentService.
AppointmentDTO:
Defines the structure of the data required to schedule an appointment.
2. Application Layer
AppointmentService:
Orchestrates the scheduling process.
Validates the input and applies business logic.
Calls the AppointmentRepository to save the appointment.

3. Domain Layer
Appointment:
Represents the core business entity for appointments.
Contains logic to validate the appointment data.
4. Infrastructure Layer
AppointmentRepository:
Handles the persistence of appointment data.
Uses the MongoDB schema to save the data.
MongoDB Schema:
Defines how appointment data is stored in MongoDB.
By following this structured approach, we ensure that the business logic is cleanly separated from technical details, making the system more maintainable and scalable. This way, changes in business rules only affect the Domain and Application layers, while changes in technical infrastructure only affect the Infrastructure layer. This separation of concerns is a key principle of Domain-Driven Design.