Is it OK to rely on a child component to provide a...
# kotlin-inject
e
Is it OK to rely on a child component to provide a dependency? For example:
Copy code
class ResolverWrapper(val resolver: ContentResolver)

@Component abstract class ParentComponent {
  @Provides fun providesContentResolve(context: Context) = context.contentResolver
}

@Component abstract class ChildComponent(
  @Component val parent: ParentComponent,
  @get:Provides val context: Context
) {
  abstract val wrapper: ResolverWrapper
}
The above works, but is it OK to rely on this behavior?
e
Interesting, will have to take a closer look, I don't really see any issues with this example but it could be a problem with scopes. Ex:
Copy code
class ResolverWrapper(val resolver: ContentResolver)

@Singleton
@Component abstract class ParentComponent {
  @Provides @Singleton fun providesContentResolve(context: Context) = context.contentResolver
}

@Component abstract class ChildComponent(
  @Component val parent: ParentComponent,
  @get:Provides val activityContext: Context
) {
  abstract val wrapper: ResolverWrapper
}
Mind filing an issue?
Specifically, you could get a leak if child dependency is provided to a parent dependency with a longer lifetime
e
Sure, I'll put something together tomorrow
What would be the solution to this issue? I don't think blocking the behavior is necessarily the right way, because while leaking an activity is bad, there are other valid use cases that could do this.
e
there are other valid use cases that could do this.
Agreed, will have have to think about it. If you want to include your usecase for this behavior that would be useful in figuring out what to do.