0Pricing
Kotlin Multiplatform Academy · Lektion

In ViewModels und Repos injizieren

Abhängigkeiten sauber in der gesamten App auflösen

In ViewModels und Repos injizieren ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🎯

Häufig gestellte Fragen

Ist die Lektion „In ViewModels und Repos injizieren“ kostenlos?

Ja — der vollständige Text von „In ViewModels und Repos injizieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „In ViewModels und Repos injizieren“?

Abhängigkeiten sauber in der gesamten App auflösen Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?

Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „In ViewModels und Repos injizieren“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?

Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Ein gemeinsames Koin-Modul deklarieren
  2. single- vs. factory-Scopes
  3. Koin auf Android und iOS starten
  4. In ViewModels und Repos injizieren
← Zurück zu Kotlin Multiplatform Academy