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.

Binding interface and implementation without module

See original GitHub issue

There’re cases when module provides only one object:

class BroadcastModule {
  @Provides
  open fun provideBroadcastBus(inst: BroadcastBusImpl): BroadcastBus = inst
}

And this seems like an overkill: create a module class to bind one implementation with interface for only one object.

Is there any plan to introduce some way to bind interface with implementation?

For instance annotation to generate such kind of modules:

class BroadcastBusImpl @Inject constructor():BroadcastBus {
  //methods
}
@Implementation(BroadcastBusImpl.class)
interface BroadcastBus{
  //methods
}

Or even better: if there is only one implementation then use it, so no need to have annotation or module declaration. And this behaviour could be probably used to replace modules which provide multiple objects, but still have one interface and implementation.

ps I don’t mean cases when interface is not needed. If it’s possible to use implementation then of course there is no problem.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:14

github_iconTop GitHub Comments

3reactions
sczerwinskicommented, Jan 3, 2021

@JavierSegoviaCordoba yes, but it will take a few days before it’s ready. I can post a link when it’s ready.

EDIT: If anyone is still looking for an existing annotation processor generating Hilt modules, I’ve released an open source library that does that: https://github.com/sczerwinski/android-hilt#generating-hilt-modules

3reactions
bcorsocommented, Aug 8, 2020

It’s not feasible in Dagger, but it is definitely possible in Hilt (which is more similar to Anvil). However, we’ve been hesitant because the APIs for this aren’t great. We’ve had people write annotation processors on top of Hilt for this type of feature (internally) but even to handle only a subset of the use cases it ends up looking like this:

// Equivalent to:  @Binds @FooQualifier Foo<Bar> bind(FooImpl impl);
@ContributesTo(
  to = Foo.class,
  typeParameter = Bar.class,
  qualifier = FooQualifier.class,
  components = ApplicationComponent.class,
)
class FooImpl extends ... {...}

It looks like Anvil has intentionally left the API simple – it only handles cases where the class extends a single type that is the exact type you want to bind to. This might be fine for the majority of use cases though.

FWIW, we’re not opposed to someone else writing an annotation processor on top of Hilt to do this. It would be a pretty trivial annotation processor to replicate @ContributesTo. For example

@ContributesTo(ApplicationComponent.class)
class FooImpl implements Foo {...}

The annotation processor just needs to generate the module:

@Module
@InstallIn(ApplicationComponent.class)
interface FooImpl_ContributesTo {
  @Binds Foo bind(FooImpl impl);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Binding without interfaces - Google Groups
my codebase only implement interfaces where there are multiple implementations of that interface) . Guice does not permit me to do this:
Read more >
How do I bind the Interface with Implementation for Generic ...
It works just fine! Let's now incorporate the interface: public class Module extends AbstractModule { @Override ...
Read more >
Inject interfaces without provide methods on Dagger 2 - Medium
If you have provide methods which just call constructor of implementation classes to inject interfaces, use @Binds annotation instead to get rid of...
Read more >
Providing Interface Implementations with Dagger - Android Stuff
To do this we start by binding our modules to the engine implementations. @Module interface PetrolEngineModule { @Binds Engine engine( ...
Read more >
Depending on an interface - Dagger
Dagger does not actually call this method or provide an implementation for it. ... Modules are collections of binding methods (methods annotated with...
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