3

I'm a bit confused should I use Koin or any other dependency Injection for providing repositories to ViewModel or Should I use Kotlin object class for repositories so that we don't need to pass any dependency to ViewModel. Also for unit testing, we can mock kotlin object using mockk so no problem in mocking.

In my opinion, the first approach is very easy to create a Kotlin object but there may be a reason to use Koin or any other in this situation that I'm not aware of.

object SomeRepository{

    fun someFunction() {
        ApiHelper.getData()
    }
}

class SomeViewModel {

    fun someFunction(number: Long) {
       SomeRepository.someFunction() 
    }
}

OR

class SomeRepository(api:ApiHelper){

    fun someFunction() {
        api.getData()
    }
}

class SomeViewModel(repo: SomeRepository) {

    fun someFunction(number: Long) {
       repo.someFunction() 
    }
}

Using koin we can inject repository and API helper

Please help me to clarify which approach is best and why.

Alex Krupa
  • 520
  • 5
  • 8
Sachin Gurnani
  • 2,444
  • 7
  • 36
  • 45
  • 3
    No matter how u create Singleton(by Object or by Injection Scope) the dependencies has to be passed through the graph . Dependency Injection is much more than just providing Singletons. Its not the point to make a choice on using DI . [Here is a reference why you should go with DI](https://developer.android.com/training/dependency-injection). – ADM Mar 30 '22 at 07:16

0 Answers0