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).
