Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using Dagger in a Clean Architecture project

742 views

Published on

Clean Architecture and app modularization are often used together to achieve a better code structure and a faster build time. But how can we use Dagger in an app structured in that way? Can we use subcomponents (with or without Dagger Android) or are component dependencies enough?
In this talk we’ll see how to leverage Dagger to organize the dependencies in a multi-module project with particular attention to testing and decoupling. The examples will be both in a standard layered architecture and in a Clean Architecture where the Dependency Inversion increases the overall structure but can complicate the Dagger code.

Published in: Software
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Using Dagger in a Clean Architecture project

  1. 1. Using Dagger in a Clean Architecture project Fabio Collini
  2. 2. @fabioCollini linkedin.com/in/fabiocollini github.com/fabioCollini medium.com/@fabioCollini Android programmazione avanzata Android Developers Italia Ego slide
  3. 3. entitiesentities Layered Architecture Clean Architecture Dagger Modularization
  4. 4. Modularization
  5. 5. https://www.youtube.com/watch?v=PZBg5DIzNww
  6. 6. https://jeroenmols.com/blog/2019/03/06/modularizationwhy/
  7. 7. 1. Speeds up builds 2. Enable on demand delivery 3. Simplify development 4. Reuse modules across apps 5. Experiment with new technologies 6. Scale development teams 7. Enables refactoring 8. Simplifies test automation Modularization - Why you should care https://jeroenmols.com/blog/2019/03/06/modularizationwhy/
  8. 8. App feature3feature1 feature2
  9. 9. dynamic feature3 App feature1 feature2
  10. 10. App dynamic feature3 feature1 feature2 Feature1App
  11. 11. Main app BUILD SUCCESSFUL in 1m 12s 847 actionable tasks: 266 executed, 542 from cache, 39 up-to-date Feature app BUILD SUCCESSFUL in 43s 504 actionable tasks: 159 executed, 321 from cache, 24 up-to-date Feature module BUILD SUCCESSFUL in 30s 427 actionable tasks: 87 executed, 308 from cache, 32 up-to-date Core module BUILD SUCCESSFUL in 22s 308 actionable tasks: 63 executed, 220 from cache, 25 up-to-date Utils BUILD SUCCESSFUL in 6s 22 actionable tasks: 6 executed, 13 from cache, 3 up-to-date Poorman'sbenchmark
  12. 12. Main app BUILD SUCCESSFUL in 1m 12s 847 actionable tasks: 266 executed, 542 from cache, 39 up-to-date Feature app BUILD SUCCESSFUL in 43s 504 actionable tasks: 159 executed, 321 from cache, 24 up-to-date Feature module BUILD SUCCESSFUL in 30s 427 actionable tasks: 87 executed, 308 from cache, 32 up-to-date Core module BUILD SUCCESSFUL in 22s 308 actionable tasks: 63 executed, 220 from cache, 25 up-to-date Utils BUILD SUCCESSFUL in 6s 22 actionable tasks: 6 executed, 13 from cache, 3 up-to-date Poorman'sbenchmark
  13. 13. Architectures
  14. 14. Activity Repository api1 UseCase ViewModel
  15. 15. entitiesentitiesentitiesentities domain repository UI data source presenter Activity Repository api1 UseCase ViewModel
  16. 16. entitiesentitiesentitiesentities domain repository UI data source presenter Activity Repository api1 UseCase ViewModel
  17. 17. RepositoryImplRepositoryUseCase
  18. 18. domain repository UseCase RepositoryImpl Repository
  19. 19. domain repository Repository RepositoryImpl UseCase
  20. 20. Inversion Of Control The “I” in S.O.L.I.D.
  21. 21. entitiesentitiesentitiesentities domain repository UI data source presenter Activity Repository api1 UseCase ViewModel
  22. 22. TDD Pros Framework independence entitiesentitiesentitiesentities domain repository UI data source presenter
  23. 23. https://www.youtube.com/watch?v=GlDsfq3xHvo&t=
  24. 24. Dagger
  25. 25. App dynamic feature3 feature1 feature2 Feature1App
  26. 26. App dynamic feature3 feature1 feature2 Feature1App Dagger Dagger Dagger Dagger Dagger Dagger Dagger Dagger Dagger Dagger Dagger Dagger
  27. 27. Component dependencies Subcomponents Dagger Android Daggerpatterns
  28. 28. Component dependencies
  29. 29. https://medium.com/androiddevelopers/dependency-injection-in-a-multi-module-project-1a09511c14b7 https://proandroiddev.com/using-dagger-in-a-multi-module-project-1e6af8f06ffc
  30. 30. DaggerTip#1 @Component interface Component1 @Component(dependencies = [Component1::class]) interface Component2 fun main() { DaggerComponent2.builder() .build() }1
  31. 31. DaggerTip#1 Exception in thread "main" java.lang.IllegalStateException: Component1 must be set at dagger.internal.Preconditions.checkBuilderRequirement(Preconditions.java:95) at DaggerComponent2$Builder.build(DaggerComponent2.java:35) @Component interface Component1 @Component(dependencies = [Component1::class]) interface Component2 fun main() { DaggerComponent2.builder() .build() }1
  32. 32. DaggerTip#1 Exception in thread "main" java.lang.IllegalStateException: Component1 must be set at dagger.internal.Preconditions.checkBuilderRequirement(Preconditions.java:95) at DaggerComponent2$Builder.build(DaggerComponent2.java:35) @Component interface Component1 @Component(dependencies = [Component1::class]) interface Component2 fun main() { DaggerComponent2.builder() .component1(DaggerComponent1.create()) .build() }1
  33. 33. DaggerTip#1 @Component interface Component1 @Component(dependencies = [Component1::class]) interface Component2 { @Component.Factory interface Factory { fun create(component1: Component1): Component2 }2 }3 fun main() { DaggerComponent2.factory() .create(DaggerComponent1.create()) }1
  34. 34. DaggerTip#2 @Module class Module1 { @Provides fun pattern() = "#,###.00" @Provides fun format(pattern: String) = DecimalFormat(pattern) }1 Module1 pattern format
  35. 35. DaggerTip#2 @Module class Module1 { @Provides fun pattern() = "#,###.00" @Provides fun format(pattern: String) = DecimalFormat(pattern) }1 @Component(modules = [Module1::class]) interface Component1 { val format: DecimalFormat }2 Module1 Component1 pattern format format
  36. 36. DaggerTip#2 @Module class Module1 { @Provides fun pattern() = "#,###.00" @Provides fun format(pattern: String) = DecimalFormat(pattern) }1 @Component(modules = [Module1::class]) interface Component1 { val format: DecimalFormat }2 @Module class Module2 { }3 @Component( modules = [Module2::class], dependencies = [Component1::class] )5 interface Component2 { }4 Module2 Component2 Module1 Component1 pattern format format
  37. 37. DaggerTip#2 @Module class Module1 { @Provides fun pattern() = "#,###.00" }1 @Component(modules = [Module1::class]) interface Component1 { }2 @Module class Module2 { @Provides fun format(pattern: String) = DecimalFormat(pattern) }3 @Component( modules = [Module2::class], dependencies = [Component1::class] )5 interface Component2 { val format: DecimalFormat }4 Module2 Component2 format Module1 Component1 pattern format error: [Dagger/MissingBinding] String cannot be provided without an @Inject constructor or an @Provides-annotated method. public abstract interface Component2 { ^ String is injected at Module2.format(pattern) DecimalFormat is provided at Component2.getFormat()
  38. 38. DaggerTip#2 @Module class Module1 { @Provides fun pattern() = "#,###.00" }1 @Component(modules = [Module1::class]) interface Component1 { val pattern: String }2 @Module class Module2 { @Provides fun format(pattern: String) = DecimalFormat(pattern) }3 @Component( modules = [Module2::class], dependencies = [Component1::class] )5 interface Component2 { val format: DecimalFormat }4 Module2 Component2 format Module1 Component1 patternpattern format
  39. 39. DaggerTip#2 @Module class Module1 { @Provides fun pattern() = "#,###.00" }1 @Component(modules = [Module1::class]) interface Component1 { val pattern: String }2 @Module class Module2 { @Provides fun format(pattern: String) = DecimalFormat(pattern) }3 @Component( modules = [Module2::class], dependencies = [Component1::class] )5 interface Component2 { val format: DecimalFormat }4 Module2 Component2 format Module1 Component1 patternpattern format
  40. 40. DaggerTip#3 interface Component1 { val pattern: String }2 @Module class Module2 { @Provides fun format(pattern: String) = DecimalFormat(pattern) }3 @Component( modules = [Module2::class], dependencies = [Component1::class] )5 interface Component2 { val format: DecimalFormat }4
  41. 41. DaggerTip#3 interface Component1 { val pattern: String }2 @Module class Module2 { @Provides fun format(pattern: String) = DecimalFormat(pattern) }3 @Component( modules = [Module2::class], dependencies = [Component1::class] )5 interface Component2 { val format: DecimalFormat }4 fun main() { val component1 = object : Component1 { override val pattern ="#,###.0000" }3 val component2 = DaggerComponent2.factory().create(component1) }4
  42. 42. @Module class LibModule { @Provides fun provideMyLibObject() = MyLibObject() }3 @Component(modules = [LibModule::class]) interface LibComponent { val myLibObject: MyLibObject }1 Lib
  43. 43. @Module class LibModule { @Provides fun provideMyLibObject() = MyLibObject() }3 @Component(modules = [LibModule::class]) interface LibComponent { val myLibObject: MyLibObject }1 Lib feature2 Lib feature1
  44. 44. feature2 Lib feature1 @Component(dependencies = [LibComponent::class]) interface Feature1Component @Component(dependencies = [LibComponent::class]) interface Feature2Component @Module class LibModule { @Provides fun provideMyLibObject() = MyLibObject() }3 @Component(modules = [LibModule::class]) interface LibComponent { val myLibObject: MyLibObject }1 feat1feat2Lib
  45. 45. feature2 Lib feature1 @Singleton @Component(dependencies = [LibComponent::class]) interface Feature1Component @Singleton @Component(dependencies = [LibComponent::class]) interface Feature2Component @Module class LibModule { @Singleton @Provides fun provideMyLibObject() = MyLibObject() }3 @Singleton @Component(modules = [LibModule::class]) interface LibComponent { val myLibObject: MyLibObject }1 Libfeat1feat2
  46. 46. feature2 Lib feature1 @Scope annotation class Feature1SingletonScope @Feature1SingletonScope @Component(dependencies = [LibComponent::class]) interface Feature1Component @Scope annotation class Feature2SingletonScope @Feature2SingletonScope @Component(dependencies = [LibComponent::class]) interface Feature2Component @Scope annotation class LibSingletonScope @Module class LibModule { @LibSingletonScope @Provides fun provideMyLibObject() = MyLibObject() }3 @LibSingletonScope @Component(modules = [LibModule::class]) interface LibComponent { val myLibObject: MyLibObject }1 Libfeat1feat2
  47. 47. DaggerTip#4 @Module class LibModule { @Singleton @Provides fun provideMyLibObject() = MyLibObject() }3 @Singleton @Component(modules = [LibModule::class]) interface LibComponent { val myLibObject: MyLibObject }1
  48. 48. DaggerTip#4 @Module class LibModule { @Singleton @Provides fun provideMyLibObject() = MyLibObject() }3 @Singleton @Component(modules = [LibModule::class]) interface LibComponent { val myLibObject: MyLibObject }1 fun main() { val obj1 = DaggerLibComponent.create().myLibObject val obj2 = DaggerLibComponent.create().myLibObject println(obj1 === obj2) //false :( }2
  49. 49. DaggerTip#4 @Module class LibModule { @Singleton @Provides fun provideMyLibObject() = MyLibObject() }3 @Singleton @Component(modules = [LibModule::class]) interface LibComponent { val myLibObject: MyLibObject }1 fun main() { val component = DaggerLibComponent.create() val obj1 = component.myLibObject val obj2 = component.myLibObject println(obj1 === obj2) //true :) }2
  50. 50. interface LibComponentProvider { val libComponent: LibComponent }8 Lib
  51. 51. interface Feature1Provider { val feature1Component: Feature1Component }7 interface LibComponentProvider { val libComponent: LibComponent }8 feature1Lib
  52. 52. class MyApp : Application(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }6 interface Feature1Provider { val feature1Component: Feature1Component }7 interface LibComponentProvider { val libComponent: LibComponent }8 feature1LibApp
  53. 53. class MyApp : Application(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }6 interface Feature1Provider { val feature1Component: Feature1Component }7 fun Application.feature1Component() = (this as Feature1Provider).feature1Component interface LibComponentProvider { val libComponent: LibComponent }8 fun Application.libComponent() = (this as LibComponentProvider).libComponent Libfeature1App
  54. 54. class MyApp : Application(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }6 class MyFeature1App : Application(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }3 interface Feature1Provider { val feature1Component: Feature1Component }7 fun Application.feature1Component() = (this as Feature1Provider).feature1Component interface LibComponentProvider { val libComponent: LibComponent }8 fun Application.libComponent() = (this as LibComponentProvider).libComponent Libfeature1Feature1AppApp
  55. 55. interface ComponentHolder { val map: MutableMap<KClass<*>, Any> }1 open class ComponentHolderApp : Application(), ComponentHolder { override val map = mutableMapOf<KClass<*>, Any>() }2 inline fun <reified C : Any> ComponentHolder.getOrCreate(factory: () -> C): C = map.getOrPut(C::class, factory) as C A “real” implementation is available here: https://github.com/fabioCollini/CleanWeather/blob/dagger/kotlinUtils/ src/main/java/it/codingjam/cleanweather/kotlinutils/ComponentHolder.kt
  56. 56. class MyApp : Application(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }6 class MyFeature1App : Application(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }3 interface Feature1Provider { val feature1Component: Feature1Component }7 fun Application.feature1Component() = (this as Feature1Provider).feature1Component interface LibComponentProvider { val libComponent: LibComponent }8 fun Application.libComponent() = (this as LibComponentProvider).libComponent Libfeature1Feature1AppApp
  57. 57. class MyApp : ComponentHolderApp(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }6 class MyFeature1App : ComponentHolderApp(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }3 interface Feature1Provider { val feature1Component: Feature1Component }7 fun ComponentHolderApp.feature1Component() = (this as Feature1Provider).feature1Component interface LibComponentProvider { val libComponent: LibComponent }8 fun ComponentHolderApp.libComponent() = (this as LibComponentProvider).libComponent Libfeature1Feature1AppApp
  58. 58. class MyApp : ComponentHolderApp(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }6 class MyFeature1App : ComponentHolderApp(), LibComponentProvider, Feature1Provider { override val libComponent = DaggerLibComponent.create() override val feature1Component = DaggerFeature1Component.factory().create(libComponent) }3 interface Feature1Provider { val feature1Component: Feature1Component }7 fun ComponentHolderApp.feature1Component() = getOrCreate { }1 interface LibComponentProvider { val libComponent: LibComponent }8 fun ComponentHolderApp.libComponent() = getOrCreate { }2 Libfeature1Feature1AppApp
  59. 59. class MyApp : ComponentHolderApp(), LibComponentProvider, Feature1Provider class MyFeature1App : ComponentHolderApp(), LibComponentProvider, Feature1Provider interface Feature1Provider { val feature1Component: Feature1Component }7 fun ComponentHolderApp.feature1Component() = getOrCreate { DaggerFeature1Component.factory() .create(libComponent()) }1 interface LibComponentProvider { val libComponent: LibComponent }8 fun ComponentHolderApp.libComponent() = getOrCreate { DaggerLibComponent.create() }2 Feature1App App Libfeature1
  60. 60. class MyApp : ComponentHolderApp() class MyFeature1App : ComponentHolderApp() fun ComponentHolderApp.feature1Component() = getOrCreate { DaggerFeature1Component.factory() .create(libComponent()) }1 fun ComponentHolderApp.libComponent() = getOrCreate { DaggerLibComponent.create() }2 Libfeature1 Feature1App App
  61. 61. @Feature1SingletonScope @Component(dependencies = [Lib1Component::class, Lib2Component::class]) interface Feature1Component { //... } DaggerTip#5 error: @Feature1SingletonScope Feature1Component depends on more than one scoped component: @dagger.Component(dependencies = {Lib1Component.class, Lib2Component.class}) ^ @Lib1SingletonScope Lib1Component @Lib2SingletonScope Lib2Component
  62. 62. @Lib1SingletonScope @Component(modules = [Lib1Module::class]) interface Lib1Component { val myLibObject: MyLibObject }2 DaggerTip#5
  63. 63. interface Lib1Component { val myLibObject: MyLibObject }2 @Lib1SingletonScope @Component(modules = [Lib1Module::class]) interface Lib1ComponentImpl : Lib1Component DaggerTip#5
  64. 64. @Scope annotation class Lib1SingletonScope @Module class Lib1Module { @Lib1SingletonScope @Provides fun provideMyLibObject() = MyLibObject() }1 interface Lib1Component { val myLibObject: MyLibObject }2 @Lib1SingletonScope @Component(modules = [Lib1Module::class]) interface Lib1ComponentImpl : Lib1Component fun ComponentHolderApp.lib1Component(): Lib1Component = getOrCreate { DaggerLib1ComponentImpl.create() } DaggerTip#5
  65. 65. @Scope internal annotation class Lib1SingletonScope @Module internal class Lib1Module { @Lib1SingletonScope @Provides fun provideMyLibObject() = MyLibObject() }1 interface Lib1Component { val myLibObject: MyLibObject }2 @Lib1SingletonScope @Component(modules = [Lib1Module::class]) internal interface Lib1ComponentImpl : Lib1Component fun ComponentHolderApp.lib1Component(): Lib1Component = getOrCreate { DaggerLib1ComponentImpl.create() } DaggerTip#5
  66. 66. DaggerTip#5 interface Lib1Component { val myLibObject: MyLibObject }2 fun ComponentHolderApp.lib1Component(): Lib1Component = getOrCreate { DaggerLib1ComponentImpl.create() }
  67. 67. NODaggerTip interface Lib1Component { val myLibObject: MyLibObject }2 fun ComponentHolderApp.lib1Component(): Lib1Component = getOrCreate { DaggerLib1ComponentImpl.create() }
  68. 68. NODaggerTip interface Lib1Component { val myLibObject: MyLibObject }2 private class Lib1ComponentImpl : Lib1Component { override val myLibObject by lazy { MyLibObject() }3 }4 fun ComponentHolderApp.lib1Component(): Lib1Component = getOrCreate { Lib1ComponentImpl() }
  69. 69. Clean Architecture
  70. 70. domain repository Repository RepositoryImpl UseCase
  71. 71. class UseCase @Inject constructor( val repository: Repository) { //... }9 domain domain repository Repository RepositoryImpl UseCase
  72. 72. class UseCase @Inject constructor( val repository: Repository) { //... }9 @Component interface DomainComponent { val useCase: UseCase }2 domain domain repository Repository RepositoryImpl UseCase Domain Component
  73. 73. class UseCase @Inject constructor( val repository: Repository) { //... }9 @Module class DomainModule { @Provides fun provideRepository(): Repository = RepositoryImpl() }1 @Component(modules = [DomainModule::class]) interface DomainComponent { val useCase: UseCase }2 domain domain repository Repository RepositoryImpl UseCase Domain Component
  74. 74. @Component interface DomainComponent { val useCase: UseCase }2 domain domain repository Repository RepositoryImpl UseCase Domain Component
  75. 75. interface DomainDependencies { val repository: Repository }3 @Component( dependencies = [DomainDependencies::class] )9 interface DomainComponent { val useCase: UseCase }2 domain domain repository Repository RepositoryImpl UseCase Domain Dependencies Domain Component
  76. 76. interface DomainDependencies { val repository: Repository }3 @Component( dependencies = [DomainDependencies::class] )9 interface DomainComponent { val useCase: UseCase }2 @Component interface RepositoryComponent : DomainDependencies domainrepo domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component
  77. 77. fun ComponentHolderApp.domainComponent() = getOrCreate { DaggerDomainComponent.factory().create( ??? )3 }4 domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component domain
  78. 78. fun ComponentHolderApp.domainComponent() = getOrCreate { DaggerDomainComponent.factory().create( map[DomainDependencies::class] as DomainDependencies )3 }4 domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component domain
  79. 79. class MyApp : ComponentHolderApp() { override fun onCreate() { map[DomainDependencies::class] = DaggerRepositoryComponent.create() }1 }21 fun ComponentHolderApp.domainComponent() = getOrCreate { DaggerDomainComponent.factory().create( map[DomainDependencies::class] as DomainDependencies )3 }4 domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component domainApp
  80. 80. interface DomainDependencies { val repository: Repository }2 @Component(dependencies = [DomainDependencies::class]) interface DomainComponent { val useCase: UseCase }3 fun ComponentHolderApp.domainComponent() = getOrCreate { DaggerDomainComponent.factory().create( ??? )4 }5 @Component interface RepositoryComponent : DomainDependencies domainrepo domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component
  81. 81. interface DomainDependencies { val repository: Repository }2 @get:InversionDef val ComponentHolderApp.domainDependencies by Inversion.of(DomainDependencies::class) @Component(dependencies = [DomainDependencies::class]) interface DomainComponent { val useCase: UseCase }3 fun ComponentHolderApp.domainComponent() = getOrCreate { DaggerDomainComponent.factory().create( ??? )4 }5 @Component interface RepositoryComponent : DomainDependencies domainrepo domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component
  82. 82. interface DomainDependencies { val repository: Repository }2 @get:InversionDef val ComponentHolderApp.domainDependencies by Inversion.of(DomainDependencies::class) @Component(dependencies = [DomainDependencies::class]) interface DomainComponent { val useCase: UseCase }3 fun ComponentHolderApp.domainComponent() = getOrCreate { DaggerDomainComponent.factory().create( domainDependencies() )4 }5 @Component interface RepositoryComponent : DomainDependencies domainrepo domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component
  83. 83. interface DomainDependencies { val repository: Repository }2 @get:InversionDef val ComponentHolderApp.domainDependencies by Inversion.of(DomainDependencies::class) @Component(dependencies = [DomainDependencies::class]) interface DomainComponent { val useCase: UseCase }3 fun ComponentHolderApp.domainComponent() = getOrCreate { DaggerDomainComponent.factory().create( domainDependencies() )4 }5 @Component interface RepositoryComponent : DomainDependencies @InversionProvider fun ComponentHolderApp.provideDomainDependencies(): DomainDependencies = getOrCreate { DaggerRepositoryComponent.create() }6 domainrepo domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component
  84. 84. interface DomainDependencies { val repository: Repository }2 @get:InversionDef val ComponentHolderApp.domainDependencies by Inversion.of(DomainDependencies::class) @Component(dependencies = [DomainDependencies::class]) interface DomainComponent { val useCase: UseCase }3 fun ComponentHolderApp.domainComponent() = getOrCreate { DaggerDomainComponent.factory().create( domainDependencies() )4 }5 @Component interface RepositoryComponent : DomainDependencies @InversionProvider fun ComponentHolderApp.provideDomainDependencies(): DomainDependencies = getOrCreate { DaggerRepositoryComponent.create() }6 domainrepo domain repository Repository RepositoryImpl UseCase Repository Component Domain Dependencies Domain Component
  85. 85. Testing
  86. 86. class MainActivity : AppCompatActivity() { @Inject lateinit var useCase: UseCase override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val activityComponent = DaggerMainActivityComponent.factory().create( (application as ComponentHolderApp).domainComponent(), this )1 activityComponent.inject(this) setContentView(R.layout.activity_main) findViewById<TextView>(R.id.text).text = useCase.retrieve() }2 }3
  87. 87. class MainActivity : AppCompatActivity() { @Inject lateinit var useCase: UseCase override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val activityComponent = DaggerMainActivityComponent.factory().create( (application as ComponentHolderApp).domainComponent(), this )1 activityComponent.inject(this) setContentView(R.layout.activity_main) findViewById<TextView>(R.id.text).text = useCase.retrieve() }2 }3
  88. 88. class MainActivity : AppCompatActivity() { @Inject lateinit var useCase: UseCase override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val activityComponent = DaggerMainActivityComponent.factory().create( (application as ComponentHolderApp).domainComponent(), this )1 activityComponent.inject(this) setContentView(R.layout.activity_main) findViewById<TextView>(R.id.text).text = useCase.retrieve() }2 }3
  89. 89. class MainActivityTest { @get:Rule val rule = ActivityTestRule(MainActivity::class.java, false, false) private val useCaseMock = mock<UseCase>() }4
  90. 90. class MainActivityTest { @get:Rule val rule = ActivityTestRule(MainActivity::class.java, false, false) private val useCaseMock = mock<UseCase>() @Before fun setUp() { }2 }4
  91. 91. class MainActivityTest { @get:Rule val rule = ActivityTestRule(MainActivity::class.java, false, false) private val useCaseMock = mock<UseCase>() @Before fun setUp() { val app = ApplicationProvider.getApplicationContext<ComponentHolderApp>() app.map.clear() app.map[DomainComponent::class] = object : DomainComponent { override val useCase = useCaseMock }1 }2 }4
  92. 92. class MainActivityTest { @get:Rule val rule = ActivityTestRule(MainActivity::class.java, false, false) private val useCaseMock = mock<UseCase>() @Before fun setUp() { val app = ApplicationProvider.getApplicationContext<ComponentHolderApp>() app.map.clear() app.map[DomainComponent::class] = object : DomainComponent { override val useCase = useCaseMock }1 }2 @Test fun launchActivity() { whenever(useCaseMock.retrieve()) doReturn "ABCDEF" rule.launchActivity(null) onView(withId(R.id.text)) .check(matches(withText("ABCDEF"))) }3 }4
  93. 93. class MainActivityTest { @get:Rule val rule = ActivityTestRule(MainActivity::class.java, false, false) private val useCaseMock = mock<UseCase>() @Before fun setUp() { val app = ApplicationProvider.getApplicationContext<ComponentHolderApp>() app.map.clear() app.map[DomainComponent::class] = object : DomainComponent { override val useCase = useCaseMock }1 }2 @Test fun launchActivity() { whenever(useCaseMock.retrieve()) doReturn "ABCDEF" rule.launchActivity(null) onView(withId(R.id.text)) .check(matches(withText("ABCDEF"))) }3 }4
  94. 94. Wrappingup Component dependencies Each module exposes: a Component interface a method to create the Component A map in the App can be useful to: manage singletons replace real objects with fakes/mocks
  95. 95. Links Demo Project github.com/fabioCollini/CleanWeather/tree/dagger Droidcon Italy talk - SOLID principles in practice: the Clean Architecture youtube.com/watch?v=GlDsfq3xHvo&t= Implementing Dependency Inversion using Dagger components medium.com/google-developer-experts/implementing-dependency-inversion-using-dagger-components-d6b0fb3b6b5e Inversion library github.com/fabioCollini/Inversion Yigit Boyar, Florina Muntenescu - Build a Modular Android App Architecture (Google I/O'19) youtube.com/watch?v=PZBg5DIzNww Jeroen Mols - Modularization - Why you should care jeroenmols.com/blog/2019/03/06/modularizationwhy/ Ben Weiss - Dependency injection in a multi module project medium.com/androiddevelopers/dependency-injection-in-a-multi-module-project-1a09511c14b7 Marcos Holgado - Using Dagger in a multi-module project proandroiddev.com/using-dagger-in-a-multi-module-project-1e6af8f06ffc
  96. 96. THANKS FOR YOUR ATTENTION
  97. 97. QUESTIONS? @fabioCollini

×