DTOをDomain Modelsにマッピングする
ネットワーク上の形式とドメイン型を分離します
「DTOをDomain Modelsにマッピングする」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
AI チューターと学ぶ Kotlin — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「DTOをDomain Modelsにマッピングする」レッスンは無料ですか?
はい。「DTOをDomain Modelsにマッピングする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「DTOをDomain Modelsにマッピングする」で何を学びますか?
ネットワーク上の形式とドメイン型を分離します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「DTOをDomain Modelsにマッピングする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Repository Interfaceを定義する
- Repositoryの背後にAPIをラップする
- DTOをDomain Modelsにマッピングする
- ResultsをFlowとして公開する