Using Dagger for dependency injection on constructors

前端 未结 5 685
故里飘歌
故里飘歌 2021-01-31 17:17

So, I\'m currently redesigning an Android app of mine to use Dagger. My app is large and complicated, and I recently came across the following scenario:

Object A requir

5条回答
  •  孤街浪徒
    2021-01-31 18:06

    You're having a problem because you are mixing injectables and non injectables in your constructor. The general rules for injection that will save you tons of heartache and keep your code clean are:

    1. Injectables can ask for other injectables in their constructor, but not for newables.

    2. Newables can ask for other newables in their constructor but not for injectables.

    Injectables are service type objects, ie objects that do work such as a CreditCardProcessor, MusicPlayer, etc.

    Newables are value type objects such as CreditCard, Song, etc.

提交回复
热议问题