1 year ago

#352763

test-img

Sachin Gurnani

Koin singleton injection vs Kotlin object

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.

android

unit-testing

kotlin

dependency-injection

koin

0 Answers

Your Answer

Accepted video resources