androidmvpdagger-2presenter

should presenters(mvP) be injected(dagger2) to views in android?


In the context of developing and android app, should I use presenters directly in views using 'new' or would it be better if I injected them to the view.

Pros/cons for not using injected presenters:

  1. Faster development time, without having to write components and modules.
  2. Presenters are tightly coupled with the views, I don't see this as much of a problem as most of the time presenters are not shared across multiple views(ie. one single view for a presenter).
  3. Might be a problem for testing, as with using dependency injection mock implementations of the presenters can be provided(not sure if this is any useful, need more insight on this).

Solution

  • You're right. Using injection will only help you in the long run. You could either spend 5 minutes setting up your module / component, or you could be just coding.

    As long as you don't do proper testing, there is not much difference to it, if you presenter looks like the following

    mPresenter = new Presenter();
    

    Assuming you use constructor injection properly, after creating your component, you save some lines as compared to

    @Inject Presenter mPresenter;
    
    // onCreate or some other place
    {
        getComponent().inject(this); /* getComponent() also 4-5 lines */
    }
    

    Again. If you use proper constructor injection there is a good chance you don't have many module code. Just creating some component will do.

    But you saved some minutes and once you want to do testing this is just some easy refactoring, which can quickly be done.

    So why Dagger?

    This was assuming your presenter has no dependencies on other objects. But what if it does?

    SharedPreferences preferences = getPreferences();
    MyStorage storage = new MyStorage(preferences);
    mPresenter = new Presenter(storage);
    

    Using something to store your data is properly a good use case. While you just added some more logic about object creation to your activity, the dagger implementation would still look the same.

    Even more?

    Now let's assume you want to share this storage from above between activities. Now you have to add some logic to your Application or in some other place where you can create a Singleton to use throughout your app.

    This will probably not be your only singleton, though, and you will start to clutter up your Application as well. Don't get me started on managing those objects lifecycle, e.g. user logging in or out, make sure to clear that cached data!

    Again. The dagger implementation still looks the same. If there is some more logic needed it is well placed in the modules and abstracted with component dependencies.


    Once you start thinking I could just create classes that handle object construction and injection you know that you could have just used dagger in the first place ;)

    I also wrote a blog post about dagger basics including how constructor injection works, which many beginners are not using properly for some reason.