Slide 1

Slide 1 text

Scaling Dagger DI in a modular world Droidcon Madrid 2019

Slide 2

Slide 2 text

@CesarDielo César Puerta Technical Lead Twitter for Android @mrmans0n Nacho López Software Engineer Android Foundation Architecture

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Author: Ben Sandofsky Date: Mon Jun 21 23:24:55 2010 -0700 Initial import.

Slide 6

Slide 6 text

Author: Ben Sandofsky Date: Mon Jun 21 23:24:55 2010 -0700 Initial import.

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Periscope @PeriscopeCo You might have heard some news: It involves a blue bird. #YouCanGuessTheRest #WeJoinedTheFlockInJanuary 13 Mar 2015 629 761

Slide 16

Slide 16 text

+

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

D D

Slide 22

Slide 22 text

DI is important

Slide 23

Slide 23 text

DI is important Reusability for…

Slide 24

Slide 24 text

DI is important Testability for…

Slide 25

Slide 25 text

DI is important Correctness for…

Slide 26

Slide 26 text

DI is important Refactoring for…

Slide 27

Slide 27 text

regardless of app size DI is important

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Static checks

Slide 31

Slide 31 text

Static checks Errors at compile time > Runtime errors

Slide 32

Slide 32 text

Description: [Dagger/MissingBinding] com.twitter.ui.util.UiArchLayoutContentViewProvider cannot be provided without an @Inject constructor or an @Provides-annotated method. Description: [Dagger/DuplicateBindings] com.twitter.ui.util.UiArchLayoutContentViewProvider is bound multiple times.

Slide 33

Slide 33 text

Code generation

Slide 34

Slide 34 text

Code generation Automating the creation of glue code

Slide 35

Slide 35 text

val inflater = LayoutInflater.from(context) val renderer = Renderer(inflater) val database = Database() val timeline = Timeline(database, renderer) class Renderer (database: Database, renderer: Renderer) class Timeline class Database() (inflater: LayoutInflater)

Slide 36

Slide 36 text

class Renderer (database: Database, renderer: Renderer) class Timeline class Database () (inflater: LayoutInflater) @Inject constructor @Inject constructor @Inject constructor

Slide 37

Slide 37 text

Graph Architecture

Slide 38

Slide 38 text

Graph Architecture Scoped object graphs mapping to the app lifecycle

Slide 39

Slide 39 text

Graph Architecture Scoped object graphs mapping to the app lifecycle @Component @Subcomponent

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

@ApplicationScope @UserScope @RetainedScope @ViewScope

Slide 45

Slide 45 text

@Component(modules = [ApplicationModule::class]) @ApplicationScope interface ApplicationObjectGraph

Slide 46

Slide 46 text

@Component(modules = [ApplicationModule::class]) @ApplicationScope interface ApplicationObjectGraph { fun userObjectGraphBuilder(): UserObjectGraph.Builder }

Slide 47

Slide 47 text

@Subcomponent(modules = [UserModule::class]) @UserScope interface UserObjectGraph @Component(modules = [ApplicationModule::class]) @ApplicationScope interface ApplicationObjectGraph { fun userObjectGraphBuilder(): UserObjectGraph.Builder }

Slide 48

Slide 48 text

{ fun featureRetainedBuilder(): FeatureRetainedObjectGraph.Builder } @Subcomponent(modules = [UserModule::class]) @UserScope interface UserObjectGraph @Component(modules = [ApplicationModule::class]) @ApplicationScope interface ApplicationObjectGraph { fun userObjectGraphBuilder(): UserObjectGraph.Builder }

Slide 49

Slide 49 text

{ fun featureRetainedBuilder(): FeatureRetainedObjectGraph.Builder } @Subcomponent(modules = [UserModule::class]) @UserScope interface UserObjectGraph @ApplicationScope interface ApplicationObjectGraph { fun userObjectGraphBuilder(): UserObjectGraph.Builder } @Subcomponent(modules = [FeatureRetainedModule::class]) @RetainedScope interface FeatureRetainedObjectGraph

Slide 50

Slide 50 text

{ fun featureRetainedBuilder(): FeatureRetainedObjectGraph.Builder } @Subcomponent(modules = [UserModule::class]) @UserScope interface UserObjectGraph @ApplicationScope interface ApplicationObjectGraph { fun userObjectGraphBuilder(): UserObjectGraph.Builder } @Subcomponent(modules = [FeatureRetainedModule::class]) @RetainedScope interface FeatureRetainedObjectGraph { fun featureViewGraphBuilder(): FeatureViewObjectGraph.Builder }

Slide 51

Slide 51 text

{ fun featureRetainedBuilder(): FeatureRetainedObjectGraph.Builder } @UserScope interface UserObjectGraph @Subcomponent(modules = [FeatureRetainedModule::class]) @RetainedScope interface FeatureRetainedObjectGraph { fun featureViewGraphBuilder(): FeatureViewObjectGraph.Builder } @Subcomponent(modules = [FeatureViewModule::class]) @ViewScope interface FeatureViewObjectGraph

Slide 52

Slide 52 text

{ fun featureRetainedBuilder(): FeatureRetainedObjectGraph.Builder } @UserScope interface UserObjectGraph Components or ? Subcomponents @ApplicationScope @UserScope @RetainedScope @ViewScope

Slide 53

Slide 53 text

Components for the root graph for the nested graphs Subcomponents @ApplicationScope @UserScope @RetainedScope @ViewScope

Slide 54

Slide 54 text

Where do we place our graphs?

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

@ApplicationScope @UserScope @RetainedScope @ViewScope

Slide 57

Slide 57 text

Let’s compose a Tweet

Slide 58

Slide 58 text

:features:composer @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 59

Slide 59 text

:features:composer ComposerActivity ComposerViewModel ComposerViewDelegate @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 60

Slide 60 text

:features:composer ComposerActivity ComposerViewModel ComposerViewDelegate @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 61

Slide 61 text

:subsystems:tweets TweetRepository :features:composer @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 62

Slide 62 text

:subsystems:tweets TweetRepository :features:composer @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 63

Slide 63 text

:subsystems:tweets TweetRepository :features:composer @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 64

Slide 64 text

:subsystems:tweets TweetRepository :features:composer @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 65

Slide 65 text

:twitter:media ImageUploader :features:composer :subsystems:tweets @RetainedScope @ViewScope @UserScope @ApplicationScope

Slide 66

Slide 66 text

:twitter:media ImageUploader :features:composer :subsystems:tweets @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 67

Slide 67 text

:twitter:media ImageUploader :features:composer :subsystems:tweets @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 68

Slide 68 text

:twitter:media ImageUploader :features:composer :subsystems:tweets @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 69

Slide 69 text

:core:network OkHttpClient :features:composer :subsystems:tweets :twitter:media @RetainedScope @ViewScope @UserScope @ApplicationScope

Slide 70

Slide 70 text

:core:network OkHttpClient :features:composer :subsystems:tweets :twitter:media @RetainedScope @ViewScope @ApplicationScope @UserScope

Slide 71

Slide 71 text

:core:network :features:composer :subsystems:tweets :twitter:media @RetainedScope @ViewScope @ApplicationScope @UserScope ComposerNotifHandler

Slide 72

Slide 72 text

:core:network :features:composer :subsystems:tweets :twitter:media @RetainedScope @ViewScope @ApplicationScope @UserScope ComposerNotifHandler

Slide 73

Slide 73 text

:core:network :features:composer :subsystems:tweets :twitter:media @RetainedScope @ViewScope @ApplicationScope @UserScope M M M M M M

Slide 74

Slide 74 text

:core:network :features:composer :subsystems:tweets :twitter:media @RetainedScope @ViewScope @ApplicationScope @UserScope M M M M M M

Slide 75

Slide 75 text

Ideal Location

Slide 76

Slide 76 text

App wide graphs definitions belong in the top gradle module. Ideal Location

Slide 77

Slide 77 text

Graphs can be large!

Slide 78

Slide 78 text

@Component @ApplicationScope interface ApplicationObjectGraph } fun composerLauncher(): ComposerLauncher fun composerNotificationHandler(): ComposerNotifHandler { ]) (modules = [ComposerAppModule::class

Slide 79

Slide 79 text

@Component @ApplicationScope interface ApplicationObjectGraph } fun composerLauncher(): ComposerLauncher fun composerNotificationHandler(): ComposerNotifHandler fun tweetsRepository(): TweetsRepository fun tweetsDataSource(): TweetsDataSource fun imageLoader(): ImageLoader { , TweetRepositoryAppModule::cl (modules = [ComposerAppModule::class

Slide 80

Slide 80 text

fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService @Component @ApplicationScope interface ApplicationObjectGraph fun composerLauncher(): ComposerLauncher fun composerNotificationHandler(): ComposerNotifHandler fun tweetsRepository(): TweetsRepository fun tweetsDataSource(): TweetsDataSource fun imageLoader(): ImageLoader { fun loginController(): LoginController fun loginAssistController(): LoginAssistController fun appEventTracker(): AppEventTracker fun installationReferrer(): InstallationReferrer fun jobManager(): JobManager fun playServicesUtil(): PlayServicesUtil fun httpRequestController(): HttpRequestController fun registrableHeadsetPlugReceiver(): RegistrableHeadsetPlugReceiver , TweetRepositoryAppModule::cl (modules = [ComposerAppModule::class

Slide 81

Slide 81 text

fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService @Component @ApplicationScope interface ApplicationObjectGraph fun composerLauncher(): ComposerLauncher fun composerNotificationHandler(): ComposerNotifHandler fun tweetsRepository(): TweetsRepository fun tweetsDataSource(): TweetsDataSource fun imageLoader(): ImageLoader { fun loginController(): LoginController fun loginAssistController(): LoginAssistController fun appEventTracker(): AppEventTracker fun installationReferrer(): InstallationReferrer fun jobManager(): JobManager fun playServicesUtil(): PlayServicesUtil fun httpRequestController(): HttpRequestController fun registrableHeadsetPlugReceiver(): RegistrableHeadsetPlugReceiver , TweetRepositoryAppModule::cl (modules = [ComposerAppModule::class

Slide 82

Slide 82 text

fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService @Component @ApplicationScope interface ApplicationObjectGraph fun composerLauncher(): ComposerLauncher fun composerNotificationHandler(): ComposerNotifHandler fun tweetsRepository(): TweetsRepository fun tweetsDataSource(): TweetsDataSource fun imageLoader(): ImageLoader { fun loginController(): LoginController fun loginAssistController(): LoginAssistController fun appEventTracker(): AppEventTracker fun installationReferrer(): InstallationReferrer fun jobManager(): JobManager fun playServicesUtil(): PlayServicesUtil fun httpRequestController(): HttpRequestController fun registrableHeadsetPlugReceiver(): RegistrableHeadsetPlugReceiver , TweetRepositoryAppModule::cl (modules = [ComposerAppModule::class

Slide 83

Slide 83 text

Subgraphs

Slide 84

Slide 84 text

Subgraphs Slices of an Object Graph

Slide 85

Slide 85 text

@Component @ApplicationScope interface ApplicationObjectGraph { } fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService fun composerLauncher(): ComposerLauncher fun composerNotificationHandler(): ComposerNotifHandler fun tweetsRepository(): TweetsRepository fun tweetsDataSource(): TweetsDataSource fun imageLoader(): ImageLoader :app:twitter

Slide 86

Slide 86 text

: @Component @ApplicationScope interface ApplicationObjectGraph { } fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService fun composerLauncher(): ComposerLauncher fun composerNotificationHandler(): ComposerNotifHandler fun tweetsRepository(): TweetsRepository fun tweetsDataSource(): TweetsDataSource fun imageLoader(): ImageLoader ComposerAppSubgraph interface ComposerAppSubgraph { } :app:twitter :features:composer

Slide 87

Slide 87 text

: @Component @ApplicationScope interface ApplicationObjectGraph { } fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService fun tweetsRepository(): TweetsRepository fun tweetsDataSource(): TweetsDataSource fun imageLoader(): ImageLoader ComposerAppSubgraph :app:twitter

Slide 88

Slide 88 text

@Component @ApplicationScope interface ApplicationObjectGraph , : { } fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService fun tweetsRepository(): TweetsRepository fun tweetsDataSource(): TweetsDataSource fun imageLoader(): ImageLoader ComposerAppSubgraph interface TweetRepositoryAppSubgraph { } TweetRepositoryAppSubgraph :app:twitter :subsystems:tweets

Slide 89

Slide 89 text

@Component @ApplicationScope interface ApplicationObjectGraph , : { } fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService fun imageLoader(): ImageLoader ComposerAppSubgraph TweetRepositoryAppSubgraph :app:twitter

Slide 90

Slide 90 text

@Component @ApplicationScope interface ApplicationObjectGraph , : { } fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService fun imageLoader(): ImageLoader ComposerAppSubgraph TweetRepositoryAppSubgraph interface MediaAppSubgraph { } MediaAppSubgraph , :app:twitter :twitter:media

Slide 91

Slide 91 text

@Component @ApplicationScope interface ApplicationObjectGraph , : { } fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService ComposerAppSubgraph TweetRepositoryAppSubgraph MediaAppSubgraph , :app:twitter

Slide 92

Slide 92 text

@Component @ApplicationScope interface ApplicationObjectGraph , : fun okHttpClient(): OkHttpClient fun twitterApiService(): TwitterApiService ComposerAppSubgraph TweetRepositoryAppSubgraph MediaAppSubgraph , interface CoreNetworkAppSubgraph { } CoreNetworkAppSubgraph , :core:network :app:twitter

Slide 93

Slide 93 text

@Component @ApplicationScope interface ApplicationObjectGraph , : ComposerAppSubgraph TweetRepositoryAppSubgraph MediaAppSubgraph , CoreNetworkAppSubgraph , :app:twitter ApplicationObjectGraph

Slide 94

Slide 94 text

:core:network :features:composer :subsystems:tweets :twitter:media TweetRepositoryAppSubgraph MediaAppSubgraph CoreNetworkAppSubgraph ApplicationObjectGraph ComposerAppSubgraph

Slide 95

Slide 95 text

:core:network :features:composer :subsystems:tweets :twitter:media TweetRepositoryAppSubgraph MediaAppSubgraph CoreNetworkAppSubgraph ApplicationObjectGraph ComposerAppSubgraph

Slide 96

Slide 96 text

ApplicationObjectGraph UserObjectGraph Multiple Retained and ViewObjectGraphs

Slide 97

Slide 97 text

ApplicationObjectGraph UserObjectGraph Multiple Retained and ViewObjectGraphs

Slide 98

Slide 98 text

Big monolithic graphs

Slide 99

Slide 99 text

Big monolithic graphs Scatter the definitions of the object graphs into subgraphs that can live in the gradle modules where the bindings are actually defined.

Slide 100

Slide 100 text

How to instantiate your graphs?

Slide 101

Slide 101 text

component = .application(this) .build() class MyApplication : Application { private lateinit var component: ApplicationObjectGraph override fun onCreate() { } super.onCreate() } DaggerApplicationObjectGraph.builder()

Slide 102

Slide 102 text

component = .application(this) .build() class MyApplication : Application { private lateinit var component: ApplicationObjectGraph override fun onCreate() { } super.onCreate() } DaggerApplicationObjectGraph.builder()

Slide 103

Slide 103 text

Dagger.getObjectGraphBuilder() component = .application(this) .build() class MyApplication : Application { private lateinit var component: ApplicationObjectGraph override fun onCreate() { } super.onCreate() }

Slide 104

Slide 104 text

fun getObjectGraphBuilder(builderClass: Class): B } } class Dagger { companion object { inline fun getObjectGraphBuilder(): B = getObjectGraphBuilder(B::class.java) } {

Slide 105

Slide 105 text

fun getObjectGraphBuilder(builderClass: Class): B } } class Dagger { companion object { inline fun getObjectGraphBuilder(): B = getObjectGraphBuilder(B::class.java) } {

Slide 106

Slide 106 text

if (!builderClass.isAnnotationPresent(Component.Builder::class.java)) { throw IllegalArgumentException("Not a component builder class: $builderClass") } val componentClass = builderClass.enclosingClass ?: throw IllegalStateException("Component builder is not enclosed in a component.") val generatedDaggerClassName = getDaggerComponentImplementationClassName(componentClass) val generatedObjectGraphClass = ReflectionUtils.loadClass(generatedDaggerClassName) ?: throw IllegalStateException("Generated Dagger class can't be found: $generatedDagge val builderMethod = ReflectionUtils.getMethod(generatedObjectGraphClass, "builder") ?: throw IllegalStateException("The Dagger component generated class does not contain return ReflectionUtils.invokeMethod(builderMethod, null) ?: throw IllegalStateException("The builder() method returned null.") fun getObjectGraphBuilder(builderClass: Class): B } } class Dagger { companion object { inline fun getObjectGraphBuilder(): B = getObjectGraphBuilder(B::class.java) } {

Slide 107

Slide 107 text

if (!builderClass.isAnnotationPresent(Component.Builder::class.java)) { throw IllegalArgumentException("Not a component builder class: $builderClass") } val componentClass = builderClass.enclosingClass ?: throw IllegalStateException("Component builder is not enclosed in a component.") val generatedDaggerClassName = getDaggerComponentImplementationClassName(componentClass) val generatedObjectGraphClass = ReflectionUtils.loadClass(generatedDaggerClassName) ?: throw IllegalStateException("Generated Dagger class can't be found: $generatedDagge val builderMethod = ReflectionUtils.getMethod(generatedObjectGraphClass, "builder") ?: throw IllegalStateException("The Dagger component generated class does not contain return ReflectionUtils.invokeMethod(builderMethod, null) ?: throw IllegalStateException("The builder() method returned null.") fun getObjectGraphBuilder(builderClass: Class): B } } class Dagger { companion object { inline fun getObjectGraphBuilder(): B = getObjectGraphBuilder(B::class.java) } TL;DR Use reflection once to find and instantiate the graph’s Component.Builder {

Slide 108

Slide 108 text

if (!builderClass.isAnnotationPresent(Component.Builder::class.java)) { throw IllegalArgumentException("Not a component builder class: $builderClass") } val componentClass = builderClass.enclosingClass ?: throw IllegalStateException("Component builder is not enclosed in a component.") val generatedDaggerClassName = getDaggerComponentImplementationClassName(componentClass) val generatedObjectGraphClass = ReflectionUtils.loadClass(generatedDaggerClassName) ?: throw IllegalStateException("Generated Dagger class can't be found: $generatedDagge val builderMethod = ReflectionUtils.getMethod(generatedObjectGraphClass, "builder") ?: throw IllegalStateException("The Dagger component generated class does not contain return ReflectionUtils.invokeMethod(builderMethod, null) ?: throw IllegalStateException("The builder() method returned null.") fun getObjectGraphBuilder(builderClass: Class): B } } class Dagger { companion object { inline fun getObjectGraphBuilder(): B = getObjectGraphBuilder(B::class.java) } Don’t worry! We will share the code in GitHub! {

Slide 109

Slide 109 text

Referencing generated code DaggerApplicationObjectGraph.builder()

Slide 110

Slide 110 text

Referencing generated code Use reflection once to grab the root object graph builder. Dagger.getObjectGraphBuilder() DaggerApplicationObjectGraph.builder()

Slide 111

Slide 111 text

How to access graphs at the top from intermediate layers?

Slide 112

Slide 112 text

ApplicationObjectGraph

Slide 113

Slide 113 text

ApplicationObjectGraph :twitter:media ImageUploader L LegacyActivity

Slide 114

Slide 114 text

:twitter:media L ImageUploader LegacyActivity ApplicationObjectGraph

Slide 115

Slide 115 text

:twitter:media MediaAppSubgraph L LegacyActivity ApplicationObjectGraph

Slide 116

Slide 116 text

object ApplicationObjectGraphProvider { var graphProvider: Any? }

Slide 117

Slide 117 text

component = Dagger.getObjectGraphBuilder() .application(this) .build() class MyApplication : Application { private lateinit var component: ApplicationObjectGraph override fun onCreate() { } super.onCreate() }

Slide 118

Slide 118 text

component = Dagger.getObjectGraphBuilder() .application(this) .build() class MyApplication : Application { private lateinit var component: ApplicationObjectGraph override fun onCreate() { } super.onCreate() }

Slide 119

Slide 119 text

super.onCreate() } ApplicationObjectGraphProvider.graphProvider = Dagger.getObjectGraphBuilder() .application(this) .build() class MyApplication : Application { override fun onCreate() { }

Slide 120

Slide 120 text

ApplicationObjectGraph ApplicationObjectGraphProvider

Slide 121

Slide 121 text

ApplicationObjectGraph ApplicationObjectGraphProvider :twitter:media L ImageUploader LegacyActivity MediaAppSubgraph

Slide 122

Slide 122 text

(ApplicationObjectGraphProvider.graphProvider as MediaAppSubgraph).imageUploader()

Slide 123

Slide 123 text

object ApplicationObjectGraphProvider { } fun getSubgraph(clazz: Class): T var graphProvider: Any?

Slide 124

Slide 124 text

object ApplicationObjectGraphProvider { } fun getSubgraph(clazz: Class): T var graphProvider: Any? { return clazz.cast(graphProvider) }

Slide 125

Slide 125 text

fun imageUploader(): ImageUploader interface MediaAppSubgraph { }

Slide 126

Slide 126 text

fun imageUploader(): ImageUploader interface MediaAppSubgraph { } companion object { fun get(): MediaAppSubgraph { return ApplicationObjectGraphProvider.graphProvider .getSubgraph(MediaAppSubgraph::class.java) }

Slide 127

Slide 127 text

val uploader = MediaAppSubgraph.get().imageUploader() fun imageUploader(): ImageUploader interface MediaAppSubgraph { } companion object { fun get(): MediaAppSubgraph { return ApplicationObjectGraphProvider.graphProvider .getSubgraph(MediaAppSubgraph::class.java) }

Slide 128

Slide 128 text

val uploader = MediaAppSubgraph.get().imageUploader()

Slide 129

Slide 129 text

Grabbing objects from Dagger w/o visibility MediaAppSubgraph L LegacyActivity ApplicationObjectGraph

Slide 130

Slide 130 text

Grabbing objects from Dagger w/o visibility Wrap your Application and User graphs in providers that can be statically obtained, and add a way to extract subgraphs from them. ApplicationObjectGraphProvider UserObjectGraphProvider MediaAppSubgraph L LegacyActivity

Slide 131

Slide 131 text

How would that work in testing?

Slide 132

Slide 132 text

object ApplicationObjectGraphProvider { } var graphProvider: Any? private val mapping: Map, Any> = mutableMapOf() fun overrideSubgraph(clazz: Class, subgraph: T) { mapping[clazz] = subgraph } fun getSubgraph(clazz: Class): T { if (mapping.containsKey(clazz)) return clazz.cast(mapping[clazz]) return clazz.cast(graphProvider) }

Slide 133

Slide 133 text

object ApplicationObjectGraphProvider { } var graphProvider: Any? private val mapping: Map, Any> = mutableMapOf() fun overrideSubgraph(clazz: Class, subgraph: T) { mapping[clazz] = subgraph } fun getSubgraph(clazz: Class): T { if (mapping.containsKey(clazz)) return clazz.cast(mapping[clazz]) return clazz.cast(graphProvider) }

Slide 134

Slide 134 text

How to decouple features?

Slide 135

Slide 135 text

How to decouple features? The app navigation case

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

ComposerActivity MainActivity DMActivity TweetDetailActivity C D M TD

Slide 138

Slide 138 text

ComposerActivity MainActivity DMActivity TweetDetailActivity C D M TD

Slide 139

Slide 139 text

ComposerActivity MainActivity DMActivity TweetDetailActivity C D M TD

Slide 140

Slide 140 text

ComposerActivity MainActivity DMActivity TweetDetailActivity C D M TD Circular Dependency!

Slide 141

Slide 141 text

ComposerActivity MainActivity TweetDetailActivity C D M TD DMActivity :subsystems:navigation

Slide 142

Slide 142 text

ComposerActivity MainActivity TweetDetailActivity C D M TD Intent().putExtra(…) DMActivity :subsystems:navigation C D M TD C D M TD Intent factories

Slide 143

Slide 143 text

ComposerActivity MainActivity TweetDetailActivity C D M TD DMActivity :subsystems:navigation C D M TD Intent factories ActivityArgs

Slide 144

Slide 144 text

ComposerActivity MainActivity TweetDetailActivity C D M TD DMActivity :subsystems:navigation TD M D C ActivityArgs

Slide 145

Slide 145 text

ComposerActivity MainActivity TweetDetailActivity C D M TD DMActivity :subsystems:navigation TD M D C ActivityArgs

Slide 146

Slide 146 text

interface ActivityArgs { fun toIntent(context: Context, clazz: Class): Intent }

Slide 147

Slide 147 text

override fun toIntent(context: Context, clazz: Class): { .apply { putExtra("user_id", uid) putExtra("text", text) } } interface ActivityArgs { fun toIntent(context: Context, clazz: Class): Intent } data class ComposerArgs(val uid: Long, val text: String): ActivityArgs { } return Intent(context, clazz)

Slide 148

Slide 148 text

override fun toIntent(context: Context, clazz: Class): { .apply { putExtra("user_id", uid) putExtra("text", text) } } interface ActivityArgs { fun toIntent(context: Context, clazz: Class): Intent } data class ComposerArgs(val uid: Long, val text: String): ActivityArgs { } return Intent(context, clazz)

Slide 149

Slide 149 text

Service Discovery

Slide 150

Slide 150 text

Service Discovery Multibindings

Slide 151

Slide 151 text

C D TD ComposerArgs TweetDetailArgs DMArgs :features:tweetdetail :features:dm :features:composer @Multibinds

Slide 152

Slide 152 text

C D TD ComposerArgs TweetDetailArgs DMArgs :features:tweetdetail :features:dm :features:composer @Multibinds

Slide 153

Slide 153 text

C D TD ComposerArgs TweetDetailArgs DMArgs :features:tweetdetail :features:dm :features:composer @Multibinds

Slide 154

Slide 154 text

C D TD ComposerArgs TweetDetailArgs DMArgs :features:tweetdetail :features:dm :features:composer Map, Class> C D TD ComposerActivity DMActivity TweetDetailActivity

Slide 155

Slide 155 text

@Module object ComposerModule { @Provides @ApplicationScope fun provideActivityClass: Class { } } :features:composer @ActivityArgsKey(ComposerArgs::class) @IntoMap return ComposerActivity::class.java

Slide 156

Slide 156 text

@Module object ComposerModule { @Provides @ApplicationScope fun provideActivityClass: Class { } } :features:composer @ActivityArgsKey(ComposerArgs::class) @IntoMap return ComposerActivity::class.java

Slide 157

Slide 157 text

@Module object ComposerModule { @Provides @ApplicationScope fun provideActivityClass: Class { } } :features:composer @ActivityArgsKey(ComposerArgs::class) @IntoMap return ComposerActivity::class.java

Slide 158

Slide 158 text

@Module object ComposerModule { @Provides @ApplicationScope fun provideActivityClass: Class { } } :features:composer @ActivityArgsKey(ComposerArgs::class) @IntoMap return ComposerActivity::class.java

Slide 159

Slide 159 text

@ApplicationScope class ActivityStarter @Inject constructor( ) private val activityMap: Map, Class> { fun startActivity(context: Context, args: ActivityArgs) { } } val intent = args.toIntent(context, activityMap[args.javaClass]) context.startActivity(intent)

Slide 160

Slide 160 text

@ApplicationScope class ActivityStarter @Inject constructor( ) { fun startActivity(context: Context, args: ActivityArgs) { } } val intent = args.toIntent(context, activityMap[args.javaClass]) context.startActivity(intent) private val activityMap: Map, Class>

Slide 161

Slide 161 text

{ fun startActivity(context: Context, args: ActivityArgs) { } } @ApplicationScope class ActivityStarter @Inject constructor( ) val intent = args.toIntent(context, activityMap[args.javaClass]) context.startActivity(intent) private val activityMap: Map, Class>

Slide 162

Slide 162 text

{ fun startActivity(context: Context, args: ActivityArgs) { } } @ApplicationScope class ActivityStarter @Inject constructor( ) val intent = args.toIntent(context, activityMap[args.javaClass]) context.startActivity(intent) private val activityMap: Map, Class>

Slide 163

Slide 163 text

C TD Start an Activity we can’t see at compile time

Slide 164

Slide 164 text

C D TD C D TD Start an Activity we can’t see at compile time At compile time you might not see everything, but at runtime it is all there. Let’s use Multibindings to aggregate references at compile time and resolve them at runtime.

Slide 165

Slide 165 text

No content

Slide 166

Slide 166 text

Initializers

Slide 167

Slide 167 text

Notification Handlers

Slide 168

Slide 168 text

Deeplink Handlers

Slide 169

Slide 169 text

No content

Slide 170

Slide 170 text

No content

Slide 171

Slide 171 text

Subgraphs

Slide 172

Slide 172 text

Subgraphs are not atomic units

Slide 173

Slide 173 text

S ApplicationObjectGraph S S S S S S S

Slide 174

Slide 174 text

S ApplicationObjectGraph M S M S M S M S M S M S M S M

Slide 175

Slide 175 text

S ApplicationObjectGraph M S M S M S M S S M S M S M @Component(modules = [ ComposerAppModule::class, TweetDetailAppModule::class, HomeAppModule::class, ExploreAppModule::class, CoreAppModule::class, CoreNetworkAppModule::class, MediaAppModule::class, VideoAppModule::class, NavigationAppModule::class, LoggingAppModule::class, TimelineAppModule::class, … and on and on and on ]) @ApplicationScope interface ApplicationObjectGraph: M

Slide 176

Slide 176 text

ApplicationObjectGraph M M M M M M M M TimelineAppModule::class, … and on and on and on ]) @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, ComposerAppSubgraph, TweetDetailAppSubgraph, HomeAppSubgraph, ExploreAppSubgraph, CoreAppSubgraph, CoreNetworkAppSubgraph, MediaAppSubgraph, VideoAppSubgraph, NavigationAppSubgraph, … and on and on and on S S S S S S S S

Slide 177

Slide 177 text

S M

Slide 178

Slide 178 text

@Component(modules = [ … and on and on and on ]) @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, TweetDetailAppSubgraph, @Component(modules = [ … and on and on and on ]) @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, MapSubgraph, S M

Slide 179

Slide 179 text

@Component(modules = [ BroadcastAppModule::class, … and on and on and on ]) @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, BroadcastAppSubgraph, TweetDetailAppSubgraph, @Component(modules = [ BroadcastAppModule::class, … and on and on and on ]) @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, BroadcastAppSubgraph, MapSubgraph, S M

Slide 180

Slide 180 text

No content

Slide 181

Slide 181 text

{ fun broadcastController(): BroadcastController } @Module { @Binds @ApplicationScope fun bindBcastController(impl: BroadcastControllerImpl): BroadcastController } object BroadcastAppModule interface BroadcastAppSubgraph

Slide 182

Slide 182 text

interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph @Module { @Binds @ApplicationScope fun bindBcastController(impl: BroadcastControllerImpl): BroadcastController } object BroadcastAppModule interface BroadcastAppSubgraph

Slide 183

Slide 183 text

@ApplicationScope interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph @Module { @Binds @ApplicationScope fun bindBcastController(impl: BroadcastControllerImpl): BroadcastController } object BroadcastAppModule interface BroadcastAppSubgraph

Slide 184

Slide 184 text

@ApplicationScope interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph @Subgraph(modules = [BroadcastAppModule::class]) @Module { @Binds @ApplicationScope fun bindBcastController(impl: BroadcastControllerImpl): BroadcastController } object BroadcastAppModule interface BroadcastAppSubgraph

Slide 185

Slide 185 text

@ApplicationScope interface ApplicationObjectGraph : BroadcastAppSubgraph, InitializationSubgraph, ... (modules = [BroadcastAppModule::class, ...]) @Component

Slide 186

Slide 186 text

@ApplicationScope interface ApplicationObjectGraph : BroadcastAppSubgraph, InitializationSubgraph, ... (modules = [BroadcastAppModule::class, ...]) @Component

Slide 187

Slide 187 text

@ApplicationScope interface ApplicationObjectGraph : BroadcastAppSubgraph, InitializationSubgraph, ... @ObjectGraph

Slide 188

Slide 188 text

@UserScope interface UserObjectGraph : UserManagerSubgraph, TweetRepository, ... (modules = [UserManagerModule::class, ...]) @ApplicationScope interface ApplicationObjectGraph : BroadcastAppSubgraph, InitializationSubgraph, ... @ObjectGraph @Subcomponent

Slide 189

Slide 189 text

@UserScope interface UserObjectGraph : UserManagerSubgraph, TweetRepository, ... (modules = [UserManagerModule::class, ...]) @ApplicationScope interface ApplicationObjectGraph : BroadcastAppSubgraph, InitializationSubgraph, ... @ObjectGraph @Subcomponent

Slide 190

Slide 190 text

@UserScope interface UserObjectGraph : UserManagerSubgraph, TweetRepository, ... @ApplicationScope interface ApplicationObjectGraph : BroadcastAppSubgraph, InitializationSubgraph, ... @ObjectGraph @ObjectGraph

Slide 191

Slide 191 text

@ObjectGraph @Subgraph

Slide 192

Slide 192 text

Annotation Processor @ObjectGraph @Subgraph @Component @Subcomponent

Slide 193

Slide 193 text

@ObjectGraph @Subgraph Annotation Processor @Component @Subcomponent

Slide 194

Slide 194 text

Annotation Processor @ObjectGraph @Subgraph @Component @Subcomponent

Slide 195

Slide 195 text

Fully Dagger Compatible Annotation Processor

Slide 196

Slide 196 text

Subgraphs

Slide 197

Slide 197 text

Subgraphs are not encapsulated

Slide 198

Slide 198 text

S S S S S S

Slide 199

Slide 199 text

S S S S S S

Slide 200

Slide 200 text

S S S S S S

Slide 201

Slide 201 text

S

Slide 202

Slide 202 text

S @ApplicationScope interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph(modules = [BroadcastAppModule::class])

Slide 203

Slide 203 text

@Subgraph( subgraphDependencies = [ CoreNetworkSubgraph::class, VideoPlayerSubgraph::class ]) S @ApplicationScope interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph @Subgraph(modules = [BroadcastAppModule::class],

Slide 204

Slide 204 text

S ApplicationObjectGraph S S S S S S S

Slide 205

Slide 205 text

S ApplicationObjectGraph S S S S S S S

Slide 206

Slide 206 text

S ApplicationObjectGraph S S S S S S S

Slide 207

Slide 207 text

Subgraphs

Slide 208

Slide 208 text

Subgraphs are not self documenting

Slide 209

Slide 209 text

Subgraphs are not self documenting

Slide 210

Slide 210 text

@Subgraph( subgraphDependencies = [ CoreNetworkSubgraph::class, VideoPlayerSubgraph::class ]) @ApplicationScope interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph @Subgraph(modules = [BroadcastAppModule::class],

Slide 211

Slide 211 text

@Subgraph( subgraphDependencies = [ CoreNetworkSubgraph::class, VideoPlayerSubgraph::class ]) @ApplicationScope interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph @Subgraph(modules = [BroadcastAppModule::class],

Slide 212

Slide 212 text

@Subgraph( subgraphDependencies = [ CoreNetworkSubgraph::class, VideoPlayerSubgraph::class ]) @ApplicationScope interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph @Subgraph(modules = [BroadcastAppModule::class],

Slide 213

Slide 213 text

@Subgraph( subgraphDependencies = [ CoreNetworkSubgraph::class, VideoPlayerSubgraph::class ]) @ApplicationScope interface BroadcastAppSubgraph { fun broadcastController(): BroadcastController } @Subgraph @Subgraph(modules = [BroadcastAppModule::class],

Slide 214

Slide 214 text

Graphs

Slide 215

Slide 215 text

Graphs are hard to maintain

Slide 216

Slide 216 text

@ObjectGraph @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, BroadcastAppSubgraph, TweetDetailAppSubgraph, HomeAppSubgraph, ExploreAppSubgraph, CoreAppSubgraph, CoreNetworkAppSubgraph, MediaAppSubgraph, VideoPlayerSubgraph, NavigationAppSubgraph, TweetUploadAppSubgraph, PerformanceMetricsAppSubgraph, DMAppSubgraph, JSONModelsAppSubgraph, DynamicModulesAppSubgraph, NotificationsAppSubgraph,

Slide 217

Slide 217 text

@ObjectGraph @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, BroadcastAppSubgraph, TweetDetailAppSubgraph, HomeAppSubgraph, ExploreAppSubgraph, CoreAppSubgraph, CoreNetworkAppSubgraph, MediaAppSubgraph, VideoPlayerSubgraph, NavigationAppSubgraph, TweetUploadAppSubgraph, PerformanceMetricsAppSubgraph, DMAppSubgraph, JSONModelsAppSubgraph, DynamicModulesAppSubgraph, NotificationsAppSubgraph, This is actually insane

Slide 218

Slide 218 text

Annotation Processor @ObjectGraph @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, BroadcastAppSubgraph, TweetDetailAppSubgraph, HomeAppSubgraph, ExploreAppSubgraph, CoreAppSubgraph, CoreNetworkAppSubgraph, MediaAppSubgraph, VideoPlayerSubgraph, NavigationAppSubgraph, TweetUploadAppSubgraph, PerformanceMetricsAppSubgraph, DMAppSubgraph, JSONModelsAppSubgraph, DynamicModulesAppSubgraph, NotificationsAppSubgraph,

Slide 219

Slide 219 text

Annotation Processor @ObjectGraph @ApplicationScope interface ApplicationObjectGraph: InitializationSubgraph, BroadcastAppSubgraph, TweetDetailAppSubgraph, HomeAppSubgraph, ExploreAppSubgraph, CoreAppSubgraph, CoreNetworkAppSubgraph, MediaAppSubgraph, VideoPlayerSubgraph, NavigationAppSubgraph, TweetUploadAppSubgraph, PerformanceMetricsAppSubgraph, DMAppSubgraph, JSONModelsAppSubgraph, DynamicModulesAppSubgraph, NotificationsAppSubgraph,

Slide 220

Slide 220 text

@ObjectGraph @ApplicationScope interface ApplicationObjectGraph Annotation Processor Automatic Discovery

Slide 221

Slide 221 text

S ApplicationObjectGraph S S S S S S S

Slide 222

Slide 222 text

S ApplicationObjectGraph S S S S S S S

Slide 223

Slide 223 text

Subgraphs

Slide 224

Slide 224 text

Subgraphs are not reusable

Slide 225

Slide 225 text

CoreNetworkSubgraph OkHttpClient Cache Set CoreNetworkSubgraph :core:network

Slide 226

Slide 226 text

PeriscopeNetworkSubgraph :periscope:network CoreNetworkSubgraph OkHttpClient Cache Set CoreNetworkSubgraph :core:network

Slide 227

Slide 227 text

CoreNetworkSubgraph OkHttpClient Cache Set CoreNetworkSubgraph :core:network PeriscopeNetworkSubgraph :periscope:network

Slide 228

Slide 228 text

open class CoreNetworkSubgraph { fun okHttpClient(): OkHttpClient = … fun interceptors(): Set = … } fun cache(): Cache = Cache()

Slide 229

Slide 229 text

class PeriscopeNetworkSubgraph : CoreNetworkSubgraph { override fun cache(): Cache = PeriscopeCachePolicy() } open fun cache(): Cache = Cache() open class CoreNetworkSubgraph { fun okHttpClient(): OkHttpClient = … fun interceptors(): Set = … }

Slide 230

Slide 230 text

@Subgraph(module = [CoreNetworkModule::class]) @Open interface CoreNetworkSubgraph { fun okHttpClient(): OkHttpClient @Open fun cache(): Cache fun interceptors(): Set }

Slide 231

Slide 231 text

@Subgraph(module = [CoreNetworkModule::class]) @Open interface CoreNetworkSubgraph { fun okHttpClient(): OkHttpClient @Open fun cache(): Cache fun interceptors(): Set } @Subgraph interface PeriscopeNetworkSubgraph : CoreNetworkSubgraph { @Override @Binds fun bindCache(cache: PeriscopeCache): Cache }

Slide 232

Slide 232 text

Subgraphs

Slide 233

Slide 233 text

Subgraphs don’t support inversion of dependencies

Slide 234

Slide 234 text

interface DatabaseSubgraph { fun migrationManager(): MigrationManager } fun schema(): Schema @Subgraph

Slide 235

Slide 235 text

interface DatabaseSubgraph { fun migrationManager(): MigrationManager } fun schema(): Schema @Subgraph @Abstract @Abstract

Slide 236

Slide 236 text

interface DatabaseSubgraph { fun migrationManager(): MigrationManager } fun schema(): Schema @Subgraph @Abstract @Abstract @Subgraph interface TwitterDatabaseSubgraph : DatabaseSubgraph { @Binds fun schema(bound: TwitterSchema): Schema }

Slide 237

Slide 237 text

And more!

Slide 238

Slide 238 text

And more!

Slide 239

Slide 239 text

And more!

Slide 240

Slide 240 text

And more!

Slide 241

Slide 241 text

Annotation Processor

Slide 242

Slide 242 text

Scythe Scythe

Slide 243

Slide 243 text

Scythe OOP concepts for DI IDE support Better build times Binding overrides Modular scalability Self documentation Reusability Automatic discovery Completeness validation Factory bindings Assisted Injection Compatible with Dagger

Slide 244

Slide 244 text

Scythe Open-source launch for everybody in 2020

Slide 245

Slide 245 text

@CesarDielo César @mrmans0n Nacho

Slide 246

Slide 246 text

@CesarDielo César @mrmans0n Nacho Thank you! More questions? Follow ups from this talk? Hit us up on Twitter!