SlideShare a Scribd company logo
Taipei
Jintin
Dagger vs koin
Designed for the mobile
generation
● Snap, list, sell
● Private chat
● A.I. assisted selling
● Personalised content
What is Dependency Injection?
public class Twitter {
public void getFeeds() {
TwitterApi api = new TwitterApi();
api.getFeeds();
}
}
public class TwitterApi {
public void getFeeds () {
HttpClient httpClient = new HttpClient();
httpClient.connect("http://xxx.com")
}
}
public class Twitter {
public void getFeeds() {
TwitterApi api = new TwitterApi();
api.getFeeds();
}
}
public class TwitterApi {
public void getFeeds () {
HttpClient httpClient = new HttpClient();
httpClient.connect("http://xxx.com")
}
}
What’s the problem now?
● New is evil
● High coupling
● Hard to replace
● Hard to test
Dependency Injection
Dependency injection is basically providing
the objects that an object needs (its
dependencies) instead of having it construct
them itself.
How to Inject Object?
● Constructor Injection
● Method Injection
● Field Injection
public class Twitter {
public void getFeeds() {
TwitterApi api = new TwitterApi();
api.getFeeds();
}
}
public class TwitterApi {
public void getFeeds () {
HttpClient httpClient = new HttpClient();
httpClient.connect("http://xxx.com")
}
}
public class Twitter {
public void getFeeds(TwitterApi api) {
api.getFeeds();
}
}
public class TwitterApi {
HttpClient httpClient;
public TwitterApi(HttpClient httpClient) {
this.httpClient = httpClient;
}
}
What’s the problem now?
● Hard to use
● Manage dependency graph is a mess
● Dagger is the rescue
What is Dagger?
Dagger 2
● Dependency Injection tool
● Simple Annotation structure
● Readable generated code
● No reflection (fast)
● No runtime error
● Proguard friendly
● Pure Java
● https://github.com/google/dagger
● https://android.jlelse.eu/dagger-2-c00f31decda5
Annotation Processing
● Annotate in source code
● Start compile
● Read Annotation and relevant data
● Generate code under build folder
● Compile finish
● https://medium.com/@jintin/annotation-pr
ocessing-in-java-3621cb05343a
Annotations in Dagger2
● @Provides
● @Module
● @Component
● @Inject (in Java)
● @Qualifier (@Named)
● @Scope (@Singleton)
Bee Honey
Lemon
// Object class
class Lemon
class Bee
class Honey(var bee: Bee)
class HoneyLemonade
@Inject
constructor(var honey: Honey, var lemon: Lemon)
@Provides
Lemon provideLemon() {
return new Lemon();
}
@Provides
Honey provideHoney(Bee bee) {
return new Honey(bee);
}
@Provides
Bee provideBee() {
return new Bee();
}
@Module
public class DrinkModule {
@Provides
Lemon provideLemon() {...}
@Provides
Honey provideHoney(Bee bee) {...}
@Provides
Bee provideBee() {...}
}
@Component(modules = {DrinkModule.class})
public interface DrinkComponent {
HoneyLemonade drink();
void inject(MainActivity activity);
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrinkComponent co = DaggerDrinkComponent.create();
HoneyLemonade drink = co.drink();
}
}
public class MainActivity extends AppCompatActivity {
@Inject
HoneyLemonade drink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrinkComponent co = DaggerDrinkComponent.create();
co.inject(this);
}
}
Recap
● Provides: provide dependency
● Modules: group Provides
● Component: The relation graph
● Inject: Identifier for inject
Other feature
● Qualifier: Named...
● Scope: Singleton...
● Subcomponent / includes
● Lazy / Provider...
Demo
What is koin?
koin
● Service locator by Kotlin DSL
● No code generation
● No reflection
● No compile time overhead
● Pure Kotlin
● https://github.com/InsertKoinIO/koin
Service locator
The service locator pattern is a design pattern
used in software development to encapsulate the
processes involved in obtaining a service with a
strong abstraction layer.
Object Locator
Lemon
Honey
Kotlin DSL
● Function literals with receiver
● Describe data structure
● Like Anko, ktor
// HTML DSL
val result =
html {
head {
title {+"XML encoding with Kotlin"}
}
body {
h1 {+"XML encoding with Kotlin"}
p {+"this format can be used"}
a(href = "http://kotlinlang.org") {+"Kotlin"}
}
}
fun html(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}
fun body(init: Body.() -> Unit) : Body {
val body = Body()
body.init()
return body
}
koin DSL
● Module - create a Koin Module
● Factory - provide a factory bean definition
● Single - provide a singleton bean definition
(also aliased as bean)
● Get - resolve a component dependency
// gradle install
dependencies {
// Koin for Android
compile 'org.koin:koin-android:1.0.2'
// or Koin for Lifecycle scoping
compile 'org.koin:koin-android-scope:1.0.2'
// or Koin for Android Architecture ViewModel
compile 'org.koin:koin-android-viewmodel:1.0.2'
}
// Object class
class Bee
class Honey(var bee: Bee)
class Lemon
class HoneyLemonade(var honey: Honey,
var lemon: Lemon)
class MyApplication : Application() {
override fun onCreate(){
super.onCreate()
// start Koin!
startKoin(this, listOf(myModule))
}
}
val myModule = module {
single { Bee() }
single { Honey(get()) }
single { Lemon() }
single { HoneyLemonade(get(), get()) }
}
// How get() work? reified
inline fun <reified T : Any> get(
name: String = "",
scopeId: String? = null,
noinline parameters: ParameterDefinition = ...
): T {
val scope: Scope? = scopeId?.let {
koinContext.getScope(scopeId)
}
return koinContext.get(name, scope, parameters)
}
reified function
● Work with inline
● Can access actual type T
● After compile T will replace with actual type
class MyActivity() : AppCompatActivity() {
val drink : HoneyLemonade by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val drink : HoneyLemonade = get()
}
}
// How inject(), get() work?
inline fun <reified T : Any> ComponentCallbacks.inject(
name: String = "",
scope: Scope? = null,
noinline parameters: ParameterDefinition = ...
) = lazy { get<T>(name, scope, parameters) }
inline fun <reified T : Any> ComponentCallbacks.get(
name: String = "",
scope: Scope? = null,
noinline parameters: ParameterDefinition = ...
): T = getKoin().get(name, scope, parameters)
// What is ComponentCallbacks btw?
Demo
Dagger vs koin
Dagger
● Pros:
○ + Pure Java
○ + Stable, flexible, powerful
○ + No Runtime error
○ + Fast in Runtime
● Cons:
○ - Compile overhead
○ - Hard to learn
koin
● Pros:
○ + No Annotation processing
○ + Easy to learn/setup
● Cons:
○ - Hard to replace/remove
○ - Test in its env
○ - Error in Runtime
“you and only you are responsible
for your life choices and decisions”
- Robert T. Kiyosaki
Jintin
Taipei
Thank you!

More Related Content

What's hot

Angular2 + rxjs
Angular2 + rxjsAngular2 + rxjs
Angular2 + rxjs
Christoffer Noring
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2
giwoolee
 
Sword fighting with Dagger GDG-NYC Jan 2016
 Sword fighting with Dagger GDG-NYC Jan 2016 Sword fighting with Dagger GDG-NYC Jan 2016
Sword fighting with Dagger GDG-NYC Jan 2016
Mike Nakhimovich
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Ray Ploski
 
Practical Protocol-Oriented-Programming
Practical Protocol-Oriented-ProgrammingPractical Protocol-Oriented-Programming
Practical Protocol-Oriented-Programming
Natasha Murashev
 
Make XCUITest Great Again
Make XCUITest Great AgainMake XCUITest Great Again
Make XCUITest Great Again
Kenneth Poon
 
Kotlin Delegates in practice - Kotlin community conf
Kotlin Delegates in practice - Kotlin community confKotlin Delegates in practice - Kotlin community conf
Kotlin Delegates in practice - Kotlin community conf
Fabio Collini
 
Building maintainable app #droidconzg
Building maintainable app #droidconzgBuilding maintainable app #droidconzg
Building maintainable app #droidconzg
Kristijan Jurković
 
Kotlin delegates in practice - Kotlin Everywhere Stockholm
Kotlin delegates in practice - Kotlin Everywhere StockholmKotlin delegates in practice - Kotlin Everywhere Stockholm
Kotlin delegates in practice - Kotlin Everywhere Stockholm
Fabio Collini
 
Dependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutesDependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutes
Antonio Goncalves
 
Open sourcing the store
Open sourcing the storeOpen sourcing the store
Open sourcing the store
Mike Nakhimovich
 
Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SF
Pierre-Yves Ricau
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
Christoffer Noring
 
What’s new in Android JetPack
What’s new in Android JetPackWhat’s new in Android JetPack
What’s new in Android JetPack
Hassan Abid
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
Visual Engineering
 
Building maintainable app
Building maintainable appBuilding maintainable app
Building maintainable app
Kristijan Jurković
 
Testing Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKTesting Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UK
Fabio Collini
 
Aggregation and Awareness or How to Reduce the Amount of your FrontEnd Code ...
Aggregation and Awareness or How to Reduce the Amount of  your FrontEnd Code ...Aggregation and Awareness or How to Reduce the Amount of  your FrontEnd Code ...
Aggregation and Awareness or How to Reduce the Amount of your FrontEnd Code ...
ISS Art, LLC
 
Improving app performance with Kotlin Coroutines
Improving app performance with Kotlin CoroutinesImproving app performance with Kotlin Coroutines
Improving app performance with Kotlin Coroutines
Hassan Abid
 

What's hot (20)

Angular2 + rxjs
Angular2 + rxjsAngular2 + rxjs
Angular2 + rxjs
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2
 
Sword fighting with Dagger GDG-NYC Jan 2016
 Sword fighting with Dagger GDG-NYC Jan 2016 Sword fighting with Dagger GDG-NYC Jan 2016
Sword fighting with Dagger GDG-NYC Jan 2016
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6
 
Practical Protocol-Oriented-Programming
Practical Protocol-Oriented-ProgrammingPractical Protocol-Oriented-Programming
Practical Protocol-Oriented-Programming
 
Make XCUITest Great Again
Make XCUITest Great AgainMake XCUITest Great Again
Make XCUITest Great Again
 
Kotlin Delegates in practice - Kotlin community conf
Kotlin Delegates in practice - Kotlin community confKotlin Delegates in practice - Kotlin community conf
Kotlin Delegates in practice - Kotlin community conf
 
Building maintainable app #droidconzg
Building maintainable app #droidconzgBuilding maintainable app #droidconzg
Building maintainable app #droidconzg
 
Kotlin delegates in practice - Kotlin Everywhere Stockholm
Kotlin delegates in practice - Kotlin Everywhere StockholmKotlin delegates in practice - Kotlin Everywhere Stockholm
Kotlin delegates in practice - Kotlin Everywhere Stockholm
 
Dependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutesDependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutes
 
Open sourcing the store
Open sourcing the storeOpen sourcing the store
Open sourcing the store
 
Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SF
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
What’s new in Android JetPack
What’s new in Android JetPackWhat’s new in Android JetPack
What’s new in Android JetPack
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
Building maintainable app
Building maintainable appBuilding maintainable app
Building maintainable app
 
Testing Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKTesting Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UK
 
Aggregation and Awareness or How to Reduce the Amount of your FrontEnd Code ...
Aggregation and Awareness or How to Reduce the Amount of  your FrontEnd Code ...Aggregation and Awareness or How to Reduce the Amount of  your FrontEnd Code ...
Aggregation and Awareness or How to Reduce the Amount of your FrontEnd Code ...
 
Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2
 
Improving app performance with Kotlin Coroutines
Improving app performance with Kotlin CoroutinesImproving app performance with Kotlin Coroutines
Improving app performance with Kotlin Coroutines
 

Similar to Dagger 2 vs koin

Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)
Python Ireland
 
Vaadin7
Vaadin7Vaadin7
Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++Using the Windows 8 Runtime from C++
Vaadin 7
Vaadin 7Vaadin 7
Vaadin 7
Joonas Lehtinen
 
Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejugrobbiev
 
Why Spring <3 Kotlin
Why Spring <3 KotlinWhy Spring <3 Kotlin
Why Spring <3 Kotlin
VMware Tanzu
 
Introduction to Spring Boot.pdf
Introduction to Spring Boot.pdfIntroduction to Spring Boot.pdf
Introduction to Spring Boot.pdf
ShaiAlmog1
 
Connecting to a Webservice.pdf
Connecting to a Webservice.pdfConnecting to a Webservice.pdf
Connecting to a Webservice.pdf
ShaiAlmog1
 
Maintaining a dependency graph with weaver
Maintaining a dependency graph with weaverMaintaining a dependency graph with weaver
Maintaining a dependency graph with weaver
Scribd
 
Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014
Oren Rubin
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
Andres Almiray
 
Day 5
Day 5Day 5
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
Ali Parmaksiz
 
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Naresha K
 
How to code to code less
How to code to code lessHow to code to code less
How to code to code less
Anton Novikau
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
Jose Manuel Pereira Garcia
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin Edelson
AEM HUB
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
Justin Edelson
 

Similar to Dagger 2 vs koin (20)

Android workshop
Android workshopAndroid workshop
Android workshop
 
Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)
 
Vaadin7
Vaadin7Vaadin7
Vaadin7
 
Introduction to-java
Introduction to-javaIntroduction to-java
Introduction to-java
 
Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++
 
Vaadin 7
Vaadin 7Vaadin 7
Vaadin 7
 
Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejug
 
Why Spring <3 Kotlin
Why Spring <3 KotlinWhy Spring <3 Kotlin
Why Spring <3 Kotlin
 
Introduction to Spring Boot.pdf
Introduction to Spring Boot.pdfIntroduction to Spring Boot.pdf
Introduction to Spring Boot.pdf
 
Connecting to a Webservice.pdf
Connecting to a Webservice.pdfConnecting to a Webservice.pdf
Connecting to a Webservice.pdf
 
Maintaining a dependency graph with weaver
Maintaining a dependency graph with weaverMaintaining a dependency graph with weaver
Maintaining a dependency graph with weaver
 
Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
 
Day 5
Day 5Day 5
Day 5
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
 
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
 
How to code to code less
How to code to code lessHow to code to code less
How to code to code less
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin Edelson
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 

More from Jintin Lin

KSP intro
KSP introKSP intro
KSP intro
Jintin Lin
 
Annotation Processing
Annotation ProcessingAnnotation Processing
Annotation Processing
Jintin Lin
 
我的開源之旅
我的開源之旅我的開源之旅
我的開源之旅
Jintin Lin
 
ButterKnife
ButterKnifeButterKnife
ButterKnife
Jintin Lin
 
數學系的資訊人生
數學系的資訊人生數學系的資訊人生
數學系的資訊人生
Jintin Lin
 
Instant app Intro
Instant app IntroInstant app Intro
Instant app Intro
Jintin Lin
 
KVO implementation
KVO implementationKVO implementation
KVO implementation
Jintin Lin
 
iOS NotificationCenter intro
iOS NotificationCenter introiOS NotificationCenter intro
iOS NotificationCenter intro
Jintin Lin
 
App design guide
App design guideApp design guide
App design guide
Jintin Lin
 
J霧霾
J霧霾J霧霾
J霧霾
Jintin Lin
 
transai
transaitransai
transai
Jintin Lin
 
Swimat - Swift formatter
Swimat - Swift formatterSwimat - Swift formatter
Swimat - Swift formatter
Jintin Lin
 
Swift Tutorial 2
Swift Tutorial  2Swift Tutorial  2
Swift Tutorial 2
Jintin Lin
 
Swift Tutorial 1
Swift Tutorial 1Swift Tutorial 1
Swift Tutorial 1
Jintin Lin
 
Andle
AndleAndle
Andle
Jintin Lin
 
Realism vs Flat design
Realism vs Flat designRealism vs Flat design
Realism vs Flat design
Jintin Lin
 
Android Service Intro
Android Service IntroAndroid Service Intro
Android Service IntroJintin Lin
 

More from Jintin Lin (17)

KSP intro
KSP introKSP intro
KSP intro
 
Annotation Processing
Annotation ProcessingAnnotation Processing
Annotation Processing
 
我的開源之旅
我的開源之旅我的開源之旅
我的開源之旅
 
ButterKnife
ButterKnifeButterKnife
ButterKnife
 
數學系的資訊人生
數學系的資訊人生數學系的資訊人生
數學系的資訊人生
 
Instant app Intro
Instant app IntroInstant app Intro
Instant app Intro
 
KVO implementation
KVO implementationKVO implementation
KVO implementation
 
iOS NotificationCenter intro
iOS NotificationCenter introiOS NotificationCenter intro
iOS NotificationCenter intro
 
App design guide
App design guideApp design guide
App design guide
 
J霧霾
J霧霾J霧霾
J霧霾
 
transai
transaitransai
transai
 
Swimat - Swift formatter
Swimat - Swift formatterSwimat - Swift formatter
Swimat - Swift formatter
 
Swift Tutorial 2
Swift Tutorial  2Swift Tutorial  2
Swift Tutorial 2
 
Swift Tutorial 1
Swift Tutorial 1Swift Tutorial 1
Swift Tutorial 1
 
Andle
AndleAndle
Andle
 
Realism vs Flat design
Realism vs Flat designRealism vs Flat design
Realism vs Flat design
 
Android Service Intro
Android Service IntroAndroid Service Intro
Android Service Intro
 

Recently uploaded

Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 

Recently uploaded (20)

Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 

Dagger 2 vs koin

  • 2. Designed for the mobile generation ● Snap, list, sell ● Private chat ● A.I. assisted selling ● Personalised content
  • 3. What is Dependency Injection?
  • 4. public class Twitter { public void getFeeds() { TwitterApi api = new TwitterApi(); api.getFeeds(); } } public class TwitterApi { public void getFeeds () { HttpClient httpClient = new HttpClient(); httpClient.connect("http://xxx.com") } }
  • 5. public class Twitter { public void getFeeds() { TwitterApi api = new TwitterApi(); api.getFeeds(); } } public class TwitterApi { public void getFeeds () { HttpClient httpClient = new HttpClient(); httpClient.connect("http://xxx.com") } }
  • 6. What’s the problem now? ● New is evil ● High coupling ● Hard to replace ● Hard to test
  • 7. Dependency Injection Dependency injection is basically providing the objects that an object needs (its dependencies) instead of having it construct them itself.
  • 8. How to Inject Object? ● Constructor Injection ● Method Injection ● Field Injection
  • 9. public class Twitter { public void getFeeds() { TwitterApi api = new TwitterApi(); api.getFeeds(); } } public class TwitterApi { public void getFeeds () { HttpClient httpClient = new HttpClient(); httpClient.connect("http://xxx.com") } }
  • 10. public class Twitter { public void getFeeds(TwitterApi api) { api.getFeeds(); } } public class TwitterApi { HttpClient httpClient; public TwitterApi(HttpClient httpClient) { this.httpClient = httpClient; } }
  • 11. What’s the problem now? ● Hard to use ● Manage dependency graph is a mess ● Dagger is the rescue
  • 13. Dagger 2 ● Dependency Injection tool ● Simple Annotation structure ● Readable generated code ● No reflection (fast) ● No runtime error ● Proguard friendly ● Pure Java ● https://github.com/google/dagger ● https://android.jlelse.eu/dagger-2-c00f31decda5
  • 14. Annotation Processing ● Annotate in source code ● Start compile ● Read Annotation and relevant data ● Generate code under build folder ● Compile finish ● https://medium.com/@jintin/annotation-pr ocessing-in-java-3621cb05343a
  • 15. Annotations in Dagger2 ● @Provides ● @Module ● @Component ● @Inject (in Java) ● @Qualifier (@Named) ● @Scope (@Singleton)
  • 17. // Object class class Lemon class Bee class Honey(var bee: Bee) class HoneyLemonade @Inject constructor(var honey: Honey, var lemon: Lemon)
  • 18. @Provides Lemon provideLemon() { return new Lemon(); } @Provides Honey provideHoney(Bee bee) { return new Honey(bee); } @Provides Bee provideBee() { return new Bee(); }
  • 19. @Module public class DrinkModule { @Provides Lemon provideLemon() {...} @Provides Honey provideHoney(Bee bee) {...} @Provides Bee provideBee() {...} }
  • 20. @Component(modules = {DrinkModule.class}) public interface DrinkComponent { HoneyLemonade drink(); void inject(MainActivity activity); }
  • 21. public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DrinkComponent co = DaggerDrinkComponent.create(); HoneyLemonade drink = co.drink(); } }
  • 22. public class MainActivity extends AppCompatActivity { @Inject HoneyLemonade drink; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DrinkComponent co = DaggerDrinkComponent.create(); co.inject(this); } }
  • 23. Recap ● Provides: provide dependency ● Modules: group Provides ● Component: The relation graph ● Inject: Identifier for inject
  • 24. Other feature ● Qualifier: Named... ● Scope: Singleton... ● Subcomponent / includes ● Lazy / Provider...
  • 25. Demo
  • 27. koin ● Service locator by Kotlin DSL ● No code generation ● No reflection ● No compile time overhead ● Pure Kotlin ● https://github.com/InsertKoinIO/koin
  • 28. Service locator The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer.
  • 30. Kotlin DSL ● Function literals with receiver ● Describe data structure ● Like Anko, ktor
  • 31. // HTML DSL val result = html { head { title {+"XML encoding with Kotlin"} } body { h1 {+"XML encoding with Kotlin"} p {+"this format can be used"} a(href = "http://kotlinlang.org") {+"Kotlin"} } }
  • 32. fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html } fun body(init: Body.() -> Unit) : Body { val body = Body() body.init() return body }
  • 33. koin DSL ● Module - create a Koin Module ● Factory - provide a factory bean definition ● Single - provide a singleton bean definition (also aliased as bean) ● Get - resolve a component dependency
  • 34. // gradle install dependencies { // Koin for Android compile 'org.koin:koin-android:1.0.2' // or Koin for Lifecycle scoping compile 'org.koin:koin-android-scope:1.0.2' // or Koin for Android Architecture ViewModel compile 'org.koin:koin-android-viewmodel:1.0.2' }
  • 35. // Object class class Bee class Honey(var bee: Bee) class Lemon class HoneyLemonade(var honey: Honey, var lemon: Lemon)
  • 36. class MyApplication : Application() { override fun onCreate(){ super.onCreate() // start Koin! startKoin(this, listOf(myModule)) } } val myModule = module { single { Bee() } single { Honey(get()) } single { Lemon() } single { HoneyLemonade(get(), get()) } }
  • 37. // How get() work? reified inline fun <reified T : Any> get( name: String = "", scopeId: String? = null, noinline parameters: ParameterDefinition = ... ): T { val scope: Scope? = scopeId?.let { koinContext.getScope(scopeId) } return koinContext.get(name, scope, parameters) }
  • 38. reified function ● Work with inline ● Can access actual type T ● After compile T will replace with actual type
  • 39. class MyActivity() : AppCompatActivity() { val drink : HoneyLemonade by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val drink : HoneyLemonade = get() } }
  • 40. // How inject(), get() work? inline fun <reified T : Any> ComponentCallbacks.inject( name: String = "", scope: Scope? = null, noinline parameters: ParameterDefinition = ... ) = lazy { get<T>(name, scope, parameters) } inline fun <reified T : Any> ComponentCallbacks.get( name: String = "", scope: Scope? = null, noinline parameters: ParameterDefinition = ... ): T = getKoin().get(name, scope, parameters) // What is ComponentCallbacks btw?
  • 41. Demo
  • 43. Dagger ● Pros: ○ + Pure Java ○ + Stable, flexible, powerful ○ + No Runtime error ○ + Fast in Runtime ● Cons: ○ - Compile overhead ○ - Hard to learn
  • 44. koin ● Pros: ○ + No Annotation processing ○ + Easy to learn/setup ● Cons: ○ - Hard to replace/remove ○ - Test in its env ○ - Error in Runtime
  • 45. “you and only you are responsible for your life choices and decisions” - Robert T. Kiyosaki