2

I called a ViewModel that gets three arguments in its constructor:

class DetailViewModel(
    private val id: Long,
    private val tipo: String,
    private val msRepository: MSRepository) : ViewModel() {
val establishmentProfessional by lazyDeferred { 
      msRepository.getEstablishment(id, tipo)
   }
}

And I created a factory to build the object com DI:

class DetailViewModelFactory(
    private val id: Long,
    private val tipo: String,
    private val msRepository: MSRepository
    ) : ViewModelProvider.NewInstanceFactory() {

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
    return DetailViewModelFactory(id,  tipo, msRepository) as T
     }
}

I wrote the Factory binding as follows:bind() from factory { id: Long, entity: String -> DetailViewModelFactory(id, entity, instance()) }, the latter being previously reported.

However, when I try to retrieve the object, I can not spell the code correctly:

private var viewModelFactory: ( (Long, String) -> DetailViewModelFactory) by factory()

How do I retrieve the object correctly with a series of arguments?

I did not find in the documentation how to do it, because the information is insufficient.Retrieval Direct, multiple argumentes, Little documentation on multi-argument factories, How can I use Kodein's direct retrieval to fetch a dependency bound as a factory?

user67223
  • 33
  • 1
  • 6

1 Answers1

0

If you want to retrieve a facotry and not an instance, use by factory2().

If you want to retrieve an instance, use by instance(arg = M(42L, "whatever")).

Salomon BRYS
  • 9,247
  • 5
  • 29
  • 44
  • 1
    I used instance(arg=M(id, type)), but in debug inform "Lazy value not initialized yet." for the variable. – user67223 Jul 30 '19 at 14:45