Wstrzykiwanie zależności (DI) to wzorzec, w którym obiekty otrzymują swoje zależności z zewnątrz, zamiast je tworzyć — poprawiając testowalność, łatwość utrzymania i elastyczność. W Androidzie Hilt (zbudowany na Daggerze) to rekomendowany framework DI.
Czym jest DI i dlaczego to ważne
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).
