InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.98k stars 710 forks source link

Add bean/module destructor for Annotation-defined bean/module #1736

Closed vdshb closed 8 months ago

vdshb commented 9 months ago

Is your feature request related to a problem? Please describe.

My bean is defined with annotation:

@Module
class AppModule {
  @Single(createdAtStart = true)
  fun sqlDriver() : SqlDriver {
    //...
  }
}

I want to define bean/module close strategy to it. Effecitvely I want to run sqlDriver.close() during KoinApplication::close call.

Describe the solution you'd like The design might be something like this:

@Module
class AppModule {
  @Single(createdAtStart = true)
  fun sqlDriver() : SqlDriver {
    //...
  }
  @OnClose
  fun closeModule(sqlDriver: SqlDriver) {
    sqlDriver.close()
  }
}

Describe alternatives you've considered If bean is AutoClosable the bonus design might be something like this:

@OnClose(closeAutoClosable = true)
@Single(createdAtStart = true)
fun sqlDriver() : SqlDriver {
  //...
}

But conflicts resolution strategy with @OnClose and close order of dependant beans must be designed additionally.

Target Koin project Koin-Annotations

arnaudgiuliani commented 8 months ago

Hello, can you reopen it on https://github.com/InsertKoinIO/koin-annotations repo please? 🙏

vdshb commented 8 months ago

Done: https://github.com/InsertKoinIO/koin-annotations/issues/112

Feel free to close this one if there is no need for it.

arnaudgiuliani commented 8 months ago

thx