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.

Binds annotation for scoping dependencies

See original GitHub issue

Frequently in our applications, we would like to apply a scope annotation to a class, and given that most of the time constructor injections is perfectly appropriate for binding resolution, the scope annotation must go elsewhere.

It’s not desirable to have a scope annotation on the implementation of the class, as this confuses the how to the why, so we must use an annotation in a module, often mimicking the required constructor injection.

class OfferRepository(OfferCache cache) { ... }
@Module
class OfferModule {

  @Provides
  @PerActivity
  OfferRepository offerRepository(OfferCache offerCache) {
    return new OfferRepository(offerCache);
  }
}

Alternatively, it would be nice to be able to apply a @Binds annotation similar to how you would for an interface implementation, but simply just for the class via constructor injection.

For example

class OfferRepository {

  @Inject
  OfferRepository(OfferCache offerCache) { ... }
}
@Module
abstract class OfferModule {

  @Binds
  @PerActivity
  abstract OfferRepository offerRepository()
}

I had thought to try abstract OfferRepository offerRepository(offerRepository: OfferRepository) as a proposal but this seems a bit too verbose, and returning this value with a @Provides annotation unsurprisingly causes a cyclical dependency cycle.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

1reaction
tasomaniaccommented, Oct 12, 2017

I remember this being discussed multiple times. I think it is just about being more flexible and giving people options. If you have a base interface, then you need to use Binds in a module and that is where you can put the scope annotation. So why not give the possibility/flexibility to declare scopes also in the module for constructor injected concrete classes.

1reaction
ashdaviescommented, Oct 9, 2017

@ronshapiro potentially, as I mentioned it might be acceptable to simply use a scope alias indicating that the class contains state, a common use case I would imagine since performance based persistence can typically rely on @Reusable.

But primarily its the concern that the class shouldn’t be responsible for specifying its own scope, in the same way that an injection target shouldn’t be responsible for its own injection.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inject vs Provides vs Binds in Dagger and Hilt
@Binds is a very specialized annotation though—it's used to map an interface to an implementation. It can take only a single parameter and...
Read more >
Dagger2 dependency Cycle by Using @Binds and @Inject fields
@Binds isn't doing any scoping. Its only job is to tell dagger about different implementations. You can add @XScope with @Binds to make...
Read more >
Dagger 2 Annotations: @Binds & @ContributesAndroidInjector
Here's an example using Fragments. Scoping with @ContributesAndroidInjector. If you want to scope dependencies inside of your LoginModule , then ...
Read more >
Dagger Scopes —Let's explain it simply | by Kacper Hreniak
A scope is an annotations class with specific additional annotations: @Scope and @Retention. @Scope annotation is provided by Dagger library ...
Read more >
Android - Dagger
Singletons and Scoped Bindings ... Annotate an @Provides method or injectable class with @Singleton . The graph will use a single instance of...
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