同步操作将从 Gitee 极速下载/koin 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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
All documentation, sample and references has been move to our website. Check the official website to get started: insert-koin.io. Koin samples project are located here: koin-samples
You can check the getting started section from our website, to discover Koin with the favorite platform. Or follow the snippets below.
Follow us on Twitter for latest news: @insertkoin_io
Need help? Come on slack Koin channel from Kotlin slack. Or just open an issue on Github issues to share your problem.
koin_version = '0.9.3'
Check that you have the jcenter
repository.
// Add Jcenter to your repositories if needed
repositories {
jcenter()
}
Choose your the Koin module:
// Koin for Kotlin
compile "org.koin:koin-core:$koin_version"
// Koin for Android
compile "org.koin:koin-android:$koin_version"
// Koin for Android Architecture Components
compile "org.koin:koin-android-architecture:$koin_version"
// Koin for Spark Kotlin
compile "org.koin:koin-spark:$koin_version"
// Koin for Ktor Kotlin
compile "org.koin:koin-ktor:$koin_version"
// Koin for JUnit tests
testCompile "org.koin:koin-test:$koin_version"
compile "org.koin:koin-android:$koin_version"
Let's create a Repository to provide some data (giveHello()
):
interface Repository {
fun giveHello(): String
}
class MyRepository() : Repository {
override fun giveHello() = "Hello Koin"
}
A Presenter class, for consuming this data:
// Use Repository - injected by constructor by Koin
class MyPresenter(val repository : Repository){
fun sayHello() = repository.giveHello()
}
Use the applicationContext
function to declare a module. Let's write our dependencies via constructor injection:
// Koin module
val myModule : Module = applicationContext {
factory { MyPresenter(get()) } // get() will resolve Repository instance
bean { MyRepository() as Repository }
}
By using the factory definition for our presenter, we will obtain a new instance each time we ask about the MyPresenter
class.
Now that we have a module, let's start it with Koin. Open your application class, or make one (don't forget to declare it in your manifest.xml). Just call the startKoin()
function:
class MyApplication : Application(){
override fun onCreate() {
super.onCreate()
// Start Koin
startKoin(this, listOf(myModule))
}
}
The MyPresenter
component will be created with Repository
instance. To get it from our Activity, let's inject it with the by inject()
delegate injector (we can't directly create Activitiy instances from Koin):
class MyActivity : AppCompatActivity(){
// Inject MyPresenter
val presenter : MyPresenter by inject()
override fun onCreate() {
super.onCreate()
// Let's use our presenter
Log.i("MyActivity","presenter : ${presenter.sayHello()}")
}
}
compile "org.koin:koin-android-architecture:$koin_version"
Let's create a Repository to provide some data (giveHello()
):
interface Repository {
fun giveHello(): String
}
class MyRepository() : Repository {
override fun giveHello() = "Hello Koin"
}
A ViewModel class, for consuming this data:
// Use Repository - injected by constructor by Koin
class MyViewModel(val repository : Repository) : ViewModel(){
fun sayHello() = repository.giveHello()
}
Use the applicationContext
function to declare a module. Let's write our dependencies via constructor injection:
// Koin module
val myModule : Module = applicationContext {
viewModel { MyViewModel(get()) } // get() will resolve Repository instance
bean { MyRepository() as Repository }
}
We are also using the viewModel
keyword to declare an Android ViewModel component.
Now that we have a module, let's start it with Koin. Open your application class, or make one (don't forget to declare it in your manifest.xml). Just call the startKoin()
function:
class MyApplication : Application(){
override fun onCreate() {
super.onCreate()
// Start Koin
startKoin(this, listOf(myModule))
}
}
The MyViewModel
component will be created with Repository
instance. To get it from our Activity, let's inject it with the by viewModel()
delegate injector (we can't directly create Activitiy instances from Koin):
class MyActivity : AppCompatActivity(){
// Inject MyViewModel
val myViewModel : MyViewModel by viewModel()
override fun onCreate() {
super.onCreate()
// Let's use our ViewModel
Log.i("MyActivity","ViewModel : ${myViewModel.sayHello()}")
}
}
Or if you want to eagerly create your ViewModel in a function, just use the getViewModel()
:
class MyActivity : AppCompatActivity(){
override fun onCreate() {
super.onCreate()
val myViewModel : MyViewModel = getViewModel()
// Let's use our ViewModel
Log.i("MyActivity","ViewModel : ${myViewModel.sayHello()}")
}
}
First, add the Koin dependency like below:
// Add Jcenter to your repositories if needed
repositories {
jcenter()
}
dependencies {
// Koin testing tools
testcompile 'org.koin:koin-test:{{ site.current_version }}'
}
Let's create a Repository to provide some data (giveHello()
):
interface Repository {
fun giveHello(): String
}
class MyRepository() : Repository {
override fun giveHello() = "Hello Koin"
}
A Presenter class, for consuming this data:
// Use Repository - injected by constructor by Koin
class MyPresenter(val repository : Repository){
fun sayHello() = repository.giveHello()
}
Use the applicationContext
function to declare a module. Let's declare our first component:
// Koin module
val myModule : Module = applicationContext {
bean { MyPresenter(get()) } // get() will resolve Repository instance
bean { MyRepository() as Repository }
}
To make our first test, let's write a simple Junit test file and extend it with KoinTest
. We will be able then, to use by inject()
operators.
class FirstTest : KoinTest {
val presenter : MyPresenter by inject()
val repository : Repository by inject()
@Before
fun before(){
startKoin(listOf(myModule))
}
@After
fun after(){
closeKoin()
}
@Test
fun testSayHello() {
assertEquals(repository.giveHello(), presenter.sayHello())
}
}
A quick recap of the Koin DSL keywords:
applicationContext
- create a Koin Modulefactory
- provide a factory bean definitionbean
- provide a bean definitionbind
- additional Kotlin type binding for given bean definitionget
- resolve a component dependencygetProperty
- resolve a propertycontext
- declare a logical contextSpecial keywords:
viewModel
- declare an Android ViewModel (koin-android-architecture only)controller
- declare a SparkJava controller (koin-spark only)Deprecated: provide
has been deprecated in favor to aliases. bean ~ provide
and factory ~ provide(isSingleton=false)
Here below the Koin DSL keywords you need to know, to write your module. To declare a module, use the applicationContext
function:
val myModule = applicationContext {
// your dependencies here
}
The applicationContext
lambda function is where you will write your definitions. myModule
is the Koin module
To define your components, use the following keywords:
bean
- define a singleton (create only one instance)factory
- define a factory (create a new instance each time)Deprecated: provide
keyword is now deprecated. Please use bean
or factory
Below a simple definition of a MyRepository
singleton:
class MyRepository()
val myModule = applicationContext {
bean { MyRepository() }
}
To bind a component with its interface, we have 2 solutions. Given an interface and its implmentation:
class MyRepositoryImpl()
interface MyRepository
We can write it:
bean { MyRepositoryImpl() as MyRepository }
- will create an instance of type MyRepository
bean { MyRepositoryImpl() } bind MyRepository::class
- will create an instance of type MyRepositoryImpl
and will accept to bind on type MyRepository
*You can use the bind
keyword with a class several times: bind Class1::class bind Class2::class
If you have mulitple definitions of the same type, Koin can't guess which instance to use. Then, you have to name each instance to clearly specify which instance to use. bean
and factory
have the name
parameter (default parameter).
class MyLocalRepositoryImpl()
class MyRemoteRepositoryImpl()
interface MyRepository
we will write our module like:
val myModule = applicationContext {
bean("local") { MyLocalRepositoryImpl() as MyRepository }
bean("remote") { MyRemoteRepositoryImpl() as MyRepository }
}
Koin push you to use constructor injection to bind your component. Given classes:
class ComponentA()
class ComponentB(val componentA : ComponentA)
We wil use the get()
function to resolve a dependency:
val myModule = applicationContext {
bean { ComponentA() }
bean { ComponentB(get()) }
}
Every definition and module is lazy be default in Koin. This means that you can assemble several modules, by using the list of desired modules. Given some classes:
class ComponentA()
class ComponentB(val componentA : ComponentA)
And the two modules to declare it:
val myModule1 = applicationContext {
bean { ComponentA() }
}
val myModule2 = applicationContext {
bean { ComponentB(get()) }
}
Just start the module list together:
// Android Start
startKoin(this,listOf(module1,module2))
// Kotlin/Spark Start
startKoin(listOf(module1,module2))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。