InsertKoinIO / koin-annotations

Koin Annotations - About Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform insert-koin.io
https://insert-koin.io
Apache License 2.0
154 stars 40 forks source link

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

Open vdshb opened 8 months ago

vdshb commented 8 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

yes, definitely need to propose around closing definitions with onClose