Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Dependency Injection

Edvin Syse edited this page Jan 9, 2016 · 7 revisions

WikiDocumentationDependency Injection

Dependency Injection

View and Controller are singletons, so you need some way to access the instance of a specific component. Tornado FX supports dependency injection, but you can also lookup components with the find function.

val myController = find(MyController::class)

When you call find, the component corresponding to the given class is looked up in a global component registry. If it did not exist prior to the call, it will be created and inserted into the registry before the function returns.

If you want to declare the controller referance as a field member however, you should use the inject delegate instead. This is a lazy mechanism, so the actual instance will only be created the first time you call a function on the injected resource. Using inject is always prefered, as it allows your components to have circular dependencies.

val myController: MyController by inject()

Injection of Fragment

Fragments are not singletons, so every time you use the find function, a new instance will be created for you. If you inject a Fragment, you will get a new instance every time you access the value. This might be convenient, but it could also be confusing. For that reason, you might want to use find when accessing fragments.

Clone this wiki locally