Dependency injection (DI) je vzorec, pri katerem objekti prejmejo svoje odvisnosti od zunaj, namesto da bi jih ustvarili sami — kar izboljšuje testabilnost, vzdrževljivost in fleksibilnost. V Androidu je Hilt (zgrajen na Daggerju) priporočeni DI okvir.
Kaj je DI in zakaj
WITHOUT DI: a class CREATES its own dependencies (tightly coupled, hard to test/change):
class UserRepo { val api = ApiService() } // creates its own dependency — rigid
WITH DI: dependencies are PROVIDED from outside (injected):
class UserRepo(val api: ApiService) // receives it → flexible, testable
→ Benefits: TESTABILITY (inject mocks/fakes in tests), DECOUPLING, easier to swap
implementations, centralized dependency management, less boilerplate (with a framework).
