5

Is there a way to tell Dagger 2 how to provide something, but not allow it to be injected?

Say I want to inject a Qux. A Qux requires a Foo and a Bar:

@Provides
@Singleton
Foo foo() {
    return new Foo();
}

@Provides
@Singleton
Bar bar() {
    return new Bar();
}

@Provides
@Singleton
Qux qux(final Foo foo, final Bar bar) {
    return new Qux(foo, bar);
}

But what if I don't want Foo and Bar to be injectable? Perhaps exposing them would break the encapsulation of the Qux, or perhaps they're factories of some kind that I only want the Qux to have access to.

I've thought of a couple ways I could achieve this:

  • If the Foo singleton is needed by other providers, I could make it a class member. But this would get messy if Foo has several dependencies of its own.
  • If the Bar singleton is not needed by any other providers, I could create the instance inside the Qux provider. But this would get messy if Qux has several dependencies like that.

Neither of these solutions seem very elegant or Daggery. Is there another way?

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82

3 Answers3

2

I'd say this sounds a lot like component dependencies.

[..] As an alternative, components can use bindings only from another component interface by declaring a component dependency. When a type is used as a component dependency, each provision method on the dependency is bound as a provider. Note that only the bindings exposed as provision methods are available through component dependencies.

If you really want to hide your Foo and Bar, you could make the module, Foo, and Bar package local. Then create a component that only exposes Qux and use it as a component dependency.

Since Foo and Bar are package local, a SubComponent should not be able to use them, either.
Your component would be the only one able to use the module and thus create and use Foo and Bar in its dependency graph.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
2

The easiest way to achieve what you're trying to do is to define a package-private qualifier.

package my.pkg;

@Qualifier
@Retention(RUNTIME)
@interface ForMyPackage {}

Then, use that for your bindings for Foo and Bar:

@Provides
@Singleton
@ForMyPackage
Foo foo() {
    return new Foo();
}

@Provides
@Singleton
@ForMyPackage
Bar bar() {
    return new Bar();
}

@Provides
@Singleton
Qux qux(final @ForMyPackage Foo foo, final @ForMyPackage Bar bar) {
    return new Qux(foo, bar);
}

That way, you can only request that those versions of Foo and Bar be injected if you have access to the qualifier.

If all of these bindings are in a single module, you can even use a private, nested qualifier in the module.

Edit by asker:

I tried the last suggestion.

@Qualifier
@Retention(RUNTIME)
private @interface NoInject {}

@Provides
@Singleton
@NoInject
Foo foo() { return new Foo(); }

Attempting to inject a Foo causes a compile-time error, as desired:

Error:(15, 6) Gradle: error: com.mydomain.myapp.Foo cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. com.mydomain.myapp.MyActivity.foo [injected field of type: com.mydomain.myapp.Foo foo]

So while the error message is a bit misleading, this technique is neat and effective.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
gk5885
  • 3,742
  • 21
  • 16
  • Yes, if the bindings for `Foo` and `Bar` are in different packages from the binding for `Qux` then this won't work, but I would argue that if you want this relationship it is a little odd that it would cross the package boundary. – gk5885 Aug 30 '16 at 18:21
1

A repost of my answer elsewhere...

A simple way to do this is:

@Retention(BINARY)
@Qualifier
private annotation class InternalApi

@Module
object NetworkModule {
  @Provides 
  @InternalApi 
  fun provideClient(): OkHttpClient {
    //...
  }

  @Provides
  fun provideRetrofit(
    @InternalApi client: Lazy<OkHttpClient>
  ): Retrofit {
    //...
  }
}

culled from here.

Basically, we create private qualifiers- in this case, @InternalApi that is used to qualify the OkHttpClient dependency thus making it private to our module.

Shayne3000
  • 607
  • 8
  • 5