createDynamicGraph

inline fun <T : Any> createDynamicGraph(vararg containers: Any): T(source)

Creates a new dynamic graph of type T with the given set of dynamic containers. Note this is only applicable for graphs that have no creators (i.e. DependencyGraph.Factory).

@DependencyGraph
interface AppGraph {
val message: String

@Provides fun provideMessage(): String = "real"
}

class AppTest {
val testGraph = createGraph<AppGraph>(FakeBindings)

@Test
fun test() {
assertEquals("fake", testGraph.message)
}

@BindingContainer
object FakeBindings {
@Provides fun provideMessage(): String = "fake"
}
}

Dynamic graphs are a powerful feature of the Metro compiler that allow for dynamically replacing bindings in a given graph. The compiler will generate a dynamic graph within the enclosing class or file that is unique to the combination of input containers and target type T.

These should be used with care and are generally reserved for tests.

Constraints

  • All containers must be instances (or objects) of binding containers.

  • It's an error to pass no containers.

  • All containers must be non-local, canonical classes. i.e., they must be something with a name!

  • This overload may be called in a member function body, top-level function body, or property initializer.

  • The target T graph must be annotated with @DependencyGraph and must be a valid graph on its own.