Yet another MVP article — Part 2: How Dagger helps with the project

Written by mohsenoid | Published 2016/11/28
Tech Story Tags: android | dagger-2 | rxjava | yetanothermvp | mvp

TLDRvia the TL;DR App

Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern methods and libraries

Previously on this article…

Yet another MVP article — Part 1: Let’s get to know the project_Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com

Let’s think that you have studied part1 which means you know everything about the project’s modules and structure.

now let’s continue…

Would you explain how Dagger wire up modules and MVP layers in the core?

Dagger diagram

Dagger2 uses Modules, Components, and SubComponents to know what and how to inject dependencies. You can find more articles which explain more about what’s going on inside Dagger.

From Dagger diagram bottom to top I can mention CacheModule and SearchModule which provide View and Presenter:

“Trying new shoes doesn’t require you to saw your feet off”!, — Novoda

First of all, take care of return types of provide methods, they all return interfaces, not the implementation of that interface! This is how we can replace the implementation for further tests, and also I have to mention that this is a mixture of Open/closed, Liskov substitution and Dependency inversion principles of SOLID.

Secondly, do not forget that we are still inside core module and we have no idea about how it is gonna be used by View.

Finally, these two modules are being used by CacheSubComponent and SearchSubComponent that are ApplicationComponent’s SubComponents, which I will explain in app module.

We had two more modules inside core that help us with providing retrofit dependencies, ApiModule and ClientModule. Being familiar with retrofit2 will help you with understanding what’s inside, however, I will explain everything about it in next parts of this article.

Both of these modules and DatabaseModule are being used by ApplicationComponent which I will explain in app module.

...and what’s going on with Dagger inside app module?

The app module most important Dagger part is ApplicationComponent which uses AndroidModule and ApplicationModule to provide all the Application injections.

What’s inside ApplicationComponent:

1. AndroidModule:

This module provides Application Context and Application Resources which always comes handy for injection while developing for android to access for instance to Services or different Resources. Some developers implement this module inside ApplicationModule but I prefer to take it separate for more clearance.

This module would be created in MarvelApplication class which extends Application class:

as you might have been mentioned this class is abstract which complete in debug or release buildTypes by implementing initApplication() method which comes handy for doing things for release version or debug version.

In this sample I have used debug MarvelApplicationImpl to plant Timber library for avoiding Timber logs in release version:

2. ApplicationModule:

Almost all application requirements are being provided in this module,

  • isDebug: use BuildConfig.DEBUG boolean variable to check if the running instance of the application is in debug mode and use it for logging network API calls in logcat.
  • networkTimeoutInSeconds, cacheSize, cacheMaxAge, cacheMaxStale, cacheDir: provide network parameters which being used when creating the OkHttp client for Retrofit.
  • endpoint: provide the API’s endpoint for retrofit.
  • appScheduler: provide RxAndroid schedulers which I will explain in RxJava part.
  • isConnect: provide network state for handling offline situations.

3. ApiModule, ClientModule from core

4. …and DatabaseModule

5. SubComponents:

The nice thing about Dagger is SubComponent, you can add SubComponents to your main ApplicationComponent and use all providers within and add yours.

In this sample, SearchSubComponent and CacheSubComponent add up to ApplicationComponent and make it more brilliant.

let’s take a look…

SearchSubComponent and CacheSubComponent:

let’s take another look to ApplicationComponent:

as you can see we had the inject methods which helps with injection, but what are those plus methods?!

First of all, I have to mention that inject and plus is the name!! you can rename it to whatever you like and Dagger take care of input and output of the method to know what’s their use! but please don’t monkey around with the name cause your team member must read this code later…

using plus methods you can ask Dagger to add a SubComponent to the ApplicationComponent and do the inject method inside that SubComponent.

plus methods take a Module and return a SubComponent using that module.

let’s take a closer look:

Well well well… where does Dagger integration begin?

By a little change in manifest we can ask Android to use MarvelApplication implementations as the start point of our app:

and in MarvelApplication class we create application component once and use it in Activities and Fragments by extending BaseActivity and BaseFragment classes. Both of them are abstract and the injectDependencies method must be implemented inside the Activity or Fragment.

How to drown yourself inside Dagger?!

You can do more Dagger if you like!

Take a look at this article which explains how to Daggerify everything… I have done it inside this sample project by extending SearchModule and CacheModule from core module using AppSearchModule and AppCacheModule in app module.

This is how the magic happens and that’s it for now…

Please clone the project repo from GitHub and get more familiar with it because in next part I will explain more about retrofit and how it helps with network API calls and caching using it.

I look forward to your comments and helping for more improvements with this article.

Share this article if you think it is useful, and follow me for more articles Mohsen Mirhoseini.

To be continued…

Yet another MVP article — Part 3: Calling APIs using Retrofit_Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com


Written by mohsenoid | Senior Android Developer
Published by HackerNoon on 2016/11/28