首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关闭/关闭Kodein上下文

关闭/关闭Kodein上下文
EN

Stack Overflow用户
提问于 2018-06-21 16:13:45
回答 1查看 269关注 0票数 0

比如说,在我的应用程序中,我维护了几个Kodein上下文,在Kodein上下文中有一些共享资源,当它所属的上下文不再需要时,我想结束这些资源。

以下是这个问题的简单说明:

代码语言:javascript
复制
class SomeConnectionPool: Closeable {
    override fun close() { /* some operation */ }
}

class SomeResource: Closeable {
    override fun close() { /* some operation */ }
}

class SomeService(val pool: SomeConnectionPool) {
    fun doStuff() { /* some operation */ }
}

class SomeOtherService(val pool: SomeConnectionPool) {
    fun doOtherStuff() { /* some operation */ }
}

val kodein = Kodein {
    bind<SomeConnectionPool>() with singleton { SomeConnectionPool() }
    bind<SomeResource>() with singleton { SomeResource() }

    bind<SomeService>() with singleton { SomeService(instance()) }
    bind<SomeOtherService>() with singleton { SomeOtherService(instance()) }
}

fun main(args: Array<String>) {
    val service: SomeService by kodein.instance()
    service.doStuff()

    // this will initialize everything even the unused bindings
    val resources by kodein.allInstances<Closeable>()
    resources.forEach { it.close() }
}

理想情况下,应该实现以下几个特性:

  • 仅检索已初始化的可关闭实例并关闭它们。
  • SomeServiceSomeOtherService不应该负责关闭SomeConnectionPool,因为它们没有创建实例。他们也不知道池是否还在被其他东西使用。

我还考虑只从kodein.container检索初始化的绑定,但似乎没有明显的方法可以这样做。

EN

回答 1

Stack Overflow用户

发布于 2018-06-22 11:20:58

科迪因5.1有你的报道吗。

看一下:

  • http://kodein.org/Kodein-DI/?5.1/core#scope-closeable
  • scopes
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50973181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档