0Pricing
Kotlin Multiplatform Academy · درس

حقن تطبيقات المنصة

وفّر actual المناسب عبر Koin أو المصانع.

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

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

Who Supplies the Real Thing

An interface defines a capability, but something must hand shared code the real platform implementation. That wiring is dependency injection.

Avoid new in Shared Code

Shared logic should not construct platform classes itself. Instead it receives the implementation from outside, keeping it decoupled and testable.

Inject via Constructor

The simplest approach is a constructor parameter. Pass the capability in, and the class never knows which platform built it.

class Profile(private val info: DeviceInfo)

Provide the Android Instance

On Android, the app module creates the real actual and passes it down when it builds the shared object.

val profile = Profile(DeviceInfo())

Provide the iOS Instance

On iOS, Swift constructs the framework's DeviceInfo and injects it the same way, so shared code stays identical across platforms.

let profile = Profile(info: DeviceInfo())

Scale Up With Koin

For many dependencies, a DI library like Koin declares each capability once in a shared module and resolves it on demand.

val appModule = module {
  single<DeviceInfo> { DeviceInfo() }
}

Bind Interface to Actual

In Koin you bind the interface type to the platform implementation, so callers ask for DeviceInfo and get the right concrete one.

single<Storage> { PlatformStorage() }

Resolve Where You Need It

Anywhere in shared code you can pull a dependency from the graph by its type, no manual construction required.

val info: DeviceInfo by inject()

Swap Fakes in Tests

Because everything is injected, a test simply provides a fake implementation. No platform, emulator, or device is involved. ✅

Profile(FakeDeviceInfo())

One Wiring Point per App

Keep injection at each app's entry point. That single setup spot decides which actuals run, while shared code stays blissfully unaware.

Factories for Cheap Cases

If a DI library feels heavy, a plain factory function works too. It builds the platform object in one place and hands it to shared code.

fun deviceInfo(): DeviceInfo = DeviceInfo()

Quick Check

Think about why shared code receives its dependencies.

Recap

You learned to inject platform implementations via the constructor or Koin, binding interfaces to actuals so shared code stays clean and testable. 🎉

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

هل درس «حقن تطبيقات المنصة» مجاني؟

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

ماذا ستتعلم في «حقن تطبيقات المنصة»؟

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

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

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

كم من الوقت يستغرق درس «حقن تطبيقات المنصة»؟

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

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

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

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

  1. تصميم واجهة للقدرات
  2. expect/actual لمعلومات الجهاز
  3. نظام الملفات والمسارات لكل منصة
  4. حقن تطبيقات المنصة
← العودة إلى Kotlin Multiplatform Academy