0Pricing
Kotlin Multiplatform Academy · درس

وضع API خلف Repository

إخفاء Ktor وSerialization عن المستدعِين

وضع API خلف Repository درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Now Build the Real Thing

With the interface set, you write a concrete implementation that actually fetches data behind the scenes.

Implement the Interface

Create a class that uses implements the repository interface, promising to fulfill every method it declares.

class UserRepositoryImpl : UserRepository

Inject the Ktor Client

Pass the HttpClient into the constructor instead of creating it inside, so it can be shared and replaced in tests.

class UserRepositoryImpl(private val client: HttpClient) : UserRepository

Hide the Network Call

Inside each method, make the Ktor request and return the result. Callers see clean data, never the request itself.

override suspend fun getUsers() = client.get(url).body<List<User>>()

Keep URLs Inside

Store base URLs and paths within the implementation. The rest of the app stays blissfully unaware of any endpoint.

One Place to Change

If the API path changes, you edit only this class. The interface and every caller stay untouched.

Serialization Stays Hidden

The body call turns JSON into models here, so serialization never leaks out to the UI layer.

val user: User = client.get(url).body()

Same Code, Both Apps

This implementation lives in commonMain, so the same fetching code powers Android and iOS identically.

Swap Without Breaking Callers

Because it satisfies the interface, you can replace it with a caching version later and no caller changes.

Wire It With DI

A dependency injection setup hands callers this implementation through the interface type, keeping things loosely coupled.

single<UserRepository> { UserRepositoryImpl(get()) }

The Full Wrapper

Here is the complete wrapper: it takes a client, hides the request, and returns plain domain models.

class UserRepositoryImpl(
    private val client: HttpClient
) : UserRepository {
    override suspend fun getUsers() =
        client.get("/users").body<List<User>>()
}

Quick Check

What is the main job of the repository implementation?

Recap

The implementation takes an HttpClient, performs the request, and returns domain models, keeping all networking sealed off from callers.

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

هل درس «وضع API خلف Repository» مجاني؟

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

ماذا ستتعلم في «وضع API خلف Repository»؟

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

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

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

كم من الوقت يستغرق درس «وضع API خلف Repository»؟

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

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

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

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

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