0Pricing
Kotlin Multiplatform Academy · Lesson

Inject into ViewModels & Repos

Resolve dependencies cleanly throughout the app.

Inject into ViewModels & Repos is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Inject into ViewModels & Repos” lesson free?

Yes — the full text of “Inject into ViewModels & Repos” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Inject into ViewModels & Repos”?

Resolve dependencies cleanly throughout the app. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Inject into ViewModels & Repos” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declare a Shared Koin Module
  2. single vs factory Scopes
  3. Start Koin on Android & iOS
  4. Inject into ViewModels & Repos
← Back to Kotlin Multiplatform Academy