Brian DiCasa
Brian DiCasa

Reputation: 9487

Passing object instance to constructor using Kodein

I'm new to Kotlin and Kodein. I'm trying to use a Java library and I need to pass a singleton to one of my constructors. I can't figure out how to get an actual instance. Since I need to pass an instance to the constructor I believe I need to be using DKodein so lazy loading isn't being used?

val kodein: DKodein = Kodein.direct {
    bind<DataSource>() with singleton {
        val config = HikariConfig()
        config.jdbcUrl = "jdbc:postgresql://localhost:5432/mydb"
        config.username = "username"
        config.password = "password"
        config.driverClassName = "org.postgresql.Driver"
        HikariDataSource(config)
    }
    bind<DatabaseContext>() with singleton {
        // I thought kodein.instance() here would provide the DataSource
        // instance I declared above. However I get the error (from Intellij)
        // TypeInference failed. Expected type mismatch.
        // Required: DataSource!
        // Found: KodeinProperty<???>
        DatabaseContext(kodein.instance()) 
    }
}

Is there any easy way to achieve this? Or am I going about this the wrong way?

Thanks.

Upvotes: 0

Views: 928

Answers (1)

Rene
Rene

Reputation: 6148

Within the initialization block of Kodein you must use instance() without kodein..

//use Kodein instead of DKodein
val kodein: Kodein = Kodein {
    bind<DataSource>() with singleton {
        val config = HikariConfig()
        config.jdbcUrl = "jdbc:postgresql://localhost:5432/mydb"
        config.username = "username"
        config.password = "password"
        config.driverClassName = "org.postgresql.Driver"
        HikariDataSource(config)
    }
    bind<DatabaseContext>() with singleton {
        //use instance() without kodein
        //or use dkodein.instance()
        DatabaseContext(instance()) 
    }
}

Kodein distinguishes between DKodein and Kodein.

  • DKodein allows the direct access of instances.
  • Kodein creates delegated properties via by.

The initialization block of Kodein provides access to both interfaces. But dkodein is the default. If you use kodein.instance() the property initializer version is used.

Outside of the initialization block, you can access DKodein like this:

val datasource: DataSource = kodein.direct.instance()

Upvotes: 3

Related Questions