DTOs auf Domain-Models abbilden
Netzwerkstrukturen von Ihren Domänentypen trennen
DTOs auf Domain-Models abbilden ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „DTOs auf Domain-Models abbilden“ kostenlos?
Ja — der vollständige Text von „DTOs auf Domain-Models abbilden“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „DTOs auf Domain-Models abbilden“?
Netzwerkstrukturen von Ihren Domänentypen trennen Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?
Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „DTOs auf Domain-Models abbilden“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?
Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Ein Repository-Interface definieren
- Die API hinter dem Repository kapseln
- DTOs auf Domain-Models abbilden
- Ergebnisse als Flow bereitstellen