Last active
March 22, 2021 20:56
-
-
Save mlykotom/e4c82b4cea133e00929d2f6818b75c9b to your computer and use it in GitHub Desktop.
Lock your Dagger In Gradle Modules
This file contains hidden or 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
@Component( | |
dependencies = [ | |
FeatureComponent::class | |
] | |
) | |
@Singleton | |
interface AppComponent { | |
@Component.Factory | |
interface Factory { | |
fun create( | |
featureComponent: FeatureComponent, | |
): AppComponent | |
} | |
} |
This file contains hidden or 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
val featureComponent = DaggerFeatureComponent.factory().create() | |
val appComponent = DaggerAppComponent | |
.factory() | |
.create(featureComponent) |
This file contains hidden or 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
@Component( | |
dependencies = [ | |
FeatureComponentContract::class // dependency on contract | |
] | |
) | |
@Singleton | |
interface AppComponent { | |
@Component.Factory | |
interface Factory { | |
fun create( | |
featureComponent: FeatureComponentContract, // dependency on contract | |
): AppComponent | |
} | |
} |
This file contains hidden or 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
interface FeatureComponentContract { | |
val featureRepository: FeatureRepository | |
} | |
@Component(modules = [FeatureModule::class]) | |
@FeatureScope | |
interface FeatureComponent : FeatureComponentContract { | |
@Component.Factory | |
interface Factory { | |
fun create(): FeatureComponent | |
} | |
} |
This file contains hidden or 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
@Component(modules = [FeatureModule::class]) | |
interface FeatureComponent { | |
@Component.Factory | |
interface Factory { | |
fun create(): FeatureComponent | |
} | |
// explicitly specify, otherwise can't provide outside of the component | |
val featureRepository: FeatureRepository | |
} |
This file contains hidden or 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 | |
object FeatureModule { | |
@Provides | |
fun provideLibraryManager() = LibraryManager() | |
} |
This file contains hidden or 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 | |
abstract class FeatureModule { | |
@Binds | |
abstract fun bindsFeatureRepository(impl: FeatureRepositoryImpl): FeatureRepository | |
} |
This file contains hidden or 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
class FeatureRepository @Inject constructor( | |
private val libraryManager: LibraryManager | |
) { | |
fun doSomething() { | |
libraryManager.doSomething() | |
} | |
} |
This file contains hidden or 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
interface FeatureRepository { | |
fun doSomething() | |
} | |
internal class FeatureRepositoryImpl @Inject constructor() : FeatureRepository { | |
override fun doSomething() { | |
// ommitted | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment