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.

Allow @AssistedInject objects to be injected and provided by @Provides methods

See original GitHub issue

I got some singleton/reusable, expensive-to-create-and-update key-value/preference objects in my app that I create by calling factory methods from @Provides methods. I’d think it’s a common and valid pattern, but not possible with @AssistedInject. I can work around it with ease, but every time I come across my hand-coded factory I wonder why it has to be there. I have to add code comments to explain and defend the design flaw.

This is what I’d like to do:

class ValueCache @AssistedInject constructor(repository: Repository, @Assisted key: String) {
    @AssistedFactory interface Factory {
        fun create(key: String): ValueCache
    }
}

// In some module...    
@Provides @Reusable @Named("LikesRed") fun provideUserLikesRedCache(cacheFactory: ValueCache.Factory):
    ValueCache = cacheFactory.create("userLikesRed")

@Provides @Reusable @Named("LikesGreen") fun provideUserLikesGreenCache(cacheFactory: ValueCache.Factory):
    ValueCache = cacheFactory.create("userLikesGreen")

Dagger does not support providing @AssistedInject types

Dagger does not support injecting @AssistedInject type

If I understand correctly, it’s essentially an arbitrary restriction in this specific case to dumb things down for inexperienced users in the general case. It violates API orthogonality in the specific case. There are various precursors in Dagger doing similar handholding restriction things, and they are generally a hard pill to swallow for an experienced user wanting to write clean, low-boilerplate code; I wish Dagger was generally maintained with less bias toward disallowing things.

A flag to disable the restriction could perhaps address the issue while maintaining the restriction in the general case, like an annotation parameter: @AssistedInject(allowInjection=true). Or a compile flag to disable the restriction in general, at the cost of letting me shoot myself in the foot. I prefer that, though, over having cluttered code.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
TevJcommented, Feb 10, 2021

I also experienced this issue when attempting to migrate from Jake’s original AssistedInject library which allowed this pattern to the Dagger integrated version. Good to know a fix is coming, thanks!

0reactions
bubenheimercommented, Feb 27, 2021

Adding an artificial annotation at every injection site is polluting, and easily missed; this is one benefit of custom types, less pollution, fewer errors. Writing a custom factory is a thousand times preferable over carrying around nonsensical annotations. It hurts my brain to consider such a thing.

You got the use case. You got the blueprint library behavior in front of you. You got more practical frameworks taking over. I guess that’s not enough to change Dagger’s ways.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Assisted Injection - Dagger
Assisted injection is a dependency injection (DI) pattern that is used to construct an object where some parameters may be provided by the...
Read more >
java - How to Inject an object which is created with Assisted ...
So let me explain a little. Firstly, you created a Factory which you will use to create instances of A . You did...
Read more >
Leveraging AssistedInjection to inject ViewModels with ...
This allows us to create assisted-injected ViewModels that receive SavedStateHandle in its constructor, without a lot of actual boilerplate at ...
Read more >
Providing AssistedInject supported ViewModel for ... - Medium
Providing AssistedInject supported ViewModel for Composable using Hilt. Let's inject ViewModels. Hey Androiders , The hilt has reached ...
Read more >
How to use Assisted Inject with Google Guice - YouTube
Outlines the other options that are available before resorting to Assisted Inject as well as Guice's annotation @Assisted and the ...
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