# AutoMapper

**Kind:** Service

**Source:** `Pams/API/Pams.API/Pams.API.csproj` (line 1)

**Part of:** [Pams](subsystem-pams)

NuGet package dependency

AutoMapper is a NuGet package dependency in the Pams.API project for mapping data between object types. It supports converting domain entities into API DTOs and mapping incoming request models into application models.

## Diagram

```mermaid
sequenceDiagram
    participant Client
    participant API as Pams.API
    participant Mapper as AutoMapper
    participant Domain as Domain Model

    Client->>API: Send request DTO
    API->>Mapper: Map request DTO to domain model
    Mapper->>Domain: Create mapped model
    Domain-->>API: Return domain result
    API->>Mapper: Map domain result to response DTO
    Mapper-->>API: Return response DTO
    API-->>Client: Send API response
```

## Usage

```ts
type CreatePatientRequest = {
  name: string;
};

async function createPatient(request: CreatePatientRequest) {
  const response = await fetch("/api/patients", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(request),
  });

  if (!response.ok) {
    throw new Error("Patient request failed");
  }

  return response.json();
}
```

## AI Coding Instructions

- Define AutoMapper profiles in Pams.API or the application layer where source and destination models are known.
- Map API request and response DTOs instead of exposing domain entities directly from controllers.
- Register AutoMapper profiles with dependency injection during API startup.
- Keep mapping rules explicit when property names or value types differ between models.
- Add tests for mappings that include nested objects, renamed properties, or custom conversions.
