# hieptrinh2002/pureclinic.backend architecture

> Architecture diagram of the GitHub repository hieptrinh2002/pureclinic.backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hieptrinh2002/pureclinic.backend
- Repository: https://github.com/hieptrinh2002/pureclinic.backend

## Overview

##### Main Components
- API / Presentation Layer:
- Controllers (e.g., AccountController, AppointmentController, DoctorController, etc.)
- Action filters, Middleware (GlobalExceptionHandler, Logging, PermissionHandler)
- Extensions (e.g., mapping and security extensions)
- SignalR hubs for notifications (AppointmentHub, ChatHub, NotificationHub, etc.)

- Application / Business Layer:
- Business objects and view models (for appointments, doctors, users, etc.)
- Validators for input models
- Service interfaces (IAppointmentService, IAuthService, IDoctorService, etc.)
- Implementation of Business Logic encapsulated in Services (AuthService, AppointmentService, etc.)
- Mapper layer (MappingProfile)

- Domain / Core:
- Domain entities (Patient, Doctor, Appointment, Invoice, MedicalReport, etc.)
- Enumerations, Constants, Exceptions, and interfaces (for domain behavior)
- Core business logic and domain models

- Infrastructure Layer:
- Data Access:
- Persistence (ApplicationDbContext, EntityConfigurations, Repository implementations, UnitOfWork pattern)
- Seed data scripts and configurations
- External Services:
- Email and SMTP integrations
- File storage via Cloudinary
- Caching (MemoryCache & RedisCache)
- RabbitMQ integration for message-based background jobs (EmailProducer, FileProducer, RabbitMQ Connection, etc.)
- Background Job Processing:
- Background services (AppointmentReminderJob, DataCleanupJob, EmailConfirmationJob, ReportGenerationJob)
- Schedulers (RecurringJobScheduler, DelayedJobScheduler)
- SignalR for real-time notifications (HubConnectionService)

##### Relationships and Interactions
- The API layer receives HTTP requests and routes them to Controllers.
- Controllers invoke business services (e.g., AuthService, AppointmentService) which implement the core application logic.
- Business services in turn interact with repository interfaces in the Infrastructure layer via the Unit of Work to query or update the database.
- Caching services (both Memory and Redis) are incorporated to optimize performance.
- Background job components are scheduled via Hangfire (or equivalent scheduling logic) and use RabbitMQ to produce/consume messages.
- External services like Cloudinary and SMTP Email Services are called from within the Infrastructure services.
- Real-time communication with SignalR hubs allows updates to be pushed to connected clients (doctors, patients, etc.).
- Validators are used extensively in the API layer to ensure data integrity before processing.

##### Highlighted Architectural Patterns and Design Principles
- Clean Architecture: Clear separation into Core, Infrastructure, and API layers.
- Repository & Unit of Work: Abstract data access in a maintainable and testable manner.
- Domain-Driven Design: Use of rich domain models and business objects.
- Background Job Processing and Messaging: Integration of asynchronous tasks and RabbitMQ messaging for scalability.
- Caching Strategy: Combination of Memory Cache and Redis for optimized performance.
- Real-Time Communication: SignalR components for live notifications and interactions.

##### Instructing the Principal Software Engineer
– API Layer (Controllers, Filters, Middlewares, SignalR Hubs)
– Business/Application Layer (Services, Validators, Mappers)
– Domain/Core Layer (Entities, Business Models)
– Infrastructure (Database access, Repositories, External Services, Background Jobs, Caching, Messaging with RabbitMQ)
- Then, drill down into how each component interacts. For example:
- An HTTP request to /api/appointment triggers the AppointmentController, which calls AppointmentService.
- AppointmentService uses a repository (via the Unit of Work pattern) to interact with the database and might also trigger a background job for appointment reminders.
- The NotificationHub (via SignalR) is used to send real-time alerts.
- Separation of concerns, so each layer (presentation, business, data) is visually distinct.
- Use annotations to note integration details:
– “Cache: MemoryCacheService / RedisCacheService”
– “Background Jobs: Hangfire / Scheduler services”
– “External Services: Cloudinary, SMTP”.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API / Presentation Layer
    subgraph "API / Presentation Layer"
        A1["Controllers"]:::api
        A2["Action Filters"]:::api
        A3["Middlewares"]:::api
        A4["Extensions"]:::api
        A5["SignalR Hubs"]:::api
    end

    %% Application / Business Layer
    subgraph "Application / Business Layer"
        B1["Business Objects & Validators"]:::business
        B2["Service Interfaces"]:::business
        B3["Service Implementations"]:::business
        B4["Mapper Layer"]:::business
    end

    %% Domain / Core Layer
    subgraph "Domain / Core Layer"
        C1["Domain Entities"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        D1["Data Access & Repository"]:::infra
        D2["External Services"]:::infra
        D3["Background Jobs & Messaging"]:::infra
        D4["Caching"]:::infra
        D5["SignalR Notification Service"]:::infra
        D6(["Database"]):::database
    end

    %% Relationships in API Layer
    A3 -->|"pre-process"| A1
    A2 -->|"validate"| A1

    %% Relationships from API to Application Layer
    A1 -->|"invoke"| B3

    %% Relationships within Application Layer
    B2 -->|"implements"| B3
    B1 -->|"validate & enrich"| B3
    B4 -->|"maps data"| B3

    %% Relationships from Application to Domain Layer
    B3 -->|"processes"| C1

    %% Relationships from Application to Infrastructure Layer
    B3 -->|"data access"| D1
    B3 -->|"calls"| D2
    B3 -->|"utilizes"| D4

    %% Relationships within Infrastructure Layer
    D1 -->|"persists to"| D6
    D3 -->|"triggers"| D2
    D3 -->|"notifies"| D5
    A5 -->|"updates"| D5

    %% Class Styles
    classDef api fill:#f4e1f4,stroke:#333,stroke-width:2px;
    classDef business fill:#d0e9f2,stroke:#333,stroke-width:2px;
    classDef domain fill:#c3f3c3,stroke:#333,stroke-width:2px;
    classDef infra fill:#f9f9a9,stroke:#333,stroke-width:2px;
    classDef database fill:#ffe6a0,stroke:#333,stroke-width:2px,shape: cylinder;

    %% Click Events for API / Presentation Layer
    click A1 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.API/Controllers/V1"
    click A2 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.API/ActionFilters"
    click A3 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.API/Middlewares"
    click A4 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.API/Extensions"
    click A5 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.Infrastructure/SignalR/Hubs"

    %% Click Events for Application / Business Layer
    click B1 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PeruLife.Clinic.Application/BusinessObjects"
    click B2 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PeruLife.Clinic.Application/Interfaces/IServices"
    click B3 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PeruLife.Clinic.Application/Services"
    click B4 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PeruLife.Clinic.Application/Mapper"

    %% Click Event for Domain / Core Layer
    click C1 "https://github.com/hieptrinh2002/pureclinic.backend/blob/master/PureLifeClinic.Core"

    %% Click Events for Infrastructure Layer
    click D1 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.Infrastructure/Persistence"
    click D2 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.Infrastructure/ExternalServices"
    click D3 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.Infrastructure/BackgroundServices"
    click D4 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.Infrastructure/Caching"
    click D5 "https://github.com/hieptrinh2002/pureclinic.backend/tree/master/PureLifeClinic.Infrastructure/SignalR"
```
