BindingContainer
Binding containers are classes or interfaces that contain binding callables annotated with Provides or Binds.
Usage with DependencyGraph.Factory
Instances of binding containers must be added to dependency graphs via DependencyGraph.Factory parameters annotated with @Includes.
@BindingContainer
class NetworkBindings(val baseUrl: String) {
@Provides fun provideHttpClient(): HttpClient = HttpClient(baseUrl)
}
@DependencyGraph
interface AppGraph {
@DependencyGraph.Factory
interface Factory {
fun create(@Includes networkBindings: NetworkBindings): AppGraph
}
}
Usage with DependencyGraph.bindingContainers
In certain cases, you can declare the class in DependencyGraph.bindingContainers:
interfaces or abstract classes with only Binds providers or companion object Provides providers.
simple, non-generic classes with a public, no-arg constructor
object classes
@BindingContainer
abstract class NetworkBindings {
@Provides abstract fun RealHttpClient.bind: HttpClient
}
@DependencyGraph(includes = [NetworkBindings::class])
interface AppGraph
Binding containers added via DependencyGraph.Factory parameters do not need to be declared in DependencyGraph.bindingContainers.
Binding containers may be contributed with ContributesTo and can replace other contributed binding containers.
Notes
It is an error to annotate companion objects, annotation classes, or enum classes/entries.
companion object providers within a binding container are automatically included.
Unannotated callables in binding containers are ignored.
Enclosing classes of Binds or Provides providers do not need to be annotated with BindingContainer for Metro to process them. This annotation is purely for reference to DependencyGraph.Factory and the ability to use includes.
Binding containers in Metro are analogous to Dagger's
@Module
.
Properties
An optional array of more binding containers that this one may transitively include. Note that these must abide by the same requirements as arguments to DependencyGraph.bindingContainers.