Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dagger 2.0 - new dependency injection for Android (Droidcon Krakow 2014)

Radek Piekarz
December 05, 2014

Dagger 2.0 - new dependency injection for Android (Droidcon Krakow 2014)

Short introduction to Dagger 2.0 during Droidcon Kraków 2014.

See more information here.

Radek Piekarz

December 05, 2014
Tweet

More Decks by Radek Piekarz

Other Decks in Programming

Transcript

  1. Guice / RoboGuice • Used new features in Java 1.5

    • No XML configuration • Not so easy to debug • Runtime validation
  2. Dagger 1 • Compile time validation • Do more work

    during compile and less in runtime • Faster than Guice/RoboGuice
  3. Dagger 2 API • @Module & @Provides - mechanism for

    providing dependencies • @Inject - mechanism for requesting dependencies • @Component bridge between modules and injections
  4. @Inject class CoffeeMaker { private final Lazy<Heater> heater; private final

    Pump pump; @Inject CoffeeMaker(Lazy<Heater> heater, Pump pump) { this.heater = heater; this.pump = pump; }
  5. Example app public class CoffeeApp { public static void main(String[]

    args) { Coffee coffee = Dagger_Coffee.builder() .build(); coffee.maker().brew(); } }
  6. Dagger 1 vs Dagger 2 public class CoffeeApp implements Runnable

    { @Inject CoffeeMaker coffeeMaker; @Override public void run() { coffeeMaker.brew(); } public static void main(String[] args) { ObjectGraph objectGraph = ObjectGraph.create(new DripCoffeeModule()); CoffeeApp coffeeApp = objectGraph.get(CoffeeApp.class); coffeeApp.run(); } }
  7. Dagger 2 Pros • It is really fast • Compile

    time validation for entire graph • Easy debugging Cons • Not flexible • There is no dynamism