All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.koin.experimental.builder.ScopeSetExt.kt Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package org.koin.experimental.builder

import org.koin.core.definition.BeanDefinition
import org.koin.core.definition.DefinitionFactory
import org.koin.core.definition.Options
import org.koin.core.error.DefinitionOverrideException
import org.koin.core.qualifier.Qualifier
import org.koin.dsl.ScopeSet

/**
 * Create a Single definition for given type T
 * @param qualifier
 * @param createOnStart - need to be created at start
 * @param override - allow definition override
 */
inline fun  ScopeSet.scoped(
        name: Qualifier? = null,
        override: Boolean = false
): BeanDefinition {
    val beanDefinition = DefinitionFactory.createScoped(name, qualifier) { create(this) }
    declareDefinition(beanDefinition, Options(false, override))
    val added = definitions.add(beanDefinition)
    if (!added) {
        throw DefinitionOverrideException("Can't add definition $beanDefinition as it already exists")
    }
    return beanDefinition
}

/**
 * Create a Factory definition for given type T
 *
 * @param qualifier
 * @param override - allow definition override
 */
inline fun  ScopeSet.factory(
        name: Qualifier? = null,
        override: Boolean = false
): BeanDefinition {
    val beanDefinition = DefinitionFactory.createFactory(name, qualifier) { create(this) }
    declareDefinition(beanDefinition, Options(false, override))
    val added = definitions.add(beanDefinition)
    if (!added) {
        throw DefinitionOverrideException("Can't add definition $beanDefinition as it already exists")
    }
    return beanDefinition
}

/**
 * Create a Single definition for given type T to modules and cast as R
 * @param qualifier
 * @param createOnStart - need to be created at start
 * @param override - allow definition override
 */
inline fun  ScopeSet.scopedBy(
        name: Qualifier? = null,
        override: Boolean = false
): BeanDefinition {
    val beanDefinition = DefinitionFactory.createScoped(name, qualifier) { create(this) as R }
    declareDefinition(beanDefinition, Options(false, override))
    val added = definitions.add(beanDefinition)
    if (!added) {
        throw DefinitionOverrideException("Can't add definition $beanDefinition as it already exists")
    }
    return beanDefinition
}

/**
 * Create a Factory definition for given type T to modules and cast as R
 *
 * @param qualifier
 * @param override - allow definition override
 */
inline fun  ScopeSet.factoryBy(
        name: Qualifier? = null,
        override: Boolean = false
): BeanDefinition {
    val beanDefinition = DefinitionFactory.createFactory(name, qualifier) { create(this) as R }
    declareDefinition(beanDefinition, Options(false, override))
    val added = definitions.add(beanDefinition)
    if (!added) {
        throw DefinitionOverrideException("Can't add definition $beanDefinition as it already exists")
    }
    return beanDefinition
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy