ViewModelsとReposにInjectする
アプリ全体で依存関係をすっきり解決します
「ViewModelsとReposにInjectする」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🎯
よくある質問
「ViewModelsとReposにInjectする」レッスンは無料ですか?
はい。「ViewModelsとReposにInjectする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「ViewModelsとReposにInjectする」で何を学びますか?
アプリ全体で依存関係をすっきり解決します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「ViewModelsとReposにInjectする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 共有Koin Moduleを宣言する
- singleとfactoryのScopes
- AndroidとiOSでKoinを起動する
- ViewModelsとReposにInjectする