# mahmoud921/social-media-platform-api architecture

> Architecture diagram of the GitHub repository mahmoud921/social-media-platform-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mahmoud921/social-media-platform-api
- Repository: https://github.com/mahmoud921/social-media-platform-api

## Overview

1. **What this repo is**
   - This is a **Clean Architecture / Onion Architecture .NET template** for a `SocialMedia` API.
   - The solution is split into four layers: **API**, **Application**, **Domain**, and **Infrastructure**.

2. **Presentation boundary: `SocialMedia.API`**
   - `Program.cs` is the composition root where the app is configured and the lower layers are wired together.
   - `Middleware/ExceptionHandlingMiddleware.cs` is the main cross-cutting concern visible in the tree: it centralizes exception-to-HTTP-response translation.
   - `appsettings.json` and `appsettings.Development.json` hold runtime configuration for the API host.

3. **Application layer: use-case support and shared contracts**
   - `SocialMedia.Application` currently contains shared response/result abstractions:
     - `Common/Responses/ApiResponse.cs`
     - `Common/Results/Result.cs`
   - It also contains application-specific exception types:
     - `BadRequestException`
     - `NotFoundException`
     - `UnauthorizedException`
     - `ValidationException`
     - `BaseException`
   - This layer is where use-case orchestration would normally live, even though handlers/controllers are not present in the current tree.

4. **Domain layer: core abstractions**
   - `SocialMedia.Domain` defines the persistence-facing contracts and base entity types:
     - `Primatives/BaseEntity.cs`
     - `Primatives/AuditableEntity.cs`
     - `Interfaces/IUnitOfWork.cs`
     - `Interfaces/Repository/IRepository.cs`
   - The Domain layer is the core boundary for reusable business primitives and repository/UoW abstractions, without infrastructure details.

5. **Infrastructure layer: persistence implementation**
   - `SocialMedia.Infrastrucure` contains the concrete persistence implementation:
     - `Persistence/AppDbContext.cs`
     - `Persistence/Repository/Repository.cs`
     - `Persistence/UnitOfWork.cs`
   - This is the layer that would adapt the domain abstractions to EF Core and the database.

6. **Primary data flow**
   - A request enters the **API**.
   - Global middleware handles unhandled exceptions and shapes error responses.
   - The API would call into the **Application** layer for business operations.
   - Application code depends on **Domain** contracts.
   - **Infrastructure** implements the repository and unit-of-work contracts and talks to `AppDbContext`.
   - Results travel back as `Result` / `ApiResponse` objects, then out through the API.

7. **Error handling model**
   - The repo is set up for **centralized exception handling** rather than ad-hoc `try/catch` in endpoints.
   - The custom exceptions in `Application` are likely intended to map to consistent HTTP statuses and response payloads.
   - `ApiResponse` suggests a standardized wrapper for successful and failed responses.

8. **Persistence boundary**
   - `AppDbContext` is the database access boundary.
   - `Repository<T>` provides a generic data-access abstraction over entities.
   - `UnitOfWork` coordinates repository operations and transaction boundaries.
   - This keeps EF Core details out of the higher layers.

9. **Dependency direction**
   - The intended architecture is:
     - **Domain**: no inward dependencies
     - **Application**: depends on Domain
     - **Infrastructure**: implements Domain contracts and provides persistence
     - **API**: composes and exposes the application
   - For a diagram, arrows should point inward toward Domain, with Infrastructure pointing to Domain abstractions and API depending on the outer layers.

10. **What is not yet present in the tree**
   - The README advertises CQRS, MediatR, FluentValidation, AutoMapper, JWT auth, logging, and handlers/controllers.
   - Those modules are **not visible in the current file tree**, so this snapshot looks like a **foundation/template skeleton** rather than a fully implemented social media application.
   - There are no visible feature modules, entity definitions, controllers, or request handlers yet.

11. **Notable repo detail**
   - The infrastructure project name is misspelled as `SocialMedia.Infrastrucure`, but functionally it is the persistence/infrastructure layer.

12. **How to draw the architecture**
   - Draw four boxes: **API → Application → Domain**, with **Infrastructure** attached to Application/Domain as the persistence implementer.
   - Add a database external to Infrastructure, with `AppDbContext` as the gateway.
   - Place `ExceptionHandlingMiddleware` on the API edge and `ApiResponse/Result` as shared response contracts in the Application layer.

Main entry points: [`SocialMedia.API/Program.cs`](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/Program.cs) (Program), [`SocialMedia.API/Middleware/ExceptionHandlingMiddleware.cs`](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/Middleware/ExceptionHandlingMiddleware.cs) (Exception Middleware), [`SocialMedia.Application/Exceptions/BaseException.cs`](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/BaseException.cs) (BaseException), [`SocialMedia.Infrastrucure/Persistence/UnitOfWork.cs`](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Infrastrucure/Persistence/UnitOfWork.cs) (UnitOfWork)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API"]
  node_api_program["Program<br/>composition root<br/>[Program.cs]"]
  node_api_middleware["Exception Middleware<br/>http middleware"]
  node_api_config["App Settings<br/>runtime config<br/>[appsettings.json]"]
  node_api_config_dev["Dev Settings<br/>runtime config"]
end

subgraph group_application["Application"]
  node_app_response["ApiResponse<br/>response contract<br/>[ApiResponse.cs]"]
  node_app_result["Result<br/>[Result.cs]"]
  node_app_base_exc["BaseException<br/>app exception<br/>[BaseException.cs]"]
  node_app_badreq["BadRequest<br/>app exception"]
  node_app_notfound["NotFound<br/>app exception"]
  node_app_unauth["Unauthorized<br/>app exception"]
  node_app_validation["Validation<br/>app exception"]
end

subgraph group_domain["Domain"]
  node_domain_entity["BaseEntity<br/>entity base<br/>[BaseEntity.cs]"]
  node_domain_audit["Auditable<br/>entity base<br/>[AuditableEntity.cs]"]
  node_domain_repo["IRepository<br/>repository contract<br/>[IRepository.cs]"]
  node_domain_uow["IUnitOfWork<br/>uow contract<br/>[IUnitOfWork.cs]"]
end

subgraph group_infrastructure["Infrastructure"]
  node_infra_db[("AppDbContext<br/>ef dbcontext<br/>[AppDbContext.cs]")]
  node_infra_repo["Repository<br/>[Repository.cs]"]
  node_infra_uow["UnitOfWork<br/>ef uow<br/>[UnitOfWork.cs]"]
end

node_api_program -->|"uses"| node_api_middleware
node_api_program -->|"loads"| node_api_config
node_api_program -->|"loads"| node_api_config_dev
node_api_program -->|"composes"| node_app_response
node_api_program -->|"composes"| node_app_result
node_api_middleware -->|"maps"| node_app_base_exc
node_api_middleware -->|"maps"| node_app_badreq
node_api_middleware -->|"maps"| node_app_notfound
node_api_middleware -->|"maps"| node_app_unauth
node_api_middleware -->|"maps"| node_app_validation
node_app_badreq -->|"inherits"| node_app_base_exc
node_app_notfound -->|"inherits"| node_app_base_exc
node_app_unauth -->|"inherits"| node_app_base_exc
node_app_validation -->|"inherits"| node_app_base_exc
node_domain_audit -->|"extends"| node_domain_entity
node_infra_repo -->|"implements"| node_domain_repo
node_infra_uow -->|"implements"| node_domain_uow
node_infra_repo -->|"uses"| node_infra_db
node_infra_uow -->|"coordinates"| node_infra_repo
node_infra_uow -->|"commits"| node_infra_db
node_api_program -->|"wires"| node_infra_uow

click node_api_program "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/Program.cs"
click node_api_middleware "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/Middleware/ExceptionHandlingMiddleware.cs"
click node_api_config "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/appsettings.json"
click node_api_config_dev "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/appsettings.Development.json"
click node_app_response "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Common/Responses/ApiResponse.cs"
click node_app_result "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Common/Results/Result.cs"
click node_app_base_exc "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/BaseException.cs"
click node_app_badreq "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/BadRequestException.cs"
click node_app_notfound "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/NotFoundException.cs"
click node_app_unauth "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/UnauthorizedException.cs"
click node_app_validation "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/ValidationException.cs"
click node_domain_entity "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Domain/Primatives/BaseEntity.cs"
click node_domain_audit "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Domain/Primatives/AuditableEntity.cs"
click node_domain_repo "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Domain/Interfaces/Repository/IRepository.cs"
click node_domain_uow "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Domain/Interfaces/IUnitOfWork.cs"
click node_infra_db "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Infrastrucure/Persistence/AppDbContext.cs"
click node_infra_repo "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Infrastrucure/Persistence/Repository/Repository.cs"
click node_infra_uow "https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Infrastrucure/Persistence/UnitOfWork.cs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_program,node_api_middleware,node_api_config,node_api_config_dev toneBlue
class node_app_response,node_app_result,node_app_base_exc,node_app_badreq,node_app_notfound,node_app_unauth,node_app_validation toneAmber
class node_domain_entity,node_domain_audit,node_domain_repo,node_domain_uow toneMint
class node_infra_db,node_infra_repo,node_infra_uow toneRose
```

## Components

### API

Presentation and host composition root

- [Program](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/Program.cs): composition root · Wires the application host, middleware, and service registrations · `SocialMedia.API/Program.cs`
- [Exception Middleware](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/Middleware/ExceptionHandlingMiddleware.cs): http middleware · Central exception-to-response translator · `SocialMedia.API/Middleware/ExceptionHandlingMiddleware.cs`
- [App Settings](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/appsettings.json): runtime config · Host and environment configuration · `SocialMedia.API/appsettings.json`
- [Dev Settings](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.API/appsettings.Development.json): runtime config · Development-specific API configuration · `SocialMedia.API/appsettings.Development.json`

### Application

Use-case support, shared responses, and exceptions

- [ApiResponse](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Common/Responses/ApiResponse.cs): response contract · Standard wrapper for API responses · `SocialMedia.Application/Common/Responses/ApiResponse.cs`
- [Result](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Common/Results/Result.cs): result contract · Success/failure outcome abstraction · `SocialMedia.Application/Common/Results/Result.cs`
- [BaseException](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/BaseException.cs): app exception · Shared base type for application exceptions · `SocialMedia.Application/Exceptions/BaseException.cs`
- [BadRequest](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/BadRequestException.cs): app exception · Represents invalid client input · `SocialMedia.Application/Exceptions/BadRequestException.cs`
- [NotFound](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/NotFoundException.cs): app exception · Represents missing resources · `SocialMedia.Application/Exceptions/NotFoundException.cs`
- [Unauthorized](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/UnauthorizedException.cs): app exception · Represents authentication or authorization failures · `SocialMedia.Application/Exceptions/UnauthorizedException.cs`
- [Validation](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Application/Exceptions/ValidationException.cs): app exception · Represents validation failures · `SocialMedia.Application/Exceptions/ValidationException.cs`

### Domain

Core entities and persistence contracts

- [BaseEntity](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Domain/Primatives/BaseEntity.cs): entity base · Core entity identity boundary · `SocialMedia.Domain/Primatives/BaseEntity.cs`
- [Auditable](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Domain/Primatives/AuditableEntity.cs): entity base · Entity base with audit fields · `SocialMedia.Domain/Primatives/AuditableEntity.cs`
- [IRepository](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Domain/Interfaces/Repository/IRepository.cs): repository contract · Generic persistence abstraction · `SocialMedia.Domain/Interfaces/Repository/IRepository.cs`
- [IUnitOfWork](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Domain/Interfaces/IUnitOfWork.cs): uow contract · Coordinates repository work and persistence commits · `SocialMedia.Domain/Interfaces/IUnitOfWork.cs`

### Infrastructure

EF Core persistence implementation

- [AppDbContext](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Infrastrucure/Persistence/AppDbContext.cs): ef dbcontext · Database access boundary · `SocialMedia.Infrastrucure/Persistence/AppDbContext.cs`
- [Repository](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Infrastrucure/Persistence/Repository/Repository.cs): ef repository · Generic EF Core repository implementation · `SocialMedia.Infrastrucure/Persistence/Repository/Repository.cs`
- [UnitOfWork](https://github.com/mahmoud921/social-media-platform-api/blob/master/SocialMedia.Infrastrucure/Persistence/UnitOfWork.cs): ef uow · Concrete unit-of-work implementation · `SocialMedia.Infrastrucure/Persistence/UnitOfWork.cs`

## Connections

- Program → Exception Middleware: uses · Registers exception handling in the host pipeline
- Program → App Settings: loads · Reads runtime configuration
- Program → Dev Settings: loads · Reads development overrides
- Program → ApiResponse: composes · Consumes shared application contracts
- Program → Result: composes · Consumes shared application contracts
- Exception Middleware → BaseException: maps · Translates application exceptions to HTTP responses
- Exception Middleware → BadRequest: maps · Handles bad request errors
- Exception Middleware → NotFound: maps · Handles missing resource errors
- Exception Middleware → Unauthorized: maps · Handles authorization errors
- Exception Middleware → Validation: maps · Handles validation errors
- BadRequest → BaseException: inherits · Specialized application exception
- NotFound → BaseException: inherits · Specialized application exception
- Unauthorized → BaseException: inherits · Specialized application exception
- Validation → BaseException: inherits · Specialized application exception
- Auditable → BaseEntity: extends · Adds audit data to the base entity
- Repository → IRepository: implements · Concrete persistence adapter for repository contract
- UnitOfWork → IUnitOfWork: implements · Concrete persistence adapter for unit of work contract
- Repository → AppDbContext: uses · Persists entities through EF Core context
- UnitOfWork → Repository: coordinates · Coordinates repository operations
- UnitOfWork → AppDbContext: commits · Manages persistence boundaries and save operations
- Program → UnitOfWork: wires · Composition root connects outer host to persistence services
