0Pricing
Kotlin Multiplatform Academy · Lekcja

Mapowanie DTO na modele domenowe

Oddzielanie struktur sieciowych od typów domenowych

Mapowanie DTO na modele domenowe to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Two Kinds of Models

Your app actually has two shapes of data: the raw DTO from the API and the clean domain model your code wants.

What a DTO Is

A DTO, or data transfer object, mirrors the JSON exactly, even when the field names are messy or awkward.

@Serializable
data class UserDto(val user_name: String, val avatar_url: String?)

Why Not Use DTOs Directly

If your whole app uses DTOs, a tiny API change ripples everywhere. A domain model protects you from that.

The Domain Model

The domain model is shaped for your code: nice names, no nullable junk, exactly the fields the UI needs.

data class User(val name: String, val avatar: String)

Map in One Function

Write a small mapping function that turns a DTO into a domain model, all in one tidy, testable place.

fun UserDto.toDomain() = User(name = user_name, avatar = avatar_url ?: "")

Handle Nulls While Mapping

Mapping is the perfect spot to apply defaults, so missing or null JSON fields never crash the rest of the app.

Map Lists Too

For a list response, call map on the collection to convert every DTO into a domain item at once.

fun List<UserDto>.toDomain() = map { it.toDomain() }

Do It in the Repository

The repository fetches DTOs, then maps them to domain models, so callers only ever see the clean type.

override suspend fun getUsers() =
    client.get(url).body<List<UserDto>>().toDomain()

DTOs Stay Internal

Keep DTO classes internal to the data layer. Nothing outside the repository should ever import them.

Decoupled and Safe

Now the API can rename a field and you fix just the mapper, while the domain model and UI stay calm.

The Mapping Layer

This is the boundary in action: a DTO comes in, the mapper runs, and a clean domain model comes out.

val dto: UserDto = client.get(url).body()
val user: User = dto.toDomain()

Quick Check

Why map DTOs to separate domain models?

Recap

DTOs mirror raw JSON and stay in the data layer, while a mapping function turns them into clean domain models the rest of the app uses.

Często zadawane pytania

Czy lekcja „Mapowanie DTO na modele domenowe” jest bezpłatna?

Tak — pełny tekst „Mapowanie DTO na modele domenowe” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Mapowanie DTO na modele domenowe”?

Oddzielanie struktur sieciowych od typów domenowych Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?

Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Mapowanie DTO na modele domenowe”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?

Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Definiowanie interfejsu repozytorium
  2. Ukrywanie API za repozytorium
  3. Mapowanie DTO na modele domenowe
  4. Udostępnianie wyników jako Flow
← Powrót do Kotlin Multiplatform Academy