SlideShare a Scribd company logo
1 of 65
Download to read offline
Koin Quickstart
Matt Clarke - Auckland Android Community - Dec
2018
What is Koin?
What is Koin?
• From insert-koin.io:
What is Koin?
• From insert-koin.io:
• A pragmatic lightweight dependency injection
framework for Kotlin developers.
What is Koin?
• From insert-koin.io:
• A pragmatic lightweight dependency injection
framework for Kotlin developers.
• Written in pure Kotlin, using functional resolution only:
no proxy, no code generation, no reflection.
What is Koin?
• From insert-koin.io:
• A pragmatic lightweight dependency injection
framework for Kotlin developers.
• Written in pure Kotlin, using functional resolution only:
no proxy, no code generation, no reflection.
• Koin is a DSL, a light container and a pragmatic API
What is Koin?
What is Koin?
public class Person {
private String name;
private int age = 0;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (name != null ? !name.equals(person.name) : person.name != null) return false;
if (age != 0 ? age != person.age : person.age != 0) return false;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age='" + age + ''' +
'}';
}
}
What is Koin?
data class Person(var name: String, var age: Int)
What is Koin?
data class Person(var name: String, var age: Int)
i.e. “Koin is to Dagger as Kotlin is to Java”
Live (mob) coding
Deep(er) dive: How does
any of this work?
Deep(er) dive: How does
any of this work?
val postModule = module {
single { PostPresenter(get()) }
}
Deep(er) dive: How does
any of this work?
val postModule = module {
single { PostPresenter(get()) }
}
How does any of this work?
How does any of this work?
/**
* Create a Module
* Gather definitions
* @param path : Path of the module
* @param createOnStart : module definitions will be tagged as `createOnStart`
* @param override : allow all definitions from module to override definitions
*/
fun module(
path: String = Path.ROOT,
createOnStart: Boolean = false,
override: Boolean = false,
definition: ModuleDefinition.() -> Unit
): Module =
{ koinContext -> ModuleDefinition(path, createOnStart, override,
koinContext).apply(definition) }
org/koin/dsl/module/Module.kt
How does any of this work?
/**
* Create a Module
* Gather definitions
* @param path : Path of the module
* @param createOnStart : module definitions will be tagged as `createOnStart`
* @param override : allow all definitions from module to override definitions
*/
fun module(
path: String = Path.ROOT,
createOnStart: Boolean = false,
override: Boolean = false,
definition: ModuleDefinition.() -> Unit
): Module =
{ koinContext -> ModuleDefinition(path, createOnStart, override,
koinContext).apply(definition) }
org/koin/dsl/module/Module.kt
How does any of this work?
/**
* Create a Module
* Gather definitions
* @param path : Path of the module
* @param createOnStart : module definitions will be tagged as `createOnStart`
* @param override : allow all definitions from module to override definitions
*/
fun module(
path: String = Path.ROOT,
createOnStart: Boolean = false,
override: Boolean = false,
definition: ModuleDefinition.() -> Unit
): Module =
{ koinContext -> ModuleDefinition(path, createOnStart, override,
koinContext).apply(definition) }
org/koin/dsl/module/Module.kt
How does any of this work?
How does any of this work?
kotlinlang.org/docs/reference/lambdas.html
How does any of this work?
How does any of this work?
val postModule = module {
single { PostPresenter(get()) }
}
How does any of this work?
val postModule: Module = module(definition = {
single { PostPresenter(get()) }
})
How does any of this work?
val postModule: Module = module(definition = {
single { PostPresenter(get()) }
})
How does any of this work?
How does any of this work?
/**
* Create a Module
* Gather definitions
* @param path : Path of the module
* @param createOnStart : module definitions will be tagged as `createOnStart`
* @param override : allow all definitions from module to override definitions
*/
fun module(
path: String = Path.ROOT,
createOnStart: Boolean = false,
override: Boolean = false,
definition: ModuleDefinition.() -> Unit
): Module =
{ koinContext -> ModuleDefinition(path, createOnStart, override,
koinContext).apply(definition) }
org/koin/dsl/module/Module.kt
How does any of this work?
/**
* Create a Module
* Gather definitions
* @param path : Path of the module
* @param createOnStart : module definitions will be tagged as `createOnStart`
* @param override : allow all definitions from module to override definitions
*/
fun module(
path: String = Path.ROOT,
createOnStart: Boolean = false,
override: Boolean = false,
definition: ModuleDefinition.() -> Unit
): Module =
{ koinContext -> ModuleDefinition(path, createOnStart, override,
koinContext).apply(definition) }
org/koin/dsl/module/Module.kt
How does any of this work?
val postModule: Module = module(definition = {
single { PostPresenter(get()) }
})
How does any of this work?
val postModule: Module = module(definition = {
this.single { PostPresenter(get()) }
})
How does any of this work?
val postModule: Module = module(definition = {
this.single { PostPresenter(get()) }
})
How does any of this work?
How does any of this work?
/**
* Provide a single instance definition
* (unique instance)
*
* @param name
* @param createOnStart - need to be created at start
* @param override - allow definition override
* @param definition
*/
inline fun <reified T : Any> single(
name: String = "",
createOnStart: Boolean = false,
override: Boolean = false,
noinline definition: Definition<T>
): BeanDefinition<T> {
return provide(name, createOnStart, override, Kind.Single, definition)
}
org/koin/dsl/context/ModuleDefinition.kt
Type-Safe Builders
Type-Safe Builders
https://kotlinlang.org/docs/reference/type-safe-builders.html
Type-Safe Builders
Type-Safe Builders
What about this?
What about this?
/**
* Activity displaying the list of posts
*/
class PostActivity : AppCompatActivity(), PostView {
private val presenter: PostPresenter by inject()
// ...
What about this?
/**
* Activity displaying the list of posts
*/
class PostActivity : AppCompatActivity(), PostView {
private val presenter: PostPresenter by inject()
// ...
No magic
No magic
• Koin takes full advantage of Kotlin’s adv. language
features
No magic
• Koin takes full advantage of Kotlin’s adv. language
features
• E.g. reified generics, lambdas with receivers, type
inference
No magic
• Koin takes full advantage of Kotlin’s adv. language
features
• E.g. reified generics, lambdas with receivers, type
inference
• Less opaque/magical than annotation processing pipeline
for application developers
Testability?
Testability?
// Just tag your class with KoinTest to unlock your testing power
class SimpleTest : KoinTest {
// lazy inject BusinessService into property
val service : BusinessService by inject()
@Test
fun myTest() {
// You can start your Koin configuration
startKoin(myModules)
// or directly get any instance
val service : BusinessService = get()
// Don't forget to close it at the end
closeKoin()
}
}
What about Scopes?
What about Scopes?
val appModule = module {
// single instance of HelloRepository
single<HelloRepository> { HelloRepositoryImpl() }
// Scoped MyScopePresenter instance
scope("session") { MyScopePresenter(get())}
}
What about Scopes?
What about Scopes?
class MyScopeActivity : AppCompatActivity() {
// inject MyScopePresenter from "session" scope
val scopePresenter: MyScopePresenter by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_simple)
// bind "session" scope to component lifecycle
bindScope(getOrCreateScope("session"))
//...
}
}
Koin vs. Dagger overview
Koin vs. Dagger overview
• Koin pros:
Koin vs. Dagger overview
• Koin pros:
• Less boilerplate. Less arcane, easy to learn
Koin vs. Dagger overview
• Koin pros:
• Less boilerplate. Less arcane, easy to learn
• No annotation processing/code gen. Just language features
Koin vs. Dagger overview
• Koin pros:
• Less boilerplate. Less arcane, easy to learn
• No annotation processing/code gen. Just language features
• Straightforward Android integration
Koin vs. Dagger overview
• Koin pros:
• Less boilerplate. Less arcane, easy to learn
• No annotation processing/code gen. Just language features
• Straightforward Android integration
• Nice, simple documentation
Koin vs. Dagger overview
• Koin pros:
• Less boilerplate. Less arcane, easy to learn
• No annotation processing/code gen. Just language features
• Straightforward Android integration
• Nice, simple documentation
• Build speed (?)
Koin vs. Dagger overview
• Koin pros:
• Less boilerplate. Less arcane, easy to learn
• No annotation processing/code gen. Just language features
• Straightforward Android integration
• Nice, simple documentation
• Build speed (?)
• Koin cons:
Koin vs. Dagger overview
• Koin pros:
• Less boilerplate. Less arcane, easy to learn
• No annotation processing/code gen. Just language features
• Straightforward Android integration
• Nice, simple documentation
• Build speed (?)
• Koin cons:
• Runtime dependency resolution (though minimal overhead)
Koin vs. Dagger overview
• Koin pros:
• Less boilerplate. Less arcane, easy to learn
• No annotation processing/code gen. Just language features
• Straightforward Android integration
• Nice, simple documentation
• Build speed (?)
• Koin cons:
• Runtime dependency resolution (though minimal overhead)
• Newer, relatively unproven (compared to Dagger)
Is it Production-Ready?
Is it Production-Ready?
• Research didn’t turn up much, but…
Is it Production-Ready?
• Research didn’t turn up much, but…
• Reached v1.0 in Sep (FWIW)
Is it Production-Ready?
• Research didn’t turn up much, but…
• Reached v1.0 in Sep (FWIW)
• The author uses it in several production apps…
Is it Production-Ready?
• Research didn’t turn up much, but…
• Reached v1.0 in Sep (FWIW)
• The author uses it in several production apps…
• Workable team uses it prod, details here: https://
medium.com/@charbgr/bye-bye-dagger-1494118dcd41
Thanks!

Questions/thoughts/
comments?
Twitter: @kiwiandroiddev

Email: kiwiandroiddev@gmail.com

More Related Content

What's hot

Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Arnaud Giuliani
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?intelliyole
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptNascenia IT
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersBartosz Kosarzycki
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017Hardik Trivedi
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners Paddy Lock
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android DevelopmentSpeck&Tech
 
Clojure, Plain and Simple
Clojure, Plain and SimpleClojure, Plain and Simple
Clojure, Plain and SimpleBen Mabey
 
Android 101 - Kotlin ( Future of Android Development)
Android 101 - Kotlin ( Future of Android Development)Android 101 - Kotlin ( Future of Android Development)
Android 101 - Kotlin ( Future of Android Development)Hassan Abid
 
Fun with Functional Programming in Clojure
Fun with Functional Programming in ClojureFun with Functional Programming in Clojure
Fun with Functional Programming in ClojureCodemotion
 
JavaFX Dependency Injection with FxContainer
JavaFX Dependency Injection with FxContainerJavaFX Dependency Injection with FxContainer
JavaFX Dependency Injection with FxContainerSrikanth Shenoy
 
Annotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark ArtsAnnotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark ArtsJames Kirkbride
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module PatternsNicholas Jansma
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Addy Osmani
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptStoyan Stefanov
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvmArnaud Giuliani
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahNick Plante
 

What's hot (20)

Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android Development
 
Clojure, Plain and Simple
Clojure, Plain and SimpleClojure, Plain and Simple
Clojure, Plain and Simple
 
Android 101 - Kotlin ( Future of Android Development)
Android 101 - Kotlin ( Future of Android Development)Android 101 - Kotlin ( Future of Android Development)
Android 101 - Kotlin ( Future of Android Development)
 
Fun with Functional Programming in Clojure
Fun with Functional Programming in ClojureFun with Functional Programming in Clojure
Fun with Functional Programming in Clojure
 
JavaFX Dependency Injection with FxContainer
JavaFX Dependency Injection with FxContainerJavaFX Dependency Injection with FxContainer
JavaFX Dependency Injection with FxContainer
 
Annotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark ArtsAnnotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark Arts
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module Patterns
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScript
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
 
Kotlin intro
Kotlin introKotlin intro
Kotlin intro
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 

Similar to Koin Quickstart

Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaDILo Surabaya
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlinAdit Lal
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android developmentAdit Lal
 
Kotlin for Android devs
Kotlin for Android devsKotlin for Android devs
Kotlin for Android devsAdit Lal
 
From Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceFrom Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceStefanTomm
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Atif AbbAsi
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyMobileAcademy
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with AndroidKurt Renzo Acosta
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Mohamed Nabil, MSc.
 
A TypeScript Fans KotlinJS Adventures
A TypeScript Fans KotlinJS AdventuresA TypeScript Fans KotlinJS Adventures
A TypeScript Fans KotlinJS AdventuresGarth Gilmour
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android UpdateGarth Gilmour
 
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna loveWriting Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna loveAndré Oriani
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Tudor Dragan
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseChristian Melchior
 
Rapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and KtorRapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and KtorTrayan Iliev
 
Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Omar Miatello
 
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem novaKotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem novaVíctor Leonel Orozco López
 

Similar to Koin Quickstart (20)

Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo Surabaya
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlin
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android development
 
Kotlin for Android devs
Kotlin for Android devsKotlin for Android devs
Kotlin for Android devs
 
From Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceFrom Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practice
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRready
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
 
Fall in love with Kotlin
Fall in love with KotlinFall in love with Kotlin
Fall in love with Kotlin
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3
 
A TypeScript Fans KotlinJS Adventures
A TypeScript Fans KotlinJS AdventuresA TypeScript Fans KotlinJS Adventures
A TypeScript Fans KotlinJS Adventures
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna loveWriting Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in Practise
 
Rapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and KtorRapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and Ktor
 
Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01
 
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem novaKotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
 
從零開始學 Android
從零開始學 Android從零開始學 Android
從零開始學 Android
 

Recently uploaded

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Recently uploaded (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

Koin Quickstart

  • 1. Koin Quickstart Matt Clarke - Auckland Android Community - Dec 2018
  • 3. What is Koin? • From insert-koin.io:
  • 4. What is Koin? • From insert-koin.io: • A pragmatic lightweight dependency injection framework for Kotlin developers.
  • 5. What is Koin? • From insert-koin.io: • A pragmatic lightweight dependency injection framework for Kotlin developers. • Written in pure Kotlin, using functional resolution only: no proxy, no code generation, no reflection.
  • 6. What is Koin? • From insert-koin.io: • A pragmatic lightweight dependency injection framework for Kotlin developers. • Written in pure Kotlin, using functional resolution only: no proxy, no code generation, no reflection. • Koin is a DSL, a light container and a pragmatic API
  • 8. What is Koin? public class Person { private String name; private int age = 0; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; if (name != null ? !name.equals(person.name) : person.name != null) return false; if (age != 0 ? age != person.age : person.age != 0) return false; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; } @Override public String toString() { return "Person{" + "name='" + name + ''' + ", age='" + age + ''' + '}'; } }
  • 9. What is Koin? data class Person(var name: String, var age: Int)
  • 10. What is Koin? data class Person(var name: String, var age: Int) i.e. “Koin is to Dagger as Kotlin is to Java”
  • 12. Deep(er) dive: How does any of this work?
  • 13. Deep(er) dive: How does any of this work? val postModule = module { single { PostPresenter(get()) } }
  • 14. Deep(er) dive: How does any of this work? val postModule = module { single { PostPresenter(get()) } }
  • 15. How does any of this work?
  • 16. How does any of this work? /** * Create a Module * Gather definitions * @param path : Path of the module * @param createOnStart : module definitions will be tagged as `createOnStart` * @param override : allow all definitions from module to override definitions */ fun module( path: String = Path.ROOT, createOnStart: Boolean = false, override: Boolean = false, definition: ModuleDefinition.() -> Unit ): Module = { koinContext -> ModuleDefinition(path, createOnStart, override, koinContext).apply(definition) } org/koin/dsl/module/Module.kt
  • 17. How does any of this work? /** * Create a Module * Gather definitions * @param path : Path of the module * @param createOnStart : module definitions will be tagged as `createOnStart` * @param override : allow all definitions from module to override definitions */ fun module( path: String = Path.ROOT, createOnStart: Boolean = false, override: Boolean = false, definition: ModuleDefinition.() -> Unit ): Module = { koinContext -> ModuleDefinition(path, createOnStart, override, koinContext).apply(definition) } org/koin/dsl/module/Module.kt
  • 18. How does any of this work? /** * Create a Module * Gather definitions * @param path : Path of the module * @param createOnStart : module definitions will be tagged as `createOnStart` * @param override : allow all definitions from module to override definitions */ fun module( path: String = Path.ROOT, createOnStart: Boolean = false, override: Boolean = false, definition: ModuleDefinition.() -> Unit ): Module = { koinContext -> ModuleDefinition(path, createOnStart, override, koinContext).apply(definition) } org/koin/dsl/module/Module.kt
  • 19. How does any of this work?
  • 20. How does any of this work? kotlinlang.org/docs/reference/lambdas.html
  • 21. How does any of this work?
  • 22. How does any of this work? val postModule = module { single { PostPresenter(get()) } }
  • 23. How does any of this work? val postModule: Module = module(definition = { single { PostPresenter(get()) } })
  • 24. How does any of this work? val postModule: Module = module(definition = { single { PostPresenter(get()) } })
  • 25. How does any of this work?
  • 26. How does any of this work? /** * Create a Module * Gather definitions * @param path : Path of the module * @param createOnStart : module definitions will be tagged as `createOnStart` * @param override : allow all definitions from module to override definitions */ fun module( path: String = Path.ROOT, createOnStart: Boolean = false, override: Boolean = false, definition: ModuleDefinition.() -> Unit ): Module = { koinContext -> ModuleDefinition(path, createOnStart, override, koinContext).apply(definition) } org/koin/dsl/module/Module.kt
  • 27. How does any of this work? /** * Create a Module * Gather definitions * @param path : Path of the module * @param createOnStart : module definitions will be tagged as `createOnStart` * @param override : allow all definitions from module to override definitions */ fun module( path: String = Path.ROOT, createOnStart: Boolean = false, override: Boolean = false, definition: ModuleDefinition.() -> Unit ): Module = { koinContext -> ModuleDefinition(path, createOnStart, override, koinContext).apply(definition) } org/koin/dsl/module/Module.kt
  • 28. How does any of this work? val postModule: Module = module(definition = { single { PostPresenter(get()) } })
  • 29. How does any of this work? val postModule: Module = module(definition = { this.single { PostPresenter(get()) } })
  • 30. How does any of this work? val postModule: Module = module(definition = { this.single { PostPresenter(get()) } })
  • 31. How does any of this work?
  • 32. How does any of this work? /** * Provide a single instance definition * (unique instance) * * @param name * @param createOnStart - need to be created at start * @param override - allow definition override * @param definition */ inline fun <reified T : Any> single( name: String = "", createOnStart: Boolean = false, override: Boolean = false, noinline definition: Definition<T> ): BeanDefinition<T> { return provide(name, createOnStart, override, Kind.Single, definition) } org/koin/dsl/context/ModuleDefinition.kt
  • 38. What about this? /** * Activity displaying the list of posts */ class PostActivity : AppCompatActivity(), PostView { private val presenter: PostPresenter by inject() // ...
  • 39. What about this? /** * Activity displaying the list of posts */ class PostActivity : AppCompatActivity(), PostView { private val presenter: PostPresenter by inject() // ...
  • 41. No magic • Koin takes full advantage of Kotlin’s adv. language features
  • 42. No magic • Koin takes full advantage of Kotlin’s adv. language features • E.g. reified generics, lambdas with receivers, type inference
  • 43. No magic • Koin takes full advantage of Kotlin’s adv. language features • E.g. reified generics, lambdas with receivers, type inference • Less opaque/magical than annotation processing pipeline for application developers
  • 45. Testability? // Just tag your class with KoinTest to unlock your testing power class SimpleTest : KoinTest { // lazy inject BusinessService into property val service : BusinessService by inject() @Test fun myTest() { // You can start your Koin configuration startKoin(myModules) // or directly get any instance val service : BusinessService = get() // Don't forget to close it at the end closeKoin() } }
  • 47. What about Scopes? val appModule = module { // single instance of HelloRepository single<HelloRepository> { HelloRepositoryImpl() } // Scoped MyScopePresenter instance scope("session") { MyScopePresenter(get())} }
  • 49. What about Scopes? class MyScopeActivity : AppCompatActivity() { // inject MyScopePresenter from "session" scope val scopePresenter: MyScopePresenter by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_simple) // bind "session" scope to component lifecycle bindScope(getOrCreateScope("session")) //... } }
  • 50. Koin vs. Dagger overview
  • 51. Koin vs. Dagger overview • Koin pros:
  • 52. Koin vs. Dagger overview • Koin pros: • Less boilerplate. Less arcane, easy to learn
  • 53. Koin vs. Dagger overview • Koin pros: • Less boilerplate. Less arcane, easy to learn • No annotation processing/code gen. Just language features
  • 54. Koin vs. Dagger overview • Koin pros: • Less boilerplate. Less arcane, easy to learn • No annotation processing/code gen. Just language features • Straightforward Android integration
  • 55. Koin vs. Dagger overview • Koin pros: • Less boilerplate. Less arcane, easy to learn • No annotation processing/code gen. Just language features • Straightforward Android integration • Nice, simple documentation
  • 56. Koin vs. Dagger overview • Koin pros: • Less boilerplate. Less arcane, easy to learn • No annotation processing/code gen. Just language features • Straightforward Android integration • Nice, simple documentation • Build speed (?)
  • 57. Koin vs. Dagger overview • Koin pros: • Less boilerplate. Less arcane, easy to learn • No annotation processing/code gen. Just language features • Straightforward Android integration • Nice, simple documentation • Build speed (?) • Koin cons:
  • 58. Koin vs. Dagger overview • Koin pros: • Less boilerplate. Less arcane, easy to learn • No annotation processing/code gen. Just language features • Straightforward Android integration • Nice, simple documentation • Build speed (?) • Koin cons: • Runtime dependency resolution (though minimal overhead)
  • 59. Koin vs. Dagger overview • Koin pros: • Less boilerplate. Less arcane, easy to learn • No annotation processing/code gen. Just language features • Straightforward Android integration • Nice, simple documentation • Build speed (?) • Koin cons: • Runtime dependency resolution (though minimal overhead) • Newer, relatively unproven (compared to Dagger)
  • 61. Is it Production-Ready? • Research didn’t turn up much, but…
  • 62. Is it Production-Ready? • Research didn’t turn up much, but… • Reached v1.0 in Sep (FWIW)
  • 63. Is it Production-Ready? • Research didn’t turn up much, but… • Reached v1.0 in Sep (FWIW) • The author uses it in several production apps…
  • 64. Is it Production-Ready? • Research didn’t turn up much, but… • Reached v1.0 in Sep (FWIW) • The author uses it in several production apps… • Workable team uses it prod, details here: https:// medium.com/@charbgr/bye-bye-dagger-1494118dcd41