- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在研究 kodein 时,我经常看到 bind() with 和 bind() from。
谁能告诉我有什么区别以及我们为什么要使用它。
例如:
bind<Dice>() with provider { RandomDice(0, 5) }
bind<DataSource>() with singleton { SqliteDS.open("path/to/file") }
bind() from singleton { RandomDice(6) }
bind("DnD20") from provider { RandomDice(20) }
bind() from instance(SqliteDataSource.open("path/to/file"))
最佳答案
bind<Type>() with
定义 Type
明确地。例如,当您将接口(interface)类型绑定(bind)到它的实现时,这一点很重要:
bind<Type>() with singleton { TypeImpl() }
现在考虑您正在绑定(bind)一个非常简单的类型,例如配置数据对象:
bind<Config>() with singleton { Config("my", "config", "values") }
你写了Config
两次:一次在绑定(bind)定义中,一次在绑定(bind)本身中。
输入 bind() from
:它不定义类型,而是将绑定(bind)类型的选择留给绑定(bind)本身。绑定(bind)类型是隐式定义的。例如,您可以编写 Config
这样的绑定(bind):
bind() from singleton { Config("my", "config", "values") }
请注意,将类型绑定(bind)到自身(这就是 bind() from
的用途)通常不是一个好主意(它违反了 IoC 模式)并且应该只用于非常简单的类型,例如数据类。
关于Android kodein **bind() with** 和 **bind() from** 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60163838/
使用 Kodein,我发现我必须传递 kodein 实例或将它们注入(inject)到模块和类中。但有时我的类(class)是如此脱节,我希望他们能够发现“一个真正的 Kodein”。由于这是一个服务
TL;博士 在 JCenter 访问 Kodein 核心包是未经授权的。 详情 我们正在使用 Kodein 进行依赖注入(inject),但是当 Gradle 尝试下载任何 org.kodein.*
这是完整的错误: e: C:\Users\HP\AndroidStudioProjects\MoneyManager\app\src\main\java\com\cruxrepublic\moneym
在 Kodein 中,我将模块导入到父模块中,有时这些类需要一个 Kodein 实例,以便以后可以自己进行注入(inject)。问题是这段代码: val parentModule = Kodein {
我正在使用 Kodein作为我的 kotlin 的注入(inject)依赖项项目。所以我有一个 ViewModel 工厂,如下所示 class ReadViewModelFactory(private
我正在为我的 LibGDX 项目使用 kodein(Kodein.global 变体)。许多 LibGDX 对象都有自己的清理方法(dispose 和 Disposable 接口(interface)
我刚刚学习了用于依赖注入(inject)的kodein lib。我如何获取变量的依赖值?我在下面的应用程序类中,我将上下文与 kodein 绑定(bind): class BaseApp : Appl
在研究 kodein 时,我经常看到 bind() with 和 bind() from。 谁能告诉我有什么区别以及我们为什么要使用它。 例如: bind() with provider {
我们有一个 Android 应用程序使用 compose 作为 View 层,并且我们使用 Kodein 进行所有依赖注入(inject)。 我有一个 DIAware BaseApplication
我有 Android/kotlin 多模块项目。我的一些模块是 Singletone,例如 Network 和 Analytics 模块。它们需要应用程序上下文才能被初始化。 我可以在 app 模块中
我在 Android 上使用 Kodein 进行依赖项注入(inject)(当然在 Kotlin 中),但我在一个方面苦苦挣扎:我似乎无法将 lambda 作为参数传递给工厂。它编译正确但在运行时失败
( x-post from /r/androiddev ) 我只想在序言中说这不是一篇“哪个更好”的帖子;这严格来说是一个关于如何使用 Dagger 构建某些东西的问题(以及如何在 Kodein 中构
我用 MVVM 模式开发应用程序。我想在用户旋转屏幕时保存 UI。 MyViewModel.kt class MyViewModel(val repository: SomeRepository,
我需要为一个新项目学习 Dagger 2,并且我正在努力理解这一切。 我看过一些教程,这些教程提供了一些清晰度,但我仍然对很多内容感到困惑,例如各种移动部分(组件、模块、注入(inject)器、提供程
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 2 年前。 Improve
最近几天我一直在与这个异常作斗争。 我有一个包含这些依赖项的 kotlin 多平台项目: kotlin=1.5.10 kodein=7.6.0 ktor=1.6.0(内部使用 kotlin 协程 1.
在这个工厂中,我需要使用 Retrofit 从 api 获取我的数据并将缓存与空间一起存储,我的存储库规则这个应用程序! 我有存储库暂停功能,负责获取我的数据,一些保存/更新数据获取和保存/更新需要不
有没有办法创建一个可以产生 null 的工厂绑定(bind)? 例如, bind with factory { x: Int -> when (x) { 1 -> "A"
我有一个非常简单的设置。但是我无法在我正在使用的 fragment 中执行任何网络 Kodein在。 fragment 膨胀并处理所有交互,但网络层依赖项不知何故丢失,因为我收到:NotFoundEx
想用 Kodein DI Android 在非 Activity 或 Fragment 类中初始化接口(interface) 示例仅显示在 activity 中使用 Kodein,但在其他部分没有显示
我是一名优秀的程序员,十分优秀!