1

im debating if its better for my team to learn dagger vs just using a singleton container that would provide the dependencies. this is how it was done in the 'old days'. I am dealing specifically with android and here is an example i found on the web of using a factory container for dependency injection:

public class Container {

private static Container instance;
private FavoriteAdder favoriteAdder;
private FavoriteRepo favoriteRepo;
private FavoritesGetter favoritesGetter;
private FavoriteRemover favoritesRemover;

private Container() {}

public static Container getInstance() {
    if (instance == null) {
        instance = new Container();
    }
    return instance;
}

public FavoriteAdder getFavoriteAdder() {
    if (favoriteAdder == null) {
        favoriteAdder = new FavoriteAdder(getFavoriteRepo());
    }
    return favoriteAdder;
}

public FavoritesGetter getFavoritesGetter() {
    if (favoritesGetter == null) {
        favoritesGetter = new FavoritesGetter(getFavoriteRepo());
    }
    return favoritesGetter;
}

public FavoriteRemover getFavoritesRemover() {
    if (favoritesRemover == null) {
        favoritesRemover = new FavoriteRemover(getFavoriteRepo());
    }
    return favoritesRemover;
}

public FavoriteRepo getFavoriteRepo() {
    if (favoriteRepo == null) {
        CachingFavoriteRepo inMemoryRepo = new InMemoryFavoriteRepo();
        FavoriteRepo remoteRepo = new SlowFavoriteRepoWrapper(new MockRemoteFavoriteRepo(), 1000);
        favoriteRepo = new FavoriteReadThroughCache(inMemoryRepo, remoteRepo);
    }
    return favoriteRepo;
}

}

and then to use it in an activity we can do this:

public class FavoriteActivity extends ActionBarActivity {


    private FavoriteAdder favoriteAdder = Container.getInstance().getFavoriteAdder();
    private FavoritesGetter favoritesGetter = Container.getInstance().getFavoritesGetter();
    private FavoriteRemover favoritesRemover = Container.getInstance().getFavoritesRemover();

    private FavoriteCrudPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            presenter = new FavoriteCrudPresenter(favoriteAdder, favoritesGetter, favoritesRemover, Schedulers.io(), AndroidSchedulers.mainThread(), backgroundExecutor, foregroundExecutor);
//...

}}

it seems easier then using dagger and im wondering what the benefits would be of dagger over this way ? Why do we even bother with dagger i guess its easier to make singletons in dagger could be one scenario.

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • 1
    Dagger's goal is explicitly to write the exact factory you would write yourself, except to do it automatically. When you have four objects, writing it out manually makes lots of sense. Probably the same when you have forty. When your project gets to four hundred or four thousand objects, or when the constructors change frequently, you might figure it's not worth the bookkeeping and file merges and get a tool to write that boilerplate for you. – Jeff Bowman May 11 '17 at 23:09
  • I understand your question but asking "which is better" is a magnet for close votes on "primarily opinion based". Perhaps you can reword it and make reference to the pre-existing knowledge in SO? I think [this question](http://stackoverflow.com/questions/557742/dependency-injection-vs-factory-pattern) is quite similar – David Rawson May 12 '17 at 22:25
  • There is a [Google Clean Code Talk about DI and Service Locator](https://www.youtube.com/watch?v=RlfLCWKxHJ0&index=3&list=PL693EFD059797C21E) that goes into some detail about why you should use DI and not fetch dependencies yourself – David Medenjak May 13 '17 at 10:01
  • @DavidMedenjak thanks. now i get it. – j2emanue May 17 '17 at 04:03

0 Answers0