edvin / tornadofx

Lightweight JavaFX Framework for Kotlin
Apache License 2.0
3.68k stars 272 forks source link

TornadoFX UI with non-TornadoFX business logic and Dagger injection #1317

Open pderoovere opened 3 years ago

pderoovere commented 3 years ago

We’re working on a project where the business logic is implemented in a separate module using non-TornadoFX types. There’s a (non-TornadoFX) event system in place and we're using Dagger for dependency injection. We want to build a TornadoFX UI on top of this.

We're struggling with:

A simplified example would be:

data class Entry(...)
interface EntryDao {
    val allEntries: List<Entry>
    fun register(listener: EntryListener)   
}

Our approach would be to:

  1. Create an EntryController that gets the EntryDao injected (and optionally creates an EntryViewModel, as in the withpojo example). This EntryController would register on EntryDao's changes.

    @Singleton
    class EntryController @Inject constructor (private val entryDao: EntryDao) { ... }
  2. Create one (or more) View(s) that visualize data from (and possibly trigger actions using) the EntryController obtained using by di().

    class EntryView : View() {
    val controller: EntryController by di()
    ...
    }
  3. Implement the DIContainer interface in a way that all Controllers (and their dependencies) are created and wired automagically using Dagger.

    @Singleton
    class Controllers @Inject constructor(entryController: EntryController, ...) {
    ...
    fun <T : Any> getInstance(type: KClass<T>): T { ... }
    }

Would this be the best way of tackling this?

SchweinchenFuntik commented 3 years ago

I think not yet. DI at tornadofx would be worth revisiting