in via constructor or just accessing it inline with the help of a top-level property like:
Copy code
val appContext by lazy {
GlobalContext.get().koin.get<Context>()
}
Since an application
Context
is not mocked (I think!), is there any advantage to passing it into a constructor?
i
itnoles
02/09/2020, 5:49 AM
I uses Mockito.mock(Application::class.java) for it
m
Mark
02/09/2020, 6:31 AM
Could that not be injected into the
GlobalContext
instead of to each constructor?
i
il_agent
02/10/2020, 12:20 PM
That’s way to the dark side 🙂 Later you can think “why wouldn’t use MyRepo through static member” and so on.
m
Mark
02/10/2020, 12:26 PM
I think there’s a big difference there. There’s a good chance I want to provide a mocked MyRepo. Also MyRepo is unlikely to be as pervasive as application Context.
Mark
02/10/2020, 12:28 PM
Often I find I have functions that only take an application Context and so without that, I would be able to write
val someValue get() = // something cool
Furthermore, when accepting a Context arg, we cannot be sure if the Context is an application Context or something we should not be holding a reference to (Activity etc), so that complicates things a little.