object GraphRegistry
The graph registry creates and holds dependency graphs in a tree (directed acyclic graph).
For example consider the following application component of a basic Android application:
GraphRegistry.applicationComponent = component { // the application component
subcomponent("presentation") { // A presentation subcomponent that survives orientation changes
subcomponent("activity") {} // The activity subcomponent that gets recreated with every device rotation
}
}
Create the application dependency graph on application start:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
GraphRegistry.open() { constant<Application> { this@MyApplication }
}
}
Create the presenter dependency graph by its path (of qualifiers) if it doesn't already exist:
if (!GraphRegistry.has("presentation") {
GraphRegistry.open("presentation")
}
Create the activity dependency graph by its path (of qualifiers):
GraphRegistry.open("presentation", "activity") { constant<Activity>(theActivityInstance) }
If you need multiple instances of the same subcomponent you can pass an identifier
parameter to the open method
to register the graph instance under a different identifier
than its component qualifier:
GraphRegistry.open("presentation", "activity", identifier: theActivityInstance) {
constant<Activity>(theActivityInstance)
}
Close the activity graph:
GraphRegistry.close("presentation", "activity")
Close the activity graph that was created with an identifier:
GraphRegistry.close("presentation", theActivityInstance)
GraphRegistry.close will remove and dispose all child dependency graphs from the registry. So in our example above the call:
GraphRegistry.close("presentation")
will also close all activity dependency graphs.
To get an instance of a dependency graph use get:
GraphRegistry.get() // Get the root dependency graph
GraphRegistry.get("presentation") // Get the presentation dependency graph
var applicationComponent: Component?
The application Component that's used to create the root dependency graph. |
fun close(vararg path: Any): Unit
Remove and dispose the dependency graph and its children stored in path. |
|
fun create(vararg path: Any, builderBlock: ComponentBuilderBlock? = null): Graph
Create and return dependency graph by (sub-)component path. |
|
fun get(vararg path: Any): Graph
Get a registered dependency graph by path. |
|
fun has(vararg path: Any): Boolean
Returns true if an entry under the given path exists otherwise false. |
|
fun open(vararg path: Any, identifier: Any? = null, builderBlock: ComponentBuilderBlock? = null): Graph
Create a dependency graph by (sub-)component path and register it. Opened components will be children of each other in left to right order. |