2

Can anybody tell me whats the difference in using Dependency Injection(e.g Dagger2) to decouple the view(activity) from its presenter or to use Interfaces which the View(Activity) implements to decouple from presenter?

I'm confused.. When I try to Unit Test the presenter both approaches should work. But why using a tool like dagger when simple interfaces are sufficiant? Hope that smbdy. can help. Thanks

ECommerce
  • 469
  • 1
  • 4
  • 9
  • I find this question is a little unclear as it is currently worded. Can you provide a minimal code example of what you mean? – David Rawson Apr 25 '17 at 20:13
  • What is unclear? I can pass for example a reference from the view to the presenter using an interface that the view implements and the presenter instanciates. So I will have a weak coupling between the to classes. The implementation of the interface is not important for the presenter, that means I can use any MockView. Using Dagger2 will have the same result but it injects the view object in the presenter. In terms of testing it easy easier to use interfaces. What is the advantage using dagger? interface can solve the problem much easier... – ECommerce Apr 26 '17 at 08:12
  • If you are asking the difference between MVP with and without Dagger 2 there is no difference except dependencies in the presenter are managed better. For a presenter with a very small object graph it will not make much difference at all – David Rawson Apr 26 '17 at 08:16
  • 1
    Okay thanks for clarifying Christian. I hope the initial comment didn't come across as rude - I just didn't understand the question. MVP with Dagger 2 is very "in vogue" since there are lots of Medium articles about it but you certainly don't have to use Dagger 2 if you want to write an MVP app. The other benefits are just the general benefits of using a dependency injection framework like in the answers to [this question](http://stackoverflow.com/q/131975) – David Rawson Apr 26 '17 at 08:47
  • BUt if I try to Test the View, I need to change the injected presenter if i want to use a Mockpresenter don't I? In terms of using interfaces I just instanciate the interface with the new Mockpresenter or any presenter I want. I can't get why it should be better using Dagger in terms of Testing mvp apps in android. – ECommerce Apr 26 '17 at 08:47
  • Thanks for the answer. @DavidRawson – ECommerce Apr 26 '17 at 09:03

1 Answers1

0

there is not any interfere using both interface and Dagger. Dagger is just for DI (dependecy injection) and is used for providing different layers requirements in MVP.

the view interface must be used always because this is how MVP must be and views are decoupled from presenters by implementing an interface.

you use Dagger to provide things from a specific layer to the other. you can completely put Dagger aside and create a helper class in your view layer which implements some helper interface and send it to the presenters through constructions all over the code but this is kinda messy and thats why they use Dagger.

look at my sample project which i used Dagger: https://gitlab.com/amirziarati/Echarge

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52