แมป DTO เป็นโมเดลโดเมน
แยกรูปแบบข้อมูลเครือข่ายออกจากชนิดข้อมูลโดเมน
แมป DTO เป็นโมเดลโดเมน เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
คำถามที่พบบ่อย
บทเรียน “แมป DTO เป็นโมเดลโดเมน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แมป DTO เป็นโมเดลโดเมน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แมป DTO เป็นโมเดลโดเมน”
แยกรูปแบบข้อมูลเครือข่ายออกจากชนิดข้อมูลโดเมน คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “แมป DTO เป็นโมเดลโดเมน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- กำหนดอินเทอร์เฟซ Repository
- ห่อ API ไว้เบื้องหลัง Repository
- แมป DTO เป็นโมเดลโดเมน
- เปิดเผยผลลัพธ์เป็น Flow