Dependency Injection for ViewModels
Inject services to keep view models testable.
The Hidden Dependency Problem
If a ViewModel creates its own network client inside itself, tests are stuck hitting the real server. That tight coupling makes it hard to swap later.
Inject Instead of Create
Dependency injection means passing in what a ViewModel needs from outside, rather than letting it build those services itself.
All lessons in this course
- Why MVVM Fits SwiftUI
- Designing a ViewModel
- Binding Views to ViewModels
- Dependency Injection for ViewModels