Koin is the next step to becoming a project only with Kotlin code. What inside Koin? Only DSL & Kotlin magic. It is possible to effectively replace Dagger 2? Let’s find out does Dependency Injection + Kotlin + Easy of Use == Koin?
// create a scope val session = getKoin().createScope("scope_id") // or get scope if already created before val session = getKoin().getScope("scope_id") // will return the same instance of Presenter until Scope is closed val presenter = get<Presenter>() // close the scope session.close() // instance of presenter has been dropped
// create a scope val session = getKoin().createScope("scope_id") // or get scope if already created before val session = getKoin().getScope("scope_id") // will return the same instance of Presenter until Scope is closed val presenter = get<Presenter>() // close the scope session.close() // instance of presenter has been dropped
// create a scope val session = getKoin().createScope("scope_id") // or get scope if already created before val session = getKoin().getScope("scope_id") // will return the same instance of Presenter until Scope is closed val presenter = get<Presenter>() // close the scope session.close() // instance of presenter has been dropped
// create a scope val session = getKoin().createScope("scope_id") // or get scope if already created before val session = getKoin().getScope("scope_id") // will return the same instance of Presenter until Scope is closed val presenter = get<Presenter>() // close the scope session.close() // instance of presenter has been dropped
// create a scope val session = getKoin().createScope("scope_id") // or get scope if already created before val session = getKoin().getScope("scope_id") // will return the same instance of Presenter until Scope is closed val presenter = get<Presenter>() // close the scope session.close() // instance of presenter has been dropped
inject Presenter instance, tied to current “session" scope val presenter : Presenter by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // bind current lifecycle to Activity's scope bindScope(getScope("session")) }
inject Presenter instance, tied to current “session" scope val presenter : Presenter by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // bind current lifecycle to Activity's scope bindScope(getScope("session")) }
ComponentB(get()) } } class ComponentA(component: ComponentB) class ComponentB(component: ComponentA) org.koin.error.DependencyResolutionException: No definition found for ComponentC - Check your definitions and contexts visibility get<ComponentC>()
: BeanInstanceCreationException: Can't create bean Bean[class=ComponentB] due to error : BeanInstanceCreationException: Can't create bean Bean[class=ComponentA] due to error : DependencyResolutionException: Cyclic dependency detected while resolving class ComponentA module { single { ComponentA(get()) } single { ComponentB(get()) } } class ComponentA(component: ComponentB) class ComponentB(component: ComponentA) get<ComponentA>()
by inject() val repository : Repository by inject() @Before fun before(){ startKoin(listOf(myModule)) } @Test fun testSayHello() { val hello = repository.giveHello() assertEquals(hello, presenter.sayHello()) } @After fun after(){ closeKoin() } }
by inject() val repository : Repository by inject() @Before fun before(){ startKoin(listOf(myModule)) } @Test fun testSayHello() { val hello = repository.giveHello() assertEquals(hello, presenter.sayHello()) } @After fun after(){ closeKoin() } }
by inject() val repository : Repository by inject() @Before fun before(){ startKoin(listOf(myModule)) } @Test fun testSayHello() { val hello = repository.giveHello() assertEquals(hello, presenter.sayHello()) } @After fun after(){ closeKoin() } }
by inject() val repository : Repository by inject() @Before fun before(){ startKoin(listOf(myModule)) } @Test fun testSayHello() { val hello = repository.giveHello() assertEquals(hello, presenter.sayHello()) } @After fun after(){ closeKoin() } }
by inject() val repository : Repository by inject() @Before fun before(){ startKoin(listOf(myModule)) } @Test fun testSayHello() { val hello = repository.giveHello() assertEquals(hello, presenter.sayHello()) } @After fun after(){ closeKoin() } }
start with DI • No need Dagger 2 advanced features • Single place of dependencies declaration • DSL is amazing for dependencies declaration • Support Kotlin Multiplatform projects
start with DI • No need Dagger 2 advanced features • Single place of dependencies declaration • DSL is amazing for dependencies declaration • Support Kotlin Multiplatform projects • Faster build time
start with DI • No need Dagger 2 advanced features • Single place of dependencies declaration • DSL is amazing for dependencies declaration • Support Kotlin Multiplatform projects • Faster build time • Pure Kotlin code