如何注入Map<>或设置<> (牙签2个多重装订)牙签

时间:2017-12-27 11:47:40

标签: android dependency-injection toothpick-di

Dagger 2能够使用多重绑定注入集合或映射。 如何在牙签中做到这一点

例如我想要的 @Inject constructor(val map: Map<String, ISyncRepository>)

1 个答案:

答案 0 :(得分:1)

可能就像:

   @Binds
   @IntoMap
   @TheMapKey(“your key”)
   abstract fun getSyncRepository() : ISyncRepository

查看以下2个样本

https://google.github.io/dagger/multibindings.html

https://blog.kotlin-academy.com/understanding-dagger-2-multibindings-viewmodel-8418eb372848

代表

@Inject constructor(

     private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards 
         Provider<ViewModel>>

) 

使用:

@Binds
@IntoMap
@ViewModelKey(UserViewModel.class)
abstract ViewModel bindUserViewModel(UserViewModel userViewModel);