You are on page 1of 7

BY BIPRO

Clean Code Architecture

Onion Structure

Inspection Every Layer


Dependency flow:

❖ The presentation layer depends on the Application Layer.


❖ Application Layer depends on the Domain Layer.
❖ Infrastructure Layer depends on both Application and Domain Layers.

Let's break down each layer and discuss the types of implementations and folder structures
commonly associated with them:

Domain Layer:

Purpose: Represents the core business logic and rules of the application. It contains entities,
value objects, and the business logic that operates on them.

Implementations:

❖ Entities: Represent core business objects.


❖ Value Objects: Immutable objects represent a descriptive domain aspect with no
conceptual identity.
❖ Repositories: Interfaces specifying methods for retrieving and storing entities.
(Repositories abstract the details of data storage, allowing the application to work with
domain entities without knowing how or where the data is persisted.)
❖ Services: Classes containing domain-specific business logic.

Folder Structure:

/src
/domain
/entities
/value_objects
/repositories
/services
Application Layer:

Purpose: Orchestrates the data flow between the Presentation, Domain, and Infrastructure layers.
It contains use cases and application-specific business rules.

Implementations:

❖ Use Cases (Interactors): Classes defining application-specific business rules by


orchestrating calls to entities and services in the domain layer.
❖ DTOs (Data Transfer Objects): Objects to transfer data between layers.

Folder Structure:

/src
/application
/use_cases
/dtos

Infrastructure Layer:

Purpose: Contains implementations for external concerns such as databases, APIs, and
frameworks. This layer is where the application communicates with external services.

Implementations:

❖ Database Access: Implementations of repository interfaces from the domain layer.


❖ Frameworks: External libraries and frameworks.
❖ External Services: Implementations for interacting with external APIs.

Folder Structure:

/src
/Infrastructure
/database
/frameworks
/external_services

Presentation Layer:

Purpose: Handles user interface and input/output. It can be a web interface, CLI, or any other
means the user interacts with the application.

Implementations:

❖ Controllers (or Handlers): Handle user input, orchestrate calls to use cases, and manage
the flow of data.
❖ Views: Display information to the user.
❖ Presenters: Format data for display.

Folder Structure:

/src
/Presentation
/controllers
/views
/presenters

Example:

Suppose we are going to develop a full stack web development about Online doctor visiting,
where doctors will consult patients through video calls and write recommendations, which will
be downloaded by the patient. Before consulting with doctors, patients must register and book a
time slot filling out a form about diseases and corresponding doctors. Here I want to set up also
notifications services. Now Write Clean Code Architecture for this project.
Domain Layer:

This layer contains the core business logic and entities of your application.

Entities:

● Patient: Represents information about the patient.


● Doctor: Represents information about the doctor.
● Appointment: Represents a scheduled appointment between a patient and a doctor.
● MedicalRecord: Represents the medical records and recommendations.

Value Objects:

● TimeSlot: Represents the time slot for appointments.

Repositories:

● IPatientRepository: Interface for accessing and manipulating patient data.


● IDoctorRepository: Interface for accessing and manipulating doctor data.
● IAppointmentRepository: Interface for managing appointments and medical records.

Application Layer:

This layer contains the application-specific business rules and orchestrates the flow of data
between the domain and infrastructure layers.

Use Cases:

● RegisterPatientUseCase: Handles patient registration.


● BookAppointmentUseCase: Manages the process of booking appointments.
● ConsultationUseCase: Facilitates the video consultation and medical recommendations.

Interfaces:

● IPatientService: Interface for patient-related operations.


● IDoctorService: Interface for doctor-related operations.
● IAppointmentService: Interface for appointment-related operations.
Infrastructure Layer:

This layer contains the implementation of data access, external services, and any other
infrastructure-specific concerns.

Data Access:

● Implementations of the repositories defined in the domain layer.


● PatientRepository: Implements IPatientRepository.
● DoctorRepository: Implements IDoctorRepository.
● AppointmentRepository: Implements IAppointmentRepository.

External Services:

● Integration with video call services.


● Integration with notification services.

Presentation Layer:
This layer handles user interaction and presentation.

Controllers/Handlers:

● PatientController: Handles patient-related actions (registration, appointment booking).


● DoctorController: Handles doctor-related actions.
● AppointmentController: Manages appointment-related actions.

ViewModels/DTOs:

● PatientViewModel: Represents patient data for presentation.


● DoctorViewModel: Represents doctor data for presentation.
● AppointmentViewModel: Represents appointment data for presentation.

Notification Services:

● Implement notification services for sending reminders and updates to patients and
doctors.
Visual Presentation of Folder Structure

You might also like