Skip to content

API layer

The API layer is the outermost layer of the PetFolio Service. It handles HTTP concerns - controllers, middleware, routing, and request/response mapping. Controllers are thin; they delegate all work to the Application Layer via MediatR.

Clean Architecture - presentation layer

The API layer depends on both Application and Infrastructure (for dependency injection wiring). It never contains business logic, validation rules, or data access code.

Responsibility

  • Controllers: REST API endpoints that receive HTTP requests and return responses (e.g. AnimalsController, AccountsController)
  • Middleware: Exception handling middleware that catches exceptions and returns ProblemDetails
  • Services: HTTP-specific service implementations (e.g. HttpContextClaimsPrincipalAccessor bridging ASP.NET Core's IHttpContextAccessor to IClaimsPrincipalAccessor)
  • Configuration: Application startup, dependency injection wiring in Program.cs

Components

graph TB
    subgraph "API Layer - controllers"
        AC["Animals Controller<br>POST /Create<br>GET /Get/#id<br>GET /GetAll"]
        ACC["Accounts Controller<br>POST /Create<br>GET /Get/#id<br>GET /GetAll"]
        UC["Users Controller<br>GET /Get/#id<br>GET /GetAll"]
    end

    subgraph "Middleware"
        EXC[Exception Handling Middleware<br>Catches exceptions<br>Returns ProblemDetails]
    end

    subgraph "MediatR"
        SENDER[ISender<br>Send commands/queries]
    end

    AC --> SENDER
    ACC --> SENDER
    UC --> SENDER

    SENDER --> EXC

Controller design

Controllers should only handle HTTP concerns and delegate to MediatR. They inject ISender (not services or repositories), send commands/queries, and convert Result<T> to HTTP status codes.

Validation is handled automatically by the Application Layer's validation pipeline - there is no need for manual ModelState.IsValid checks.

DO: Keep controllers thin - only HTTP concerns and delegation to services
DON'T: Put business logic, validation, or data access in controllers