koin
koin copied to clipboard
Koin with feature layer SDK
Hello,
I'm building up a custom SDK with Koin with the following architectures where data -> domain <- ui
features/
├─ feature-a/
│ ├─ data/
│ ├─ domain/
│ ├─ ui/
├─ injector
In :feature-a:data
val exampleADataModule = module {
singleOf<ExampleAGreetings>(::ExampleAGreetingsImpl)
}
class ExampleAGreetingsImpl: ExampleAGreetings {
override fun getGreetings(): String = "Hello world"
}
In :feature-a:domain
interface ExampleAGreetings {
fun getGreetings(): String
}
class GetGreetingsUseCase(private val exampleAGreetings: ExampleAGreetings) {
operator fun invoke() = exampleAGreetings.getGreetings()
}
In :feature-a:ui which implements :injector
internal val exampleABModule = module {
factory {
GetGreetingsUseCase(get())
}
}
class ExampleAFragment: Fragment(R.layout.fragment_example_a), CustomKoinComponent {
private val useCase by inject<GetLoadHomeUseCase>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val tv = view.findViewById<TextView>(R.id.text)
tv.text = useCase()
}
}
interface CustomKoinComponent : KoinComponent {
override fun getKoin(): Koin = AwesomeSdk.koin
}
object AwesomeSdk {
private lateinit var appContext: Context
val koin by lazy {
koinApplication {
androidContext(appContext)
}.koin
}
@Synchronized
fun init(context: Context) {
appContext = context
}
}
Do you have advice with this kind of architecture ? How can I tell koin to load the exampleADataModule while keeping this dependency rule data -> domain <- ui ?