DependencyGraph

@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class DependencyGraph(val scope: KClass<*> = Nothing::class, val additionalScopes: Array<KClass<*>> = [])(source)

Declares the annotated type to be a dependency graph. Metro's compiler plugin will build, validate, and implement this graph at compile-time.

Graph types must be either an interface or an abstract class.

Scoping

See Scope before reading this section!

Graphs may declare a scope (and optionally additionalScopes if there are more). Each of these declared scopes act as an implicit SingleIn representation of that scope. For example:

@DependencyGraph(AppScope::class)
interface AppGraph

Is functionally equivalent to writing the below.

@SingleIn(AppScope::class)
@DependencyGraph(AppScope::class)
interface AppGraph

Creating a graph

For simple graphs with no creator types, an implicit one will be generated. You can instantiate them with createGraph.

val graph = createGraph<AppGraph>()

For creators (more below), you can create a factory with createGraphFactory.

val graph = createGraphFactory<AppGraph.Factory().create("hello!")

Providers

Graph types can declare providers via Provides and Binds to provide dependencies into the graph.

Graph creators can provide instance dependencies and other graphs as dependencies.

@DependencyGraph
interface AppGraph {
val httpClient: HttpClient

@Provides fun provideHttpClient: HttpClient = HttpClient()
}

Creators

Graphs can have creators. Right now, this just means Factory creators. See its doc for more details.

Aggregation

Graphs can automatically aggregate contributed bindings and interfaces. Any contributions to the same scope will be automatically aggregated to this graph. This includes contributions generated from ContributesTo (supertypes), ContributesBinding, ContributesIntoSet, and ContributesIntoMap.

@DependencyGraph(AppScope::class)
interface AppGraph

@ContributesTo(AppScope::class)
interface HttpClientProvider {
val httpClient: HttpClient
}

@ContributesBinding(AppScope::class)
@Inject
class RealHttpClient(...) : HttpClient

// Results in generated code like...
@DependencyGraph(AppScope::class)
interface AppGraph : HttpClientProvider {
/* fake */override val RealHttpClient.bind: HttpClient
}

Types

Link copied to clipboard
@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class Factory

Graph factories can be declared as a single nested declaration within the target graph to create instances with bound instances (via Provides) or graph dependencies.

Properties

Link copied to clipboard
Link copied to clipboard
val scope: KClass<*>