Dependency injection (DI) એ એક પેટર્ન છે જેમાં ઑબ્જેક્ટ્સ તેમની dependencies ને બાહ્યમાંથી મેળવે છે બદલે તેમને સ્વયં બનાવવાના — જે testability, maintainability અને flexibility ને બેહતર બનાવે છે। Android માં, Hilt (Dagger પર આધારિત) એ recommended DI framework છે.
DI શું છે અને શા માટે
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).
