@AutoBind
Usage¶
To bind objects automatically you just need to annotate a class with the @AutoBind
annotation (or @AutoBindIntoSet
for binding into a set and @AutoBindIntoMap
for binding into a map).
interface Repository
@AutoBind
@Singleton
class RealRepository @Inject constructor() : Repository
// Generates the equivalent to:
@Binds
abstract fun RealRepository.bindRealRepositoryAsRepository(): Repository
Multibindings¶
If you need to bind a object into a set or map you can use @AutoBindIntoSet
or @AutoBindIntoMap
respectively.
// Binds ExternalResource as Closeable using @IntoSet
@AutoBindIntoSet
// Binds ExternalResource as Closeable using @IntoMap with
// `StringKey` as the map key
@AutoBindIntoMap
@StringKey("ExternalResourceCloseable")
@Singleton
class ExternalResource @Inject constructor() : Closeable {
override fun close() {}
}
// Generates the equivalent to:
@Binds
@IntoSet
abstract fun ExternalResource.bindExternalResourceAsCloseableIntoSet(): Closeable
@Binds
@IntoMap
@StringKey("ExternalResourceCloseable")
abstract fun ExternalResource.bindExternalResourceAsCloseableIntoMap(): Closeable
Component¶
AutoDagger will try to infer the component to install the binding in using the scope. The following mapping is used:
Scope | Component |
---|---|
No scope | SingletonComponent |
Singleton | SingletonComponent |
Reusable | SingletonComponent |
ActivityRetainedScoped | ActivityRetainedComponent |
ActivityScoped | ActivityComponent |
FragmentScoped | FragmentComponent |
ServiceScoped | ServiceComponent |
ViewScoped | ViewComponent |
ViewModelScoped | ViewModelComponent |
ViewWithFragmentScoped | ViewWithFragmentComponent |
If you are are using custom scopes or want to change which component the binding is installed in, you can use the inComponent
property:
@AutoBind(inComponent = SomeComponent::class)
class ExternalResource @Inject constructor() : Closeable {
override fun close() {}
}
Binding multiple types¶
If your object has multiple direct supertypes, you need to specify which ones to bind explicitly using the asTypes
parameter:
@AutoBind(asTypes = [Closeable::class])
class ExternalResource @Inject constructor() : Runnable, Closeable {
override fun run() {}
override fun close() {}
}
You can bind multiple types, but only direct supertypes can be bound (see limitations).
Objects¶
Normally the bound object needs to be provided to the dependency graph using either an @Provides
annotated method or using an @Inject
annotated constructor.
Auto Dagger allows you to annotate a Kotlin object with @AutoBind
without it being provided in the graph. This is especially useful for tests:
@AutoBind
object DirectExecutor : Executor {
override fun execute(command: Runnable) {
command.run()
}
}
Generics¶
When using multibindings, it's often useful to be able to bind generic types as wildcard.
Auto Dagger will, by default, bind the exact type of the supertype. But using the bindGenericAs
parameter you can chose to bind it as a wildcard instead.
For types you own, it's sometimes useful to specify a different default for bindGenericAs
. For example, say you have a Resource<T>
interface. You probably want to always bind this using wildcards since every resource will have a unique type for T
. So you can annotate Resource
with @BindGenericAs.Default(BindGenericAs.Wildcard)
to indicate that, unless specified explicitly, it should be bound as Resource<*>
.
// This will bind StringCallable as Callable<*>
@AutoBindIntoSet(bindGenericAs = BindGenericAs.Wildcard)
class StringCallable @Inject constructor() : Callable<String> {
override fun call(): String = ""
}
// For types you own you can specify @BindGenericAs.Default to change the default.
@BindGenericAs.Default(BindGenericAs.Wildcard)
interface Resource<T>
// This will be bound as Resource<*> since the default has been set to wildcard for Resource.
@AutoBindIntoSet
class SomeResource @Inject constructor() : Resource<Something>
There are 3 options for bindGenericAs
:
Type
- Binds the object to the exact supertype (Callable<String>
in the example above). This is the default unless set with@BindGenericAs.Default
.Wildcard
- Binds the object using wildcards (Callable<*>
in the example above).TypeAndWildcard
- Binds the object as both the exact type and using wildcards.