首页 > 解决方案 > 使用 Dagger 提供没有接口的实例

问题描述

有一种方法可以在没有实际构造函数调用的情况下提供实例。

class ModelImpl @Inject constructor(...): Model{}

@Provides
fun model(inst: ModelImpl): Model = inst

如果没有接口,有没有办法做同样的事情?Dagger已经知道 的所有依赖项ModelImpl,因此它可以创建一个实例。

这显然给出了依赖循环:

@Provides
fun model(inst: ModelImpl): ModelImpl = inst

标签: kotlindagger-2

解决方案


当您使用构造函数注入时,Dagger 可以为您构造对象,并且您已经在使用 Dagger 创建ModelImpl以将其用作Model示例中的绑定!

class ModelImpl @Inject constructor(...): Model{}

@Provides
fun model(inst: ModelImpl): Model = inst

// somewhere else...
// both variants would work!
@Inject lateinit var modelImpl : ModelImpl
@Inject lateinit var model : Model

没有接口也一样

class ModelImpl @Inject constructor(...)

// somewhere else...
@Inject lateinit var model : ModelImpl

如果您注释构造函数,那么 Dagger 可以为您创建对象(如果可以解决所有依赖项)。无论您请求对象/依赖项,这都是一样的,

  • 作为带@Provides注释的方法中的参数(作为您的示例)
  • 作为字段注入属性 ( @Inject lateinit var)
  • 作为另一个对象构造函数中的参数
  • 作为组件中的提供方法 ( fun getFoo() : Foo)

以下所有方法都可以

// foo and bar can both be constructor injected
class Foo @Inject constructor()
class BarImpl @Inject constructor(val foo : Foo) : Bar

@Module
interface BarModule() {
  @Binds  // you should prefer Binds over Provides if you don't need initialization
  // barImpl can be constructor injected, so it can be requested/bound to its interface here
  fun bindBar(bar : BarImpl) : Bar
}

@Component(modules = BarModule::class)
interface BarComponent {
  fun getBar() : Bar // returns barImpl due to binding
}

@Inject lateinit var bar : BarImpl // but we could as well use the implementation directly
@Inject lateinit var bar : Foo // or just foo

我建议您从一个小示例开始,然后编译项目并查看生成的代码。如果出现问题,您将立即收到错误,同时您可以四处玩耍并尝试不同的设置!


推荐阅读