question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic parameter injection

See original GitHub issue

I’m using the koin-android-architecture version of the library. I’ve come across a situation where I’d like to use a parameter determined when an Activity/Fragment is created for a ViewModel. As an example:

SomeActivity.kt

class SomeActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val id = getIdExtra() // From intent
        TODO("use id")
        val viewModel = getViewModel<SomeViewModel>()
        // ...
    }
}

SomeViewModel.kt

class SomeViewModel(
    private val repository: Repository, // Singleton
    private val id: Long
) : BaseViewModel() {
// ...
}

Is there a good way of doing this? I know that Kodein has factory bindings, so I was wondering if there was something similar.

This issue is similar to #6 but I was wondering if there was a better method of achieving this now since that issue is relatively old.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

17reactions
iRYO400commented, Oct 28, 2019

Hi there! How is it going in 2.0?

6reactions
arnaudgiulianicommented, Feb 22, 2018

I 've add Koin parameters to release 0.9.0.

you will be able to use parameters in your definition. Given class:

class MyPresenter(val activity : MyActivity)

We can use parameters to be injected with by inject()

val module = applicationContext {
   factory { params -> MyPresenter(params["activity"])}
}

Injecting the parameter:

class MyActivity : AppCompatActivity(){
   
   // Ask for MyPresenter injection and provide parameters
   val presenter : MyPresenter by inject( parameters = mapOf("activity" to this))
}

stay tuned👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency injection with dynamic parameter - Stack Overflow
The problem I have is that I wanted to inject Request so that I could change the library I was using and it...
Read more >
How to deal with dependency injection with dynamic ...
Let's say I have a class that requires some parameters in the constructor: class SomeClass { public function __constructor(AnotherClass $cls) {.
Read more >
Dynamic dependency injection by pwm - GitHub Pages
In this short article I would like to demonstrate a way to inject dependencies that are not known until runtime. There are many...
Read more >
Dynamic injection molding parameters used in this study.
In this paper, a numerical dynamic injection molding technology (DIMT) is presented, which is based on the finite element (FE) method. This numerical...
Read more >
How to fix SQL injection flaws for dynamic parameters
View This Post. STambade077368 (Customer) asked a question. March 8, 2022 at 7:16 AM. How to fix SQL injection flaws for dynamic parameters....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found