Why DI

Why Dependency Injection Matters on Android

A class that builds its own collaborators is hard-wired to them: a CatalogViewModel that calls BookRepository(assets) can never talk to a database, a server or a unit test's canned list. Dependency injection reverses that. A class declares what it needs as constructor parameters, and one place outside, the composition root, builds the objects and passes them in, much as a React 7,897 component receives an API client through props or a context provider. Android makes the pattern especially valuable:

BookNest's object graph after this section: arrows mean needs, bands are lifetimes
BookNest's object graph after this section: arrows mean needs, bands are lifetimes