I'm new to using Dagger 2 and am wondering what its advantages are over a technique that I currently use to achieve dependency injection.
Currently to achieve DI I create a project with two flavors, mock and prod. Within these flavors I create a class called Injector.
//prod Injector
public class Injector {
public static INetworkLayer provideNetworkLayer() {
return new ProductionNetworkLayer();
}
}
//mock Injector
public class Injector {
public static INetworkLayer provideNetworkLayer() {
return new MockNetworkLayer();
}
}
The Injector class has a static method for each object I need to inject. In my application code I can then simply write:
INetworkLayer networkLayer = Injector.provideNetworkLayer();
This works great for testing because in my production code or test code I can simply tell the Injector what I want and depending on what build variant I am using, the Injector will give me either the production object or the mocked test object. If I want a certain object to be a singleton I am simply able to hold a reference to it in the Injector class and then give that reference out when provide...() is called.
I have started using Dagger2 in a new project and have found that it is much more confusing to setup mock / prod dependencies compared to the "Injector" class method that I demonstrated above.
How is what Dagger 2 does different than the "Injector class" method?
Is there a good way to use Dagger 2 to provide mock and prod classes?