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.

Forwarding parameters

See original GitHub issue

Koin 0.9.0 is really great Thanks for the parameter support when injecting dependencies. It is working great.

I have a small optimization for injecting with parameters. It would be fantastic if we can forward this parameter map.

e.g.

class MyActivity : Activity() {
   val presenter: MyPresenter by inject(parameters = mapOf("activity" to this)
}

class MyPresenter(navigator: Navigator) {

}

class Navigator(activity: Activity) {

}

and the module looks like this

val myModule: Module = applicationContext {
   context("Screen") {
      bean { params -> MyPresenter(get()) }  // here we have the params with the activity 
      bean { params -> Navigator(params["activity"]) }  // BUT the activity is needed here (and here are params null)
   }
}

can you provide us with the possibillty to forward these params in the get() method, e.g

val myModule: Module = applicationContext {
   context("Screen") {
      bean { params -> MyPresenter(get(params)) }  // forward params in get(params)
      bean { params -> Navigator(params["activity"]) }  // so we can access the params here
   }
}

looking at the get() functions it should be possible in the current version 0.9.0 you pass an emptyMap to the resolve functions.

inline fun <reified T : Any> get(): T = koinContext.resolveByClass(emptyMap())
inline fun <reified T : Any> get(name: String): T = koinContext.resolveByName(name, emptyMap())

maybe we can do something like this:

inline fun <reified T : Any> get(parameters: ParameterMap = emptyMap()): T = koinContext.resolveByClass(parameters)
inline fun <reified T : Any> get(name: String, parameters: ParameterMap = emptyMap()): T = koinContext.resolveByName(name, parameters)

What do you think about this?

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
z3ntucommented, Dec 21, 2018

I’ve got it working now by using

single { params -> provideRetrofit(get { params }, params.component1()) }

Such an example should still be put on the website though. Thanks for creating Koin!

0reactions
z3ntucommented, Dec 20, 2018

Hello. (while waiting for my kotlin slack invitation I’m writing here)

I have the following module defined:

val nextNewsModule = module {
    single { (acc: SingleSignOnAccount) -> provideAuthenticationInterceptor(acc) }
    single { params -> provideHttpClient(get { params }) }
    single { (acc: SingleSignOnAccount) -> provideRetrofit(get { params }, acc) }
    single { params -> provideNewsService(get { params }) }
    single<NewsRepository> { params -> NewsRepositoryImpl(get { params }) }
    viewModel { params -> NewsViewModel(get { params }) }
}

and getting called by

private val vm: NewsViewModel by viewModel { parametersOf(SingleSignOnAccount("a", "b", "c", "d")) }

The problem before adding all the params -> was that the “downstream dependencies” didn’t get the required SingleSignOnAccount instance and Koin failed to inject. Now I think I just need to pass the params object through the provideRetrofit line (so it reaches the provideAuthenticationInterceptor) but with the extra (acc: SingleSignOnAccount) parameter I don’t know the syntax to make this working.

I got the params -> ... get { params } from here and it would also be nice if such an example was on the website.

Read more comments on GitHub >

github_iconTop Results From Across the Web

std::forward - cppreference.com
This example demonstrates perfect forwarding of the parameter(s) to the argument of the constructor of class T . Also, perfect forwarding of parameter...
Read more >
Configuring forwarding parameters
Configuring forwarding parameters. The following configurable parameters control the forwarding behavior of HP routing switches:.
Read more >
URL Redirect with Parameters - Domains - Namecheap.com
URL Redirect with Parameters. You can use URL Redirect to forward your visitors to a specific page at the destination URL and pass...
Read more >
URL Redirect parameters · Cloudflare Rules docs
Create rules that adjust incoming requests, change Cloudflare settings, or trigger actions.
Read more >
C++ static code analysis: "Forwarding references" parameters ...
"Forwarding references" parameters should be used only to forward parameters ... Forwarding references are a special kind of references that both ignore and ......
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