Iniettare dipendenze in ViewModel e Repository
Risolvere le dipendenze in modo ordinato in tutta l'app
Iniettare dipendenze in ViewModel e Repository è una lezione Kotlin Multiplatform Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Kotlin Multiplatform Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🎯
Domande Frequenti
La lezione «Iniettare dipendenze in ViewModel e Repository» è gratuita?
Sì — il testo completo di «Iniettare dipendenze in ViewModel e Repository» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Kotlin Multiplatform Academy, passa a CoddyKit PRO. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.
Cosa imparerò in «Iniettare dipendenze in ViewModel e Repository»?
Risolvere le dipendenze in modo ordinato in tutta l'app Eserciti Kotlin Multiplatform Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Kotlin Multiplatform Academy?
Non è richiesta alcuna esperienza precedente. Kotlin Multiplatform Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Iniettare dipendenze in ViewModel e Repository»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Kotlin Multiplatform Academy?
Sì. Ogni lezione Kotlin Multiplatform Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Dichiarare un modulo Koin condiviso
- Scope single e factory
- Avviare Koin su Android e iOS
- Iniettare dipendenze in ViewModel e Repository