The document compares Dagger and Koin dependency injection frameworks for Android. It discusses that Dagger uses compile-time code generation while Koin is runtime without code generation. It provides examples of implementing dependency injection with field injection, modules, and scoping for both frameworks. It also covers testing dependency injection with Koin. In summary, the document notes that Dagger scales better but has slower build times while Koin is simpler to use but errors occur at runtime.
5. „
Neversettle
intive.com
As a result I think we need a more specific name
for this pattern. Inversion of Control is too
generic a term, and thus people find it confusing.
As a result with a lot of discussion with various
IoC advocates we settled on the
name Dependency Injection.
-Martin Fowler
DI and Service Locator
7. Neversettle
intive.com
_DI
_Logic in two sets, integration with Pure Dependency injection or frameworks
!7Source: https://www.techyourchance.com/dependency-injection-android/
DI and Service Locator
8. Neversettle
intive.com
_DI
_Logic in two sets, integration with Pure Dependency injection or frameworks
_Common techniques:
_constructor injection
_method/setter injection
_field injection
!8
DI and Service Locator
9. Neversettle
intive.com
_Service Locator
_Object which holds services that an application might need
!9Source: https://martinfowler.com/articles/injection.html#ServiceLocatorVsDependencyInjection
DI and Service Locator
10. Neversettle
intive.com
_Service Locator
class ServiceLocator {
private Map services = new HashMap();
public Object getService(String key){
return services.get(key);
}
public void loadService(String key, Object service) {
services.put(key, service);
}
}
!10
DI and Service Locator
11. „
Neversettle
intive.com
With service locator the application class asks for it
explicitly by a message to the locator. With injection
there is no explicit request, the service appears in the
application class - hence the inversion of control.
- Martin Fowler
DI and Service Locator
14. Neversettle
intive.com
_Koin
_Runtime, lightweight service locator
_No code generation
_Based on Kotlin features
_0.2.0 version in 2017, current version is 0.9.3
_Written fully in Kotlin and works in Kotlin classes only
Dagger vs. Koin
!14
15. Neversettle
intive.com
_Field injection (Dagger)
class SearchActivity : AppCompatActivity() {
@Inject
lateinit var viewModel: SearchViewModel
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
(...)
}
}
Dagger vs. Koin
!15
34. Neversettle
intive.com
_Activity test
class SearchActivityTest : AutoCloseKoinTest() {
val tested = SearchActivity().testable()
val viewModel = mock<SearchViewModel>()
@BeforeEach
fun beforeEach() {
val testModule = applicationContext {
bean { viewModel }
}
loadKoinModules(testModule)
}
@Test
fun `should test something very important here`() {
tested.onCreate(mock())
tested.dataAdapter.itemCount `should equal` 0
}
}
Testing with Koin
!34
37. Neversettle
intive.com
_Pros
_Scales better (when using constructor annotations)
_Implementation of Dependency Injection
_Errors reported on build time
_Well known
_Cons
_Slow build time (code generation)
_Relatively hard to implement and understand
_Quite a lot initial boilerplate
Summary
!37
_Dagger
38. Neversettle
intive.com
_Pros
_Very simple to use and easy to debug
_Almost no initial boilerplate
_No code generation (reified trick)
_Cons
_Errors on runtime (dryRun could help)
_Pollutes app classes with by inject() (Service Locator)
_Kind of weird scoping but flexible
_Works only in Activities/Fragments written in Kotlin (by inject())
Summary
!38
_Koin
Bonus
Supports ViewModel
out of the box