Automatic @Binds in Dagger when there is one implementation per interface

When using Dagger 2, very often I'm applying the following pattern:

  1. Create interface, let's call it LoginService
  2. Create the only one implementation - LoginServiceImpl with the constructor injection:

    class LoginServiceImpl implements LoginService {
       @Inject LoginServiceImpl() {}
    }
    
  3. Bind the implementation to the interface:

    @Binds
    abstract LoginService bindStatisticsService(LoginServiceImpl impl);
    
  4. Always depend on the interface - LoginService in this case.

Is there a possibility to avoid the @Binds annotated method? Is there a simpler (with the less amount of boilerplate code) way to tell Dagger - this class is the only one implementation of the interface, always bind it for example in some annotation on the interface itself?

I've read the documentation and unfortunately haven't found anything like that, but maybe someone knows some trick which can solve my problem.

Upvotes: 2

Views: 708

Answers (3)

Nicklas A.
Nicklas A.

Reputation: 7061

There is also AutoDagger which adds a way to automatically bind objects using annotations (disclosure: I'm the author).

Upvotes: 1

Sven Jacobs
Sven Jacobs

Reputation: 7005

I'm a bit late to the party but if you're still looking for a solution, Anvil — which works on top of Dagger — has this nice @ContributesBinding annotation which generates the binding code for you. I personally hope that a similar functionality will be added to Dagger / Hilt, too.

Upvotes: 1

Jeff Bowman
Jeff Bowman

Reputation: 95704

I don't think that's possible, even hypothetically.

Dagger can't really tell that there's only one implementation, and the only way it could would be to search for every class on the (compilation) classpath to try to find all sorts of possible implementation. That would be slow at best, but Java allows classloading from custom classloaders, so you aren't ever really guaranteed to get a full list of available classes. Even if you were to claim that Dagger should just match against the first appropriate assignable type it sees (because you know that there's only one), Dagger may not have a way to identify where to find that implementation type.

Ultimately it's going to be difficult to improve on a single-line @Binds statement that identifies the fully-qualified class of the binding key (your.package.name.LoginService) and target (your.package.name.LoginServiceImpl).

Upvotes: 3

Related Questions