Dependency injection (DI) हा एक pattern आहे जिथे objects त्यांचे 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).
