I’ve started to read about koin annotations. I’ve ...
# koin
p
I’ve started to read about koin annotations. I’ve never used before. So I’m trying to understand some points.
While using definitions, you may need to organize them in modules or not. You can even not use any module at all and use the “default” generated module. link
1) How is the cost of NOT use annotated modules? Thinking in a super app, like 90+ modules currently… I don’t know maybe a thousand of them. 2) I didn’t saw anything about tied scope using annotations for android activity. Is it possible yet? If not, why? For example, a module tied to SpecialActivity. 3) The usage of
@ComponentScan
can optimize ksp some how?
a
1. generation cost from KSP is really light. Koin Annotations generates the Koin DSL you would have written by hand. It's jsut a few lines
2. You can use @Scope to define a scope and @Scoped to make a definition inside it, but it's still need feedback
Copy code
@Scope
class MyScope

@Scoped(MyScope::class)
class MyComponent
is the same as
Copy code
scope<MyScope> {
   scoped { MyComponent()  }
}
3. @ComponentScan let KSP read given package for your definition
p
how can we use this:
Copy code
@Scope
class MyScope

@Scoped(MyScope::class)
class MyComponent
to make viewModelScoped as dagger hilt? I want one of my useCases to be scoped in viewmodel, so it is created with viewmodel and destroy with viewmodel
a
you don't need necessarly a scope. Naturally, a factory will follow up your ViewModel instance
just tag MyComponent with @Factory
p
yes, for single component it works, but I have different issue. I have MyComponent class that needs to be scoped to viewModel, but this class is not used directly. I have 2 usecases that use this component and these are created in viewmodel. When I use factory on usecases and component, the component is created twice which is wrong. I need solution to have everything scoped to viewmodel. example: MyViewModel(private val usecaseOne, private val usecaseTwo) class UseCaseOne(private val myComponent) class UseCaseTwo(private val myComponent)
a
yes, then you need scope. You need to declare you ViewModel in this scope then
p
Hey @arnaud.giuliani, please check this message. I explained what I did and what is not working ATM. https://kotlinlang.slack.com/archives/C67HDJZ2N/p1694526172956069
a
sure, I will help you 👍
did you tag your viewmodel with @Scope(MyScope::class) ?
p
lets continue the discussion on this thread: https://kotlinlang.slack.com/archives/C67HDJZ2N/p1694526172956069
👍 1