https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
i

Ive Vasiljevic

07/24/2019, 3:30 PM
Should I make a repository as an
Object
or
Class
m

Marko Mitic

07/24/2019, 3:37 PM
Class if you need to inject some dependencies into repository
👍 3
u

ursus

07/24/2019, 4:47 PM
you cannot pass parameters to
object
p

pavi2410

07/24/2019, 5:44 PM
Copy code
class MyRepository(private val apiService: ApiService) {

    fun getMyStuff() = apiCall { apiService.getMyStuff() }

    private fun <T> apiCall(call: suspend () -> Response<ResponseWrapper<T>>) = liveData {
        emit(Result.Loading)

        try {
            val response = call()

            if (response.isSuccessful) {
                response.body()?.let {
                    if (it.success)
                        emit(Result.Success(it.data))
                    else
                        emit(Result.Error(it.message))
                }
            }
        } catch (e: Exception) {
            emit(Result.Error())
        }
    }
}
This is how I write Repository
m

Marko Mitic

07/24/2019, 6:34 PM
Yeah, you can't pass apiService to object, keep it a class
g

gildor

07/25/2019, 2:35 AM
IMO Even for mid size project any
object
with business logic become a problem for testability and modularization It’s usually just a matter of time when it rewritten with proper interface/class and DI
j

Joan Colmenero

07/25/2019, 2:37 PM
@pavi2410 do you have a sample project for that one?
p

pavi2410

07/25/2019, 2:40 PM
No, I don't. This is the code from one of my private projects.
10 Views