Slide 1

Slide 1 text

Effective DI with Multi-Modular Architecture

Slide 2

Slide 2 text

Adit Lal GoPay $6.3B @aditlal We have 18+ products from food tech to fin-tech to hyper local delivery and massage services Ride Sharing 16.5m KM

Slide 3

Slide 3 text

app core feature 2 feature 1 Monolith Expensive Object

Slide 4

Slide 4 text

What could possibly go wrong?

Slide 5

Slide 5 text

Solve DI with Multi-Modular codebase

Slide 6

Slide 6 text

Modularise?

Slide 7

Slide 7 text

app core Feature B Feature A Feature modules Expensive Object

Slide 8

Slide 8 text

• Organise and decouple independent pieces of functionality • Improve project build times • Define clear ownership boundaries between different teams Thoughts on modularisation

Slide 9

Slide 9 text

Feature module : • Owned by single team ? Set of team members • Encapsulates single feature • Single entry point • Smaller is better • Cannot depend on other feature modules Thoughts on modularisation

Slide 10

Slide 10 text

App module : • No feature-specific code • No infrastructure-specific code. • Creates the dagger component Thoughts on modularisation

Slide 11

Slide 11 text

Ownership? core auth common Feature n-1 Feature n+2 Feature n-2 Feature z Feature n Feature n+1 app … … … … …

Slide 12

Slide 12 text

Ownership? core auth common Feature n-1 Feature n+2 Feature n-2 Feature z Feature n Feature n+1 app … … … … …

Slide 13

Slide 13 text

Inversion of control Dependency Inversion Dependency Injection IoC Principle

Slide 14

Slide 14 text

• Declare Components that can be reused. • High level modules should not depend on low level modules • Build abstractions so any implementation can be easily changed with new one. • Dependencies can be injected into components. IoC Principle

Slide 15

Slide 15 text

Should I do Modularise to use Dagger?

Slide 16

Slide 16 text

Don’t do it for Dagger , do it for yourself

Slide 17

Slide 17 text

:feature1 F1SubComponent F1Module :feature2 F2SubComponent F2Module :core CoreComponent CoreModule Expensive Object :app MainComponent MainModule

Slide 18

Slide 18 text

:app MainComponent MainModule :feature1 F1Component F1Module :feature2 F2Component F2Module :core CoreComponent CoreModule Expensive Object

Slide 19

Slide 19 text

• We should prefer to be explicit with the module dependencies
 • We don’t want CoreComponent to know about FeatureComponent
 • It can’t check if the graph is correct. SubComponent goes away! It needs the parent = slower compilation


Slide 20

Slide 20 text

Core @Module class CoreModule { @Provides @Singleton fun provideExpObj(): ExpensiveObject = ExpensiveObject() }

Slide 21

Slide 21 text

@Component(modules = [CoreModule::class]) @Singleton interface CoreComponent { @Component.Builder interface Builder { fun build(): CoreComponent } fun provideExpensiveObject(): ExpensiveObject } Core

Slide 22

Slide 22 text

class OurMainApplication : Application() { private val coreComponent: CoreComponent by lazy { DaggerCoreComponent .builder() .build() } companion object { @JvmStatic fun coreComponent(context: Context) = (context.applicationContext as OurMainApplication).coreComponent } }

Slide 23

Slide 23 text

class OurMainApplication : Application() { private val coreComponent: CoreComponent by lazy { DaggerCoreComponent .builder() .build() } companion object { @JvmStatic fun coreComponent(context: Context) = (context.applicationContext as OurMainApplication).coreComponent } }

Slide 24

Slide 24 text

class OurMainApplication : Application() { private val coreComponent: CoreComponent by lazy { DaggerCoreComponent .builder() .build() } companion object { @JvmStatic fun coreComponent(context: Context) = (context.applicationContext as OurMainApplication).coreComponent } }

Slide 25

Slide 25 text

fun Activity.coreComponent() = OurMainApplication.coreComponent(this) Extension function

Slide 26

Slide 26 text

@Module class Feature1Module { @Provides fun provideString() = "test" } Feature 1

Slide 27

Slide 27 text

@Component(modules = [Feature1Module::class], dependencies = [CoreComponent::class]) interface Feature1Component { @Component.Builder interface Builder { fun coreComponent(coreComponent: CoreComponent): Builder } fun inject(activity: OtherActivity) } Feature 1

Slide 28

Slide 28 text

DaggerFeature1Component .builder() .coreComponent(this.coreComponent) .build() .inject(this) Feature 1

Slide 29

Slide 29 text

@Module class Feature2Module { @Provides fun provideInt() = 1 } Feature 2

Slide 30

Slide 30 text

@Component(modules = [Feature2Module::class], dependencies = [CoreComponent::class]) interface Feature2Component { @Component.Builder interface Builder { fun coreComponent(coreComponent: CoreComponent): Builder } fun inject(activity: MainActivity) } Feature 2

Slide 31

Slide 31 text

When dealing with component dependencies we must follow 2 simple rules: • An un-scoped component cannot depend on scoped components. • A scoped component cannot depend on a component with the same scope.

Slide 32

Slide 32 text

@Scope @Retention annotation class FeatureScope

Slide 33

Slide 33 text

@Component(modules = [Feature1Module::class], dependencies = [CoreComponent::class]) @FeatureScope interface Feature1Component { @Component.Builder interface Builder { fun coreComponent(coreComponent: CoreComponent): Builder } fun inject(activity: OtherActivity) }

Slide 34

Slide 34 text

@Component(modules = [Feature2Module::class], dependencies = [CoreComponent::class]) @FeatureScope interface Feature2Component { @Component.Builder interface Builder { fun coreComponent(coreComponent: CoreComponent): Builder } fun inject(activity: MainActivity) }

Slide 35

Slide 35 text

Component.dependencies • We can choose what we expose from each component • more verbosity • we can get dependencies from multiple components faster compilation

Slide 36

Slide 36 text

Let’s get to it

Slide 37

Slide 37 text

interface CoreComponentProvider { fun provideCoreComponent(): CoreComponent }

Slide 38

Slide 38 text

object CoreInjectHelper { fun provideCoreComponent(applicationContext: Context): CoreComponent{ return if (applicationContext is CoreComponentProvider) { (applicationContext as CoreComponentProvider).provideCoreComponent() } else { throw IllegalStateException( "The context passed does not implement CoreComponentProvider" ) } } }

Slide 39

Slide 39 text

object CoreInjectHelper { fun provideCoreComponent(applicationContext: Context): CoreComponent{ return if (applicationContext is CoreComponentProvider) { (applicationContext as CoreComponentProvider).provideCoreComponent() } else { throw IllegalStateException( "The context passed does not implement CoreComponentProvider" ) } } }

Slide 40

Slide 40 text

object CoreInjectHelper { fun provideCoreComponent(applicationContext: Context): CoreComponent{ return if (applicationContext is CoreComponentProvider) { (applicationContext as CoreComponentProvider).provideCoreComponent() } else { throw IllegalStateException( "The context passed does not implement CoreComponentProvider" ) } } }

Slide 41

Slide 41 text

class OurMainApplication : Application(), CoreComponentProvider { private lateinit var coreComponent: CoreComponent override fun provideCoreComponent(): CoreComponent { if (!this::coreComponent.isInitialized) { coreComponent = DaggerCoreComponent .builder() .build() } return coreComponent } }

Slide 42

Slide 42 text

class OurMainApplication : Application(), CoreComponentProvider { private lateinit var coreComponent: CoreComponent override fun provideCoreComponent(): CoreComponent { if (!this::coreComponent.isInitialized) { coreComponent = DaggerCoreComponent .builder() .build() } return coreComponent } }

Slide 43

Slide 43 text

class OurMainApplication : Application(), CoreComponentProvider { private lateinit var coreComponent: CoreComponent override fun provideCoreComponent(): CoreComponent { if (!this::coreComponent.isInitialized) { coreComponent = DaggerCoreComponent .builder() .build() } return coreComponent } }

Slide 44

Slide 44 text

class MainActivity : AppCompatActivity() { @Inject lateinit var expensiveObject: ExpensiveObject override fun onCreate(savedInstanceState: Bundle?) { ... DaggerFeature2Component .builder() .coreComponent(this.coreComponent()) .build() .inject(this) } }

Slide 45

Slide 45 text

class MainActivity : AppCompatActivity() { @Inject lateinit var expensiveObject: ExpensiveObject override fun onCreate(savedInstanceState: Bundle?) { ... DaggerFeature2Component .builder() .coreComponent(this.coreComponent()) .build() .inject(this) } }

Slide 46

Slide 46 text

Dagger.Android

Slide 47

Slide 47 text

@Scope annotation class ActivityScope

Slide 48

Slide 48 text

@Module(includes = [ AndroidSupportInjectionModule::class ]) abstract class AppModule { @ActivityScope @ContributesAndroidInjector() abstract fun contributesMainActivityInjector(): MainActivity @ActivityScope @ContributesAndroidInjector() abstract fun contributesOtherActivityInjector(): OtherActivity }

Slide 49

Slide 49 text

@Module(includes = [ AndroidSupportInjectionModule::class ]) abstract class AppModule { @ActivityScope @ContributesAndroidInjector() abstract fun contributesMainActivityInjector(): MainActivity @ActivityScope @ContributesAndroidInjector() abstract fun contributesOtherActivityInjector(): OtherActivity }

Slide 50

Slide 50 text

class OurMainApplication : Application(), CoreComponentProvider, HasActivityInjector { @Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector private lateinit var coreComponent: CoreComponent override fun onCreate() { super.onCreate() DaggerAppComponent.create().inject(this) } override fun activityInjector(): AndroidInjector = dispatchingActivityInjector }

Slide 51

Slide 51 text

class OurMainApplication : Application(), CoreComponentProvider, HasActivityInjector { @Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector private lateinit var coreComponent: CoreComponent override fun onCreate() { super.onCreate() DaggerAppComponent.create().inject(this) } override fun activityInjector(): AndroidInjector = dispatchingActivityInjector }

Slide 52

Slide 52 text

class OurMainApplication : Application(), CoreComponentProvider, HasActivityInjector { @Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector private lateinit var coreComponent: CoreComponent override fun onCreate() { super.onCreate() DaggerAppComponent.create().inject(this) } override fun activityInjector(): AndroidInjector = dispatchingActivityInjector }

Slide 53

Slide 53 text

class OurMainApplication : Application(), CoreComponentProvider, HasActivityInjector { @Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector private lateinit var coreComponent: CoreComponent override fun onCreate() { super.onCreate() DaggerAppComponent.create().inject(this) } override fun activityInjector(): AndroidInjector = dispatchingActivityInjector }

Slide 54

Slide 54 text

class MainActivity : AppCompatActivity() { @Inject lateinit var expensiveObject: ExpensiveObject override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) ... } }

Slide 55

Slide 55 text

class MainActivity : AppCompatActivity() { @Inject lateinit var expensiveObject: ExpensiveObject override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) ... } }

Slide 56

Slide 56 text

AndroidInjection.inject(this) Activity Fragment Service Content Provider Broadcast Receiver this

Slide 57

Slide 57 text

Tip @Inject lateinit var expensiveObject: Lazy expensiveObject.get()

Slide 58

Slide 58 text

• Always go for static provide methods. • Nobody cares about name of your Scope • Expose the application context through a component builder instead of a module with constructor argument. Or even better: use the new factory! Tip

Slide 59

Slide 59 text

@Module class Feature1Module { @Provides fun provideString() = "test" } Tip

Slide 60

Slide 60 text

@Module abstract class Feature1Module { @Binds abstract fun provideString() } Tip

Slide 61

Slide 61 text

Tip Module Module Factory Component @Provides Create a inject object Component @Binds Create a inject object

Slide 62

Slide 62 text

Tip @Binds @IntoMap @ViewModelKey(X1::class) abstract bindUserViewModel(x1 : ObjX) : ObjX

Slide 63

Slide 63 text

• Provide dependencies through class constructors • Avoid unnecessary scoping - Use scope annotations sparingly and judiciously Tl;Dr

Slide 64

Slide 64 text

• Use reusable components for expensive and immutable dependencies • Use @Binds instead of a @Provides method when simply delegating one type to another. • Write tests , switch dependencies on tests with a test component instead of overriding modules. Tl;Dr

Slide 65

Slide 65 text

• Using Dagger in multi-module by Marcos Holgado • Dependency injection in a multi module project - Plaid - Ben Weiss • Modularising Android Application by Marvin Ramin Resources

Slide 66

Slide 66 text

https://superapp.is https://www.gojek.io

Slide 67

Slide 67 text

Adit Lal @aditlal adit.dev Thats all folks!