Kotlin Multiplatform Academy · درس

تحويل DTOs إلى نماذج النطاق

فصل أشكال الشبكة عن أنواع النطاق لديكم

الدرس 3 من 413 خطوة

تحويل DTOs إلى نماذج النطاق درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.

البدء مجانًا

تعلم Kotlin مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «تحويل DTOs إلى نماذج النطاق» مجاني؟

نعم — نص درس «تحويل DTOs إلى نماذج النطاق» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «تحويل DTOs إلى نماذج النطاق»؟

فصل أشكال الشبكة عن أنواع النطاق لديكم تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «تحويل DTOs إلى نماذج النطاق»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تعريف واجهة Repository
  2. وضع API خلف Repository
  3. تحويل DTOs إلى نماذج النطاق
  4. كشف النتائج على هيئة Flow
← العودة إلى Kotlin Multiplatform Academy