حقن الاعتماديات في ViewModels وRepos
حل الاعتماديات بنظافة في أنحاء التطبيق
حقن الاعتماديات في ViewModels وRepos درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
From Declared to Used
You registered dependencies and started Koin. Now comes the payoff: injecting those objects into your repositories and view models without building them by hand.
Constructor Injection First
The cleanest style is constructor injection: list what a class needs as parameters, and Koin fills them via get() in the module.
single { UserRepository(api = get()) }A Repo That Takes Deps
Your repository simply declares its needs in the constructor. It never touches Koin, which keeps it easy to test in isolation.
class UserRepository(private val api: ApiClient)Inject into a ViewModel
A shared ViewModel takes the repository through its constructor too, so all of its data comes from one injected source.
class UserViewModel(private val repo: UserRepository)Register the ViewModel
Add the view model to your module. A factory fits well, giving each screen its own fresh instance.
factory { UserViewModel(get()) }Resolve from Android Compose
On Android the koinViewModel() helper hands a Compose screen the wired-up view model in a single call.
val vm: UserViewModel = koinViewModel()Resolve from iOS
From Swift you ask Koin for the same view model through a small bridge function, so SwiftUI gets an identically wired object. 🍏
Avoid the Service Locator Trap
Prefer constructor injection over calling get() deep inside classes. Hidden lookups make code harder to read, reuse, and test.
Inject Interfaces, Not Classes
Depend on an interface and let Koin supply the implementation. Swapping in a fake for tests then becomes a one-line module change.
single<UserRepository> { UserRepositoryImpl(get()) }The Whole Chain
Notice the chain Koin assembles for you: client builds repo, repo builds use case, use case builds view model, all from constructors.
factory { UserViewModel(get(), get()) }Testing Becomes Easy
Because everything arrives through constructors, your tests just pass fakes directly, no Koin required, keeping unit tests fast and predictable.
Quick Check
Choose the cleanest injection style.
Recap
You injected dependencies through constructors into repos and view models, resolved them per platform, and saw why interfaces plus constructor injection make testing a breeze. 🎯
تعلم Kotlin مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «حقن الاعتماديات في ViewModels وRepos» مجاني؟
نعم — نص درس «حقن الاعتماديات في ViewModels وRepos» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «حقن الاعتماديات في ViewModels وRepos»؟
حل الاعتماديات بنظافة في أنحاء التطبيق تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «حقن الاعتماديات في ViewModels وRepos»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف وحدة Koin مشتركة
- نطاقا single وfactory
- بدء Koin في Android وiOS
- حقن الاعتماديات في ViewModels وRepos