Binding interface and implementation without module
See original GitHub issueThere’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:
- Created 5 years ago
- Reactions:14
- Comments:14
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
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:
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 exampleThe annotation processor just needs to generate the module: