How mahmoud921/social-media-platform-api works
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: Program.cs Program · ExceptionHandlingMiddleware.cs Exception Middleware · BaseException.cs BaseException · UnitOfWork.cs UnitOfWork
Components 18
API
Presentation and host composition root
- Program · Wires the application host, middleware, and service registrations
SocialMedia.API/Program.cs - Exception Middleware · Central exception-to-response translator
SocialMedia.API/Middleware/ExceptionHandlingMiddleware.cs - App Settings · Host and environment configuration
SocialMedia.API/appsettings.json - Dev Settings · Development-specific API configuration
SocialMedia.API/appsettings.Development.json
Application
Use-case support, shared responses, and exceptions
- ApiResponse · Standard wrapper for API responses
SocialMedia.Application/Common/Responses/ApiResponse.cs - Result · Success/failure outcome abstraction
SocialMedia.Application/Common/Results/Result.cs - BaseException · Shared base type for application exceptions
SocialMedia.Application/Exceptions/BaseException.cs - BadRequest · Represents invalid client input
SocialMedia.Application/Exceptions/BadRequestException.cs - NotFound · Represents missing resources
SocialMedia.Application/Exceptions/NotFoundException.cs - Unauthorized · Represents authentication or authorization failures
SocialMedia.Application/Exceptions/UnauthorizedException.cs - Validation · Represents validation failures
SocialMedia.Application/Exceptions/ValidationException.cs
Domain
Core entities and persistence contracts
- BaseEntity · Core entity identity boundary
SocialMedia.Domain/Primatives/BaseEntity.cs - Auditable · Entity base with audit fields
SocialMedia.Domain/Primatives/AuditableEntity.cs - IRepository · Generic persistence abstraction
SocialMedia.Domain/Interfaces/Repository/IRepository.cs - IUnitOfWork · Coordinates repository work and persistence commits
SocialMedia.Domain/Interfaces/IUnitOfWork.cs
Infrastructure
EF Core persistence implementation
- AppDbContext · Database access boundary
SocialMedia.Infrastrucure/Persistence/AppDbContext.cs - Repository · Generic EF Core repository implementation
SocialMedia.Infrastrucure/Persistence/Repository/Repository.cs - UnitOfWork · Concrete unit-of-work implementation
SocialMedia.Infrastrucure/Persistence/UnitOfWork.cs
Connections 21
- 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