One thing from like maybe like wayyyy back when at...
# dagger
c
One thing from like maybe like wayyyy back when at Droidcon montreal I feel like I remember Jake saying that one of the benefits of something like Dagger is that its helpful vs other DI approaches because you can also swap dependencies at runtime (so you still get compile time safety, but there was like some runtime aspect to it). This was like 10 years ago at this point and was curious if anyone recalls if anything I'm remembering is true at all 🙈
j
Not sure. Ultimately instances are only available at runtime, though, so you can do whatever you want at runtime with those. Dagger is the roadways between things and those are fixed, but you can still choose what actual values flow between them at runtime using normal logic.
❤️ 1
👍 1
Like you can do
Copy code
@Provides fun database(environment: Environment): Database {
  return when (environment) {
    Staging -> InMemoryDatabase()
    Production -> RealDatabase()
  }
}
👍 1
You can also of course reinitialize entire components when things change. Like having a component whose lifetime is tied to the logged-in user. It's not created until you have an account credential, and it goes away when they log out. Inside that component the account credentials are always known, because it can only exist when those credentials are valid.
If you don't put a scoping annotation on a provider function then it will be called each time the dependency is requested. You don't have to return the same thing each time. Just keep in mind that multiple objects might be alive which have different values for the same binding then, though, which could get confusing. But Dagger doesn't care either way and lets you do this.
3
u
Inside that component the account credentials are always know
do you mean
@BindsInstance
on compoment factory?