android - 如何用 Toothpick 注入(inject) Map<> 或 Set<> (dagger 2 multibindings)

标签 android dependency-injection toothpick-di

Dagger 2 能够使用多重绑定(bind)注入(inject)集合或映射。 如何在 Toothpick 中做到这一点

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

最佳答案

它可能是这样的:

   @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);

关于android - 如何用 Toothpick 注入(inject) Map<> 或 Set<> (dagger 2 multibindings),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991625/

相关文章:

java - 使用 Toothpick DI 框架与 Java(仅限)项目一起使用

android - 为什么android :colorBackground work?没有

android - 如何强制 React Native 应用程序只是 RTL?

android - 需要在 android 中动态创建 View 的建议

java - 是否存在可以接受使用构造函数调用创建的 Spring bean 的用例?

c# - 简易喷油器 : Registering a type with constructor argument that's based on its parent

android - 来自 iOS 的 CLLocation 在 Android 中的类(class)

c# - 创建使用控制反转的对象

android - 如何使用proguard牙签规则?