@Singleton
fun provideManager(net: Network, user: User): Manager
= ManagerImpl(net, user)
Salomon BRYS
Kotliner for 3 years
Author & maintainer of Kodein
Dependency injection separates the creation of a client’s dependencies from the client’s behavior, which allows program designs to be loosely coupled and to follow the dependency inversion and single responsibility principles.
Source: Microsoft
Business managers
Presenters (in MVP)
OS services
Anything that can be abstracted!
Warning | Dagger 2 is a valid solution, widely used and very reliable. All views expressed here are those of the author only. |
Allows all dependencies to be checked before compilation :)
You need to declare all injected classes
Weird compilation errors (missing dependency, circular dependency, etc.)
Less flexibility
Module configuration through annotations.
Unsemantic binding syntax.
@Singleton
fun provideManager(net: Network, user: User): Manager
= ManagerImpl(net, user)
Depends on Guava.
Adds 13k+ methods.
Annotation based.
Semantic (simple declarative DSL).
Idiomatic (best use of the Kotlin language)
Easy (to use & bootstrap).
Debuggable (understandable errors).
Android friendly (duh).
Java friendly (for legacy code).
Documented (heavily).
How about optimized?
Yes, #perfmatters, but only in critical path (see the latest ART)
The very majority of times, DI is not a critical path!
In DI, readability and debuggability are way more important !
Version 1.x (from apr. 2015): Concept
Version 2.x (from oct. 2015): Robust API
Deprecation of Injekt (26 jun. 2016)
Version 3.x (jul. 2016): Core reshaping & lots of features.
Version 4.x (jun. 2017): Compatible with JS.
Mainly
Jayson Minard (author of injekt) from Collokia, lots of help & insight.
Eliezer Graber from Insite Applications, huge investment in the Android module.
But also a lot of other smaller contributions from the community.
RTFM!
To understand the magic
user registers event at noon
user.register(event).at(noon)
inline fun <reified T> instance(): T
= container.getInstance(T::class.java)
val manager = instance<Manager>()
inline fun <reified T> instance(): T
= container.getInstance(T::class.java)
val manager: Manager = instance()
// └─────────────────┘
Let’s code, dammit!
Semantic (simple declarative DSL).
Idiomatic (best use of the Kotlin language)
Easy (to use & bootstrap).
Debuggable (understandable errors).
Android friendly (duh).
Java friendly (for legacy code).
Documented (heavily).
Thank you!