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.

Injecting Named Dependencies within Constructor

See original GitHub issue

Hello,

Is there a way to inject a named dependency within a constructor? And if not, do you have a recommended way of doing this? Let’s say I have the following:

val myModule = applicationContext {
    bean("A") { MyComponent() } 
    bean("B") { MyComponent() }
    bean { MyOtherComponent() }
}

And I have an object that I is instantiated using constructor injection:

class MyThirdComponent(val componentA: MyComponent, val otherComponent: MyOtherComponent) {
    ...
}

How would I specify which MyComponent I want in the MyThirdComponent constructor? I see in the docs how to specify which named component when injecting directly into other beans but not into a constructor.

I see some potential workarounds:

  • Not use construction injection on MyThirdComponent and create a bean instead:
val myModule = applicationContext {
    ...
    bean { MyThirdComponent(get("A"), get()) }
}

It defeats the purpose of constructor injection and requires two maintenance points (if I change the constructor of MyThirdComponent I would need to change the bean instantiation as well), but it could potentially work.

  • Inject what I can with the constructor, and then have MyThirdComponent extend KoinComponent and finish off the other injections with by inject:
class MyThirdComponent(val otherComponent: MyOtherComponent) : KoinComponent {
    val componentA: MyComponent by inject("A") 
    ...
}

It’s slightly more tedious but all of the injection is in one place and there’s no need to create an unnecessary bean in this case.

Am I missing something here/is there a better solution? If not, do you have any recommendations/thoughts on the above? I haven’t tested any of these solutions yet, and I just started playing around with Koin today so this might be wrong… My apologies if this has been answered already, I searched the docs / issues and couldn’t find anything on this subject.

Merci! Julien

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
saketcommented, Sep 6, 2019

FYI get() was updated and accepts a Qualifier instead of a String. So the new solution would be:

MyOtherComponent(get(named("A")))
0reactions
100nandoocommented, Dec 13, 2018

Hello @jguerinet, avoid using KoinComponent if you can. Try to always use constructor injection.

For you case, just use the name parameter as follow:

val myModule = applicationContext {
    bean("A") { MyComponent() } 
    bean("B") { MyComponent() }
    bean { MyOtherComponent(get("A"),get("B")) }
}

What is the reason behind that? Does using KoinComponent have slower performance? Or maybe slower compile time?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Constructor Dependency Injection in Spring - Baeldung
This quick tutorial will explore a specific type of DI technique within Spring called Constructor-Based Dependency Injection, which simply put, ...
Read more >
Understanding Constructor Injection - Manning
Constructor Injection is the act of statically defining the list of required Dependencies by specifying them as parameters to the class's ...
Read more >
Dependency Injection - TutorialsTeacher
Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and...
Read more >
4.3 Dependencies - Spring
The basic principle behind Dependency Injection (DI) is that objects define their dependencies (that is to say the other objects they work with)...
Read more >
Why You Should Use Constructor Injection in Spring
Constructor injection simplifies writing unit tests. The constructor forces us to provide valid objects for all dependencies. Using mocking ...
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