# m4shenawy1702/onionapitemplate architecture

> Architecture diagram of the GitHub repository m4shenawy1702/onionapitemplate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/m4shenawy1702/onionapitemplate
- Repository: https://github.com/m4shenawy1702/onionapitemplate

## Overview

This is a .NET 8 Clean Architecture Web API template whose implemented user-facing workflow is authentication: callers submit login or registration requests, the controller delegates to an application service, and successful requests return a JWT response. Application logic validates input, checks or creates users, and builds tokens; infrastructure supplies persistence and repository abstractions. The README also describes DTO validation, mapping, and global error handling. Some wiring is documented or visible only in the source index, so uncertain runtime links are omitted or marked dashed.

Main entry points: [`OnionApiTemplate.Application/Services/AuthService.cs`](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/Services/AuthService.cs) (Auth service), [`OnionApiTemplate.Infrastructure/Persistence/Repositories/GenericRepository.cs`](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/Repositories/GenericRepository.cs) (Generic repository), [`OnionApiTemplate.Infrastructure/Persistence/ApplicationDbContext.cs`](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/ApplicationDbContext.cs) (Identity database context), [`OnionApiTemplate.Infrastructure/InfrastructureDI.cs`](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/InfrastructureDI.cs) (Infrastructure registration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API hosting"]
  node_host["API host<br/>[Program.cs]"]
  node_controller["Auth controller"]
  node_error_middleware["Error middleware"]
  node_middleware_setup["Middleware setup"]
  node_response_factory["Error response factory"]
  node_validation_error["Validation error model"]
end

subgraph group_application["Authentication"]
  node_auth_service["Auth service<br/>[AuthService.cs]"]
  node_auth_contract["Auth service contract<br/>[IAuthService.cs]"]
  node_auth_dtos["Auth request and response<br/>[Auth]"]
  node_auth_validation["Auth validation<br/>[Auth]"]
  node_user_mapping["User mapping<br/>[UserProfile.cs]"]
  node_app_registration["Application registration"]
end

subgraph group_domain["Domain contracts"]
  node_user["Application user<br/>[ApplicationUser.cs]"]
  node_base_entity["Base entity<br/>[BaseEntity.cs]"]
  node_repository_contract["Repository contract"]
  node_specification["Specifications<br/>[ISpecification.cs]"]
  node_unit_contract["Unit of work contract<br/>[IUnitOfWork.cs]"]
  node_initializer_contract["Initializer contract<br/>[IDbInitializer.cs]"]
  node_bad_request["Bad request exception"]
  node_jwt_settings["JWT settings<br/>[JWT.cs]"]
end

subgraph group_infrastructure["Persistence"]
  node_infrastructure_registration["Infrastructure registration"]
  node_db_context[("Identity database context")]
  node_initializer["Database initializer<br/>[DbIntializer.cs]"]
  node_generic_repository["Generic repository"]
  node_spec_evaluator["Specification evaluator"]
  node_unit_of_work["Unit of work<br/>[UnitOfWork.cs]"]
end

node_caller(("API caller"))
node_identity["ASP.NET Identity"]

node_caller -->|"submits credentials"| node_controller
node_controller -->|"delegates auth"| node_auth_service
node_auth_service -->|"validates requests"| node_auth_validation
node_auth_service -->|"checks and creates users"| node_identity
node_auth_service -.->|"maps registration"| node_user_mapping
node_auth_service -->|"uses user entity"| node_user
node_auth_service -->|"reads token settings"| node_jwt_settings
node_auth_service -->|"throws validation errors"| node_bad_request
node_controller -->|"accepts and returns"| node_auth_dtos
node_auth_service -->|"processes and returns"| node_auth_dtos
node_app_registration -->|"registers service"| node_auth_service
node_app_registration -->|"registers validators"| node_auth_validation
node_app_registration -->|"registers mapping"| node_user_mapping
node_middleware_setup -->|"registers middleware"| node_error_middleware
node_error_middleware -->|"handles domain errors"| node_bad_request
node_response_factory -->|"creates response"| node_validation_error
node_host -->|"uses initializer"| node_initializer_contract
node_infrastructure_registration -->|"registers context"| node_db_context
node_infrastructure_registration -->|"registers initializer"| node_initializer
node_infrastructure_registration -->|"registers unit of work"| node_unit_of_work
node_infrastructure_registration -->|"registers repository"| node_generic_repository
node_initializer -->|"initializes database"| node_db_context
node_db_context -->|"persists identity user"| node_user
node_generic_repository -->|"queries and updates"| node_db_context
node_generic_repository -->|"builds specification query"| node_spec_evaluator
node_generic_repository -->|"implements contract"| node_repository_contract
node_unit_of_work -->|"uses context"| node_db_context
node_unit_of_work -->|"creates repositories"| node_generic_repository
node_unit_of_work -->|"implements contract"| node_unit_contract
node_repository_contract -->|"constrains entity type"| node_base_entity
node_repository_contract -->|"accepts specifications"| node_specification

click node_host "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Program.cs"
click node_controller "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Presentation/Controllers/AuthenticationController.cs"
click node_error_middleware "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Middlewares/GlobalErrorHandlingMiddleware.cs"
click node_middleware_setup "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Extensions/UseCustomExceptionHandlingMiddlware.cs"
click node_response_factory "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Factories/ApiResponseFactory.cs"
click node_validation_error "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Models/Errors/ValidationErrorResponse.cs"
click node_auth_service "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/Services/AuthService.cs"
click node_auth_contract "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/IServices/IAuthService.cs"
click node_auth_dtos "https://github.com/m4shenawy1702/onionapitemplate/tree/master/OnionApiTemplate.Application/DOTs/Auth"
click node_auth_validation "https://github.com/m4shenawy1702/onionapitemplate/tree/master/OnionApiTemplate.Application/Validations/Auth"
click node_user_mapping "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/MappingProfile/UserProfile.cs"
click node_app_registration "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/ApplicationServiceRegistration.cs"
click node_user "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/Entities/ApplicationUser.cs"
click node_base_entity "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/Entities/BaseEntity.cs"
click node_repository_contract "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/IRepositoty/IGenericRepository.cs"
click node_specification "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/IRepositoty/ISpecification.cs"
click node_unit_contract "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/IRepositoty/IUnitOfWork.cs"
click node_initializer_contract "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/IRepositoty/IDbInitializer.cs"
click node_bad_request "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/Exceptions/BadRequestException.cs"
click node_jwt_settings "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/Setting/JWT.cs"
click node_infrastructure_registration "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/InfrastructureDI.cs"
click node_db_context "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/ApplicationDbContext.cs"
click node_initializer "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/DbIntializer.cs"
click node_generic_repository "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/Repositories/GenericRepository.cs"
click node_spec_evaluator "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/Repositories/SpecificationEvaluator.cs"
click node_unit_of_work "https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/Repositories/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_host,node_controller,node_error_middleware,node_middleware_setup,node_response_factory,node_validation_error toneBlue
class node_auth_service,node_auth_contract,node_auth_dtos,node_auth_validation,node_user_mapping,node_app_registration toneAmber
class node_user,node_base_entity,node_repository_contract,node_specification,node_unit_contract,node_initializer_contract,node_bad_request,node_jwt_settings,node_caller toneMint
class node_infrastructure_registration,node_db_context,node_initializer,node_generic_repository,node_spec_evaluator,node_unit_of_work toneRose
class node_identity toneIndigo
```

## Components

### API hosting

- [API host](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Program.cs): `OnionApiTemplate.API/Program.cs`
- [Auth controller](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Presentation/Controllers/AuthenticationController.cs): `OnionApiTemplate.Presentation/Controllers/AuthenticationController.cs`
- [Error middleware](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Middlewares/GlobalErrorHandlingMiddleware.cs): `OnionApiTemplate.API/Middlewares/GlobalErrorHandlingMiddleware.cs`
- [Middleware setup](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Extensions/UseCustomExceptionHandlingMiddlware.cs): `OnionApiTemplate.API/Extensions/UseCustomExceptionHandlingMiddlware.cs`
- [Error response factory](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Factories/ApiResponseFactory.cs): `OnionApiTemplate.API/Factories/ApiResponseFactory.cs`
- [Validation error model](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.API/Models/Errors/ValidationErrorResponse.cs): `OnionApiTemplate.API/Models/Errors/ValidationErrorResponse.cs`

### Authentication

- [Auth service](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/Services/AuthService.cs): `OnionApiTemplate.Application/Services/AuthService.cs`
- [Auth service contract](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/IServices/IAuthService.cs): `OnionApiTemplate.Application/IServices/IAuthService.cs`
- [Auth request and response](https://github.com/m4shenawy1702/onionapitemplate/tree/master/OnionApiTemplate.Application/DOTs/Auth): `OnionApiTemplate.Application/DOTs/Auth`
- [Auth validation](https://github.com/m4shenawy1702/onionapitemplate/tree/master/OnionApiTemplate.Application/Validations/Auth): `OnionApiTemplate.Application/Validations/Auth`
- [User mapping](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/MappingProfile/UserProfile.cs): `OnionApiTemplate.Application/MappingProfile/UserProfile.cs`
- [Application registration](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Application/ApplicationServiceRegistration.cs): `OnionApiTemplate.Application/ApplicationServiceRegistration.cs`

### Domain contracts

- [Application user](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/Entities/ApplicationUser.cs): `OnionApiTemplate.Domain/Entities/ApplicationUser.cs`
- [Base entity](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/Entities/BaseEntity.cs): `OnionApiTemplate.Domain/Entities/BaseEntity.cs`
- [Repository contract](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/IRepositoty/IGenericRepository.cs): `OnionApiTemplate.Domain/IRepositoty/IGenericRepository.cs`
- [Specifications](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/IRepositoty/ISpecification.cs): `OnionApiTemplate.Domain/IRepositoty/ISpecification.cs`
- [Unit of work contract](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/IRepositoty/IUnitOfWork.cs): `OnionApiTemplate.Domain/IRepositoty/IUnitOfWork.cs`
- [Initializer contract](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/IRepositoty/IDbInitializer.cs): `OnionApiTemplate.Domain/IRepositoty/IDbInitializer.cs`
- [Bad request exception](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/Exceptions/BadRequestException.cs): `OnionApiTemplate.Domain/Exceptions/BadRequestException.cs`
- [JWT settings](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Domain/Setting/JWT.cs): `OnionApiTemplate.Domain/Setting/JWT.cs`

### Persistence

- [Infrastructure registration](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/InfrastructureDI.cs): `OnionApiTemplate.Infrastructure/InfrastructureDI.cs`
- [Identity database context](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/ApplicationDbContext.cs): `OnionApiTemplate.Infrastructure/Persistence/ApplicationDbContext.cs`
- [Database initializer](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/DbIntializer.cs): `OnionApiTemplate.Infrastructure/Persistence/DbIntializer.cs`
- [Generic repository](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/Repositories/GenericRepository.cs): `OnionApiTemplate.Infrastructure/Persistence/Repositories/GenericRepository.cs`
- [Specification evaluator](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/Repositories/SpecificationEvaluator.cs): `OnionApiTemplate.Infrastructure/Persistence/Repositories/SpecificationEvaluator.cs`
- [Unit of work](https://github.com/m4shenawy1702/onionapitemplate/blob/master/OnionApiTemplate.Infrastructure/Persistence/Repositories/UnitOfWork.cs): `OnionApiTemplate.Infrastructure/Persistence/Repositories/UnitOfWork.cs`

### Other components

- **API caller**
- **ASP.NET Identity**

## Connections

- API caller → Auth controller: submits credentials (evidence: `OnionApiTemplate.Presentation/Controllers/AuthenticationController.cs`)
- Auth controller → Auth service: delegates auth (evidence: `OnionApiTemplate.Presentation/Controllers/AuthenticationController.cs`)
- Auth service → Auth validation: validates requests (evidence: `OnionApiTemplate.Application/Services/AuthService.cs`)
- Auth service → ASP.NET Identity: checks and creates users (evidence: `OnionApiTemplate.Application/Services/AuthService.cs`)
- Auth service → User mapping: maps registration
- Auth service → Application user: uses user entity (evidence: `OnionApiTemplate.Application/Services/AuthService.cs`)
- Auth service → JWT settings: reads token settings (evidence: `OnionApiTemplate.Application/Services/AuthService.cs`)
- Auth service → Bad request exception: throws validation errors (evidence: `OnionApiTemplate.Application/Services/AuthService.cs`)
- Auth controller → Auth request and response: accepts and returns (evidence: `OnionApiTemplate.Presentation/Controllers/AuthenticationController.cs`)
- Auth service → Auth request and response: processes and returns (evidence: `OnionApiTemplate.Application/Services/AuthService.cs`)
- Application registration → Auth service: registers service (evidence: `OnionApiTemplate.Application/ApplicationServiceRegistration.cs`)
- Application registration → Auth validation: registers validators (evidence: `OnionApiTemplate.Application/ApplicationServiceRegistration.cs`)
- Application registration → User mapping: registers mapping (evidence: `OnionApiTemplate.Application/ApplicationServiceRegistration.cs`)
- Middleware setup → Error middleware: registers middleware (evidence: `OnionApiTemplate.API/Extensions/UseCustomExceptionHandlingMiddlware.cs`)
- Error middleware → Bad request exception: handles domain errors (evidence: `OnionApiTemplate.API/Middlewares/GlobalErrorHandlingMiddleware.cs`)
- Error response factory → Validation error model: creates response (evidence: `OnionApiTemplate.API/Factories/ApiResponseFactory.cs`)
- API host → Initializer contract: uses initializer (evidence: `OnionApiTemplate.API/Program.cs`)
- Infrastructure registration → Identity database context: registers context (evidence: `OnionApiTemplate.Infrastructure/InfrastructureDI.cs`)
- Infrastructure registration → Database initializer: registers initializer (evidence: `OnionApiTemplate.Infrastructure/InfrastructureDI.cs`)
- Infrastructure registration → Unit of work: registers unit of work (evidence: `OnionApiTemplate.Infrastructure/InfrastructureDI.cs`)
- Infrastructure registration → Generic repository: registers repository (evidence: `OnionApiTemplate.Infrastructure/InfrastructureDI.cs`)
- Database initializer → Identity database context: initializes database (evidence: `OnionApiTemplate.Infrastructure/Persistence/DbIntializer.cs`)
- Identity database context → Application user: persists identity user (evidence: `OnionApiTemplate.Infrastructure/Persistence/ApplicationDbContext.cs`)
- Generic repository → Identity database context: queries and updates (evidence: `OnionApiTemplate.Infrastructure/Persistence/Repositories/GenericRepository.cs`)
- Generic repository → Specification evaluator: builds specification query (evidence: `OnionApiTemplate.Infrastructure/Persistence/Repositories/GenericRepository.cs`)
- Generic repository → Repository contract: implements contract (evidence: `OnionApiTemplate.Infrastructure/Persistence/Repositories/GenericRepository.cs`)
- Unit of work → Identity database context: uses context (evidence: `OnionApiTemplate.Infrastructure/Persistence/Repositories/UnitOfWork.cs`)
- Unit of work → Generic repository: creates repositories (evidence: `OnionApiTemplate.Infrastructure/Persistence/Repositories/UnitOfWork.cs`)
- Unit of work → Unit of work contract: implements contract (evidence: `OnionApiTemplate.Infrastructure/Persistence/Repositories/UnitOfWork.cs`)
- Repository contract → Base entity: constrains entity type (evidence: `OnionApiTemplate.Domain/IRepositoty/IGenericRepository.cs`)
- Repository contract → Specifications: accepts specifications (evidence: `OnionApiTemplate.Domain/IRepositoty/IGenericRepository.cs`)
