Last active
September 13, 2019 08:09
-
-
Save BenHenning/a7700243f0611abfac6a7489a0ce76bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module(subcomponents = [ChildComponent::class]) | |
class SomeModule { | |
@Provides | |
@Singleton | |
fun provideComplexDependency(componentDep: ComponentDep): ComplexDependency { ... } | |
} | |
----- | |
@Scope annotation class ChildScope | |
----- | |
@Subcomponent(modules = [SomeChildModule::class]) | |
@ChildScope | |
interface ChildComponent { ... } | |
----- | |
@Module | |
class SomeChildModule { | |
@Provides | |
@ChildScope | |
fun provideSpecialDependency(impliedDependency: ImpliedDependency): SpecialDependency { ... } | |
} | |
----- | |
// This class doesn't need an explicit provider since it has an @Inject constructor. | |
@Singleton class ImpliedDependency @Inject constructor() { ... } | |
----- | |
@Singleton | |
@Component(modules = [SomeModule::class]) | |
interface SingletonComponent { | |
@Component.Builder | |
interface Builder { | |
@BindsInstance fun setComponentDep(componentDep: ComponentDep): Builder | |
fun build(): SingletonComponent | |
} | |
fun inject(classWithFieldInjection: ClassWithFieldInjection) | |
} | |
----- | |
@Singleton | |
class ClassWithCtorInjection @Inject constructor(complexDependency: ComplexDependency) { ... } | |
----- | |
class ClassWithFieldInjection { | |
@Inject lateinit var impliedDependency: ImpliedDependency | |
fun setUpObject(componentDep: ComponentDep) { | |
DaggerSingletonComponent.builder().setComponentDep(componentDep).build().inject(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment