I was also curious about this. If DI is so important, I would think that many libraries would use DI internally. Is Dagger really meant for app level code and not libraries? I would expect something like Retrofit or OkHttp to use Dagger
s
streetsofboston
03/19/2020, 1:08 AM
If it's a library that is used internally only, part of your suite of apps, go ahead and use Dagger or anything you like.
But for public libs, I'd suggest not using it (nor Koin, Kodein, etc). Try not to tie your libs users to a framework. However, make your library easy to use/configure with any DI library that the creators of apps could choose.
j
jw
03/19/2020, 1:31 AM
You can use Dagger in a library, you just shade it. It has a minimal runtime. You expose an interface that represents a component and then others can use it as a component dependency. Espresso does this, for example.
💯 2
c
Colton Idle
03/19/2020, 1:32 AM
Understood, but as I learn more about DI and it's importance, and how Dagger is used widely at square and such, why don't those libs use Dagger?
j
jw
03/19/2020, 1:32 AM
Well Retrofit and OkHttp still practice inversion of control. They don't really need Dagger
c
Colton Idle
03/19/2020, 1:35 AM
Okay. So it's mostly a "they don't need it" type of deal because they're small enough and they practice IOC, and if anything could do manual DI if they really need?
j
jw
03/19/2020, 1:35 AM
Yes. And also everything in the two are scoped to the Retrofit or OkHttpClient instance. They don't have scopes.
c
Colton Idle
03/19/2020, 1:37 AM
Thanks. That helps me in understanding when is the right time to introduce a DI framework like Dagger.