Dependency injection (DI) एक pattern हो जहाँ objects ले आफ्नो dependencies बाहिरबाट प्राप्त गर्छन् बरु आफैले सिर्जना गर्नुको सट्टामा — जसले testability, maintainability, र flexibility लाई सुधार गर्छ। Android मा, Hilt (Dagger मा निर्मित) अनुशंसित 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).
