Map DTOs to Domain Models
Separate network shapes from your domain types.
Map DTOs to Domain Models is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Map DTOs to Domain Models” lesson free?
Yes — the full text of “Map DTOs to Domain Models” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Map DTOs to Domain Models”?
Separate network shapes from your domain types. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Map DTOs to Domain Models” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Define a Repository Interface
- Wrap the API Behind the Repository
- Map DTOs to Domain Models
- Expose Results as Flow