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

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
Bartosz Kosarzycki
 

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

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

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
THE BEST IPTV in GERMANY for 2024: IPTVreel
THE BEST IPTV in  GERMANY for 2024: IPTVreelTHE BEST IPTV in  GERMANY for 2024: IPTVreel
THE BEST IPTV in GERMANY for 2024: IPTVreel
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 

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