Skip to content

Commit

Permalink
Improve Koin registration
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Jun 1, 2023
1 parent 6990903 commit cd19d18
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,33 @@

package com.trendyol.kediatr.koin

import com.trendyol.kediatr.MediatorBuilder
import com.trendyol.kediatr.DependencyProvider
import com.trendyol.kediatr.MediatorBuilder
import org.koin.core.Koin
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.component.getScopeName
import org.koin.java.KoinJavaComponent.getKoin
import org.reflections.Reflections
import kotlin.reflect.KClass

@OptIn(KoinInternalApi::class)
class KediatRKoinProvider : DependencyProvider {
private val koin = getKoin()
private var reflections: Reflections

init {
val aPackage = koin.instanceRegistry.instances.entries.map {
it.value.beanDefinition.definition.getScopeName().type.java.`package`
}.first().name
val mainPackageName = Package.getPackages().filter { aPackage.startsWith(it.name) }.map { it.name }
reflections = Reflections(mainPackageName)
}

override fun <T> getSingleInstanceOf(clazz: Class<T>): T {
return koin.get(clazz.kClass())
}

override fun <T> getSubTypesOf(clazz: Class<T>): Collection<Class<T>> {
return reflections.getSubTypesOf(clazz).map { it as Class<T> }
}

private fun <T> Class<T>.kClass(): KClass<out Any> {
return (this as Class<*>).kotlin
}
private val koin: Koin = getKoin()
private val subTypes: List<KClass<*>>
get() = koin
.instanceRegistry.instances
.map { it.value.beanDefinition }
.fold(mutableListOf<KClass<*>>()) { acc, beanDefinition ->
acc.add(beanDefinition.primaryType)
acc.addAll(beanDefinition.secondaryTypes)
acc
}.distinct()

override fun <T> getSingleInstanceOf(clazz: Class<T>): T = koin.get(clazz.kClass())

override fun <T> getSubTypesOf(clazz: Class<T>): Collection<Class<T>> = subTypes
.filter { clazz.isAssignableFrom(it.java) }
.map { it.java as Class<T> }

private fun <T> Class<T>.kClass(): KClass<out Any> = (this as Class<*>).kotlin
}

class KediatRKoin {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.trendyol.kediatr.koin

import com.trendyol.kediatr.CommandHandler
import com.trendyol.kediatr.Command
import com.trendyol.kediatr.Mediator
import com.trendyol.kediatr.CommandHandler
import com.trendyol.kediatr.HandlerNotFoundException
import com.trendyol.kediatr.Mediator
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import org.koin.dsl.bind
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.inject
Expand All @@ -25,14 +24,14 @@ class CommandHandlerTests : KoinTest {
modules(
module {
single { KediatRKoin.getMediator() }
single { ExceptionPipelineBehavior() } bind ExceptionPipelineBehavior::class
single { LoggingPipelineBehavior() } bind LoggingPipelineBehavior::class
single { MyCommandHandler(get()) } bind CommandHandler::class
single { ExceptionPipelineBehavior() }
single { LoggingPipelineBehavior() }
single { MyCommandHandler(get()) }
}
)
}

private val mediator by inject<Mediator>()
private val mediator: Mediator by inject()

init {
springTestCounter = 0
Expand Down

0 comments on commit cd19d18

Please sign in to comment.