TruckMuncher/TruckMuncher-Android — pull request #29 by MariusVolkhart (2014-11-03 05:12:21+00:00)
Title: Automatic IoC through Dagger

This introduces automatic dependency injection through the use of Dagger.

The problem we face is that we need to be able to swap out implementation details in different scenarios. For example, with our networking layer, during dev we want to use a mock layer at the moment. In a release build we always want to have the real deal. During tests, we want the test fixture to have full control over what the network is doing. So we have 3 different implementations, that need to be hot swappable, but all need to behave more or less identically.

The other problem we face is that Android components such as `Activity` `Fragment` and `Service` have to have no argument constructors, so we cannot pass dependencies into them that way. These objects need to be able to get their own dependencies, but we want to retain control over how that dependency is filled.

Dagger let's us do that.

The heavy lifting is done by Dagger `Module` classes. You can see that we have multiple ones for the different scenarios we run. The `MockNetworkModule` and `LocalNetworkModule` override the `NetworkModule` so that specific behaviors can be altered. To see how this is used, look at the `SyncAdapterTest` class.

@nockertsb Please take a look and let me know if you have any questions or comments or concerns. This is one a sample so far, and not all the dependencies have been moved over. Also, there are several tests remaining for the `SyncAdapter`. I just wanted to get this out there and see if you were on board. So don't merge it yet if you are.