تعريف وحدة Koin مشتركة
تسجيل الاعتماديات في commonMain
تعريف وحدة Koin مشتركة درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Wire Things Together
Your app is full of objects that need each other: a repository needs a client, a use case needs the repository. Dependency injection hands those pieces over for you.
Meet Koin
Koin is a lightweight DI library written in pure Kotlin, so it runs happily in commonMain and works on both Android and iOS. 🎉
Add the Dependency
You pull Koin into your shared module the same way you add any other library, listing the core artifact in your version catalog or build file.
implementation("io.insert-koin:koin-core:3.5.0")A Module Is a Recipe Book
A Koin module is just a list of recipes telling Koin how to build each dependency. You declare it once and reuse it everywhere.
Write Your First Module
The module { } builder collects your definitions. An empty one is valid; you fill it with recipes next.
val appModule = module {
}Register with single
Inside the module, single { } registers one shared instance Koin reuses for the whole app, perfect for a repository.
val appModule = module {
single { UserRepository() }
}Wire Dependencies with get
When one object needs another, call get() inside the recipe. Koin looks up the matching definition and passes it in automatically.
single { UserRepository(api = get()) }Order Does Not Matter
You can declare definitions in any order. Koin resolves the graph lazily, so a recipe can safely get() something defined further down.
Keep It in commonMain
Because Koin is multiplatform, place appModule in commonMain. Both your Android and iOS apps then share the exact same wiring.
Group Related Modules
Large apps split definitions into several modules, like a data module and a domain module, then combine them later when starting Koin.
val dataModule = module { single { ApiClient() } }A Function That Returns It
A common pattern exposes your module from a function, making it easy for each platform to grab the shared definition.
fun appModule() = module {
single { UserRepository(get()) }
}Quick Check
Let's lock in what a Koin module does.
Recap
You met Koin, added it to your shared module, and wrote a module that registers dependencies with single and links them using get. One recipe book, both platforms. 🚀
الأسئلة الشائعة
هل درس «تعريف وحدة Koin مشتركة» مجاني؟
نعم — نص درس «تعريف وحدة Koin مشتركة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «تعريف وحدة Koin مشتركة»؟
تسجيل الاعتماديات في commonMain تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «تعريف وحدة Koin مشتركة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف وحدة Koin مشتركة
- نطاقا single وfactory
- بدء Koin في Android وiOS
- حقن الاعتماديات في ViewModels وRepos