• Save
Dagger: A Fast Dependency Injector for Android and Java
Upcoming SlideShare
Loading in...5
×
 

Dagger: A Fast Dependency Injector for Android and Java

on

  • 5,132 views

Video and slides synchronized, mp3 and slide download available at http://bit.ly/YA0YEx. ...

Video and slides synchronized, mp3 and slide download available at http://bit.ly/YA0YEx.

Jesse Wilson introduces Dagger, a dependency injection framework for Java, covering the motivation behind its creation, examples on how to use it and some of the internal details. Filmed at qconsf.com.

Jesse Wilson develops apps and open source at Square. He's worked on Android, Gson, Guice, Guava, and Shush.

Statistics

Views

Total Views
5,132
Views on SlideShare
5,132
Embed Views
0

Actions

Likes
12
Downloads
0
Comments
2

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Sipho we're glad you liked it. Please check our presentation page to find more interesting presentations http://www.infoq.com/presentations/
    Are you sure you want to
    Your message goes here
    Processing…
  • This was a lovely presentation
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Dagger: A Fast Dependency Injector for Android and Java Dagger: A Fast Dependency Injector for Android and Java Presentation Transcript

  • Jesse Wilson from Square Dagger A fast dependency injector for Android and Java.Thursday, November 8, 12
  • Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations /Dagger InfoQ.com: News & Community Site• 750,000 unique visitors/month• Published in 4 languages (English, Chinese, Japanese and Brazilian Portuguese)• Post content from our QCon conferences• News 15-20 / week• Articles 3-4 / week• Presentations (videos) 12-15 / week• Interviews 2-3 / week• Books 1 / month
  • Presented at QCon San Francisco www.qconsf.comPurpose of QCon- to empower software development by facilitating the spread ofknowledge and innovationStrategy - practitioner-driven conference designed for YOU: influencers ofchange and innovation in your teams- speakers and topics driving the evolution and innovation- connecting and catalyzing the influencers and innovatorsHighlights- attended by more than 12,000 delegates since 2007- held in 9 cities worldwide
  • Introduction Motivation Using Dagger Inside Dagger Wrapping UpThursday, November 8, 12
  • •Guice Jesse •javax.inject •Android Core LibrariesThursday, November 8, 12
  • Thursday, November 8, 12
  • We’re Hiring! squareup.com/careersThursday, November 8, 12
  • Introduction Motivation Using Dagger Inside Dagger Wrapping UpThursday, November 8, 12
  • Motivation for Dependency Injection •Decouple concrete from concrete •UniformityThursday, November 8, 12
  • Dependency Injection Choices •PicoContainer •Spring •Guice •javax.injectThursday, November 8, 12
  • (cc) creative commons from flickr.com/photos/getbutterfly/6317955134/Thursday, November 8, 12
  • (cc) creative commons from flickr.com/photos/wikidave/2988710537/Thursday, November 8, 12
  • We still love you, Froyo •Eager vs. lazy graph construction •Reflection vs. codegenThursday, November 8, 12
  • “I didn’t really expect anyone to use [git] because it’s so hard to use, but that turns out to be its big appeal. No technology can ever be too arcane or complicated for the black t-shirt crowd.” –Linus Torvalds typicalprogrammer.com/?p=143Thursday, November 8, 12
  • No black t-shirt necessaryThursday, November 8, 12
  • (cc) creative commons from flickr.com/photos/mike_miley/5969110684/Thursday, November 8, 12
  • CoffeeApp •Know everything at CoffeeMaker build time. •Easy to see how Pump dependencies are Thermosiphon used & satisfied HeaterThursday, November 8, 12
  • Motivation for Dagger •Its like Guice, but with speed instead of features also... •Simple •PredictableThursday, November 8, 12
  • Introduction Motivation Using Dagger Inside Dagger Wrapping UpThursday, November 8, 12
  • Dagger?Thursday, November 8, 12
  • DAGger.Thursday, November 8, 12
  • DAGger. Directed Acyclic GraphThursday, November 8, 12
  • (cc) creative commons from flickr.com/photos/uscpsc/7894303566/Thursday, November 8, 12
  • (cc) creative commons from flickr.com/photos/uscpsc/7894303566/Thursday, November 8, 12
  • coffee maker heater thermosiphon pump (cc) creative commons from flickr.com/photos/uscpsc/7894303566/Thursday, November 8, 12
  • CoffeeApp CoffeeMaker Pump Thermosiphon Heater (cc) creative commons from flickr.com/photos/uscpsc/7894303566/Thursday, November 8, 12
  • ! class Thermosiphon implements Pump { ! private final Heater heater; ! Thermosiphon(Heater heater) { ! this.heater = heater; ! } ! @Override public void pump() { ! if (heater.isHot()) { ! System.out.println("=> => pumping => =>"); ! } ! } ! }Thursday, November 8, 12
  • Declare Dependencies class Thermosiphon implements Pump { private final Heater heater; @Inject Thermosiphon(Heater heater) { this.heater = heater; } ... }Thursday, November 8, 12
  • Declare Dependencies class CoffeeMaker { @Inject Heater heater; @Inject Pump pump; ... }Thursday, November 8, 12
  • Satisfy Dependencies @Module class DripCoffeeModule { @Provides Heater provideHeater() { return new ElectricHeater(); } @Provides Pump providePump(Thermosiphon pump) { return pump; } }Thursday, November 8, 12
  • Build the Graph class CoffeeApp { public static void main(String[] args) { ObjectGraph objectGraph = ObjectGraph.create(new DripCoffeeModule()); CoffeeMaker coffeeMaker = objectGraph.get(CoffeeMaker.class); coffeeMaker.brew(); } }Thursday, November 8, 12
  • Neat features •Lazy<T> •Module overrides •MultibindingsThursday, November 8, 12
  • Lazy<T> class GridingCoffeeMaker { @Inject Lazy<Grinder> lazyGrinder; public void brew() { while (needsGrinding()) { // Grinder created once and cached. Grinder grinder = lazyGrinder.get() grinder.grind(); } } }Thursday, November 8, 12
  • Module Overrides @Module( includes = DripCoffeeModule.class, entryPoints = CoffeeMakerTest.class, overrides = true ) static class TestModule { @Provides @Singleton Heater provideHeater() { return Mockito.mock(Heater.class); } }Thursday, November 8, 12
  • Multibindings @Module class TwitterModule { @Provides(type=SET) SocialApi provideApi() { ... } } @Module class GooglePlusModule { @Provides(type=SET) SocialApi provideApi() { ... } } ... @Inject Set<SocialApi>Thursday, November 8, 12
  • Introduction Motivation Using Dagger Inside Dagger Wrapping UpThursday, November 8, 12
  • Graphs!Thursday, November 8, 12
  • Creating Graphs •Compile time •annotation processor •Runtime •generated code loader •reflectionThursday, November 8, 12
  • Using Graphs •Injection •Validation •Graphviz!Thursday, November 8, 12
  • But how? •Bindings have names like “com.squareup.geo.LocationMonitor” •Bindings know the names of their dependencies, like “com.squareup.otto.Bus”Thursday, November 8, 12
  • final class CoffeeMaker$InjectAdapter extends Binding<CoffeeMaker> { private Binding<Heater> f0; private Binding<Pump> f1; public CoffeeMaker$InjectAdapter() { super("coffee.CoffeeMaker", ...); } public void attach(Linker linker) { f0 = linker.requestBinding("coffee.Heater", coffee.CoffeeMaker.class); f1 = linker.requestBinding("coffee.Pump", coffee.CoffeeMaker.class); } public CoffeeMaker get() { coffee.CoffeeMaker result = new coffee.CoffeeMaker(); injectMembers(result); return result; } public void injectMembers(CoffeeMaker object) { object.heater = f0.get(); object.pump = f1.get(); } public void getDependencies(Set<Binding<?>> bindings) { bindings.add(f0); bindings.add(f1); } }Thursday, November 8, 12
  • Validation •Eager at build time •Lazy at runtimeThursday, November 8, 12
  • dagger-compiler (cc) creative commons from flickr.com/photos/discover-central-california/8010906617Thursday, November 8, 12
  • javax.annotation.processing Built into javac Foolishly easy to use. Just put dagger-compiler on your classpath!Thursday, November 8, 12
  • It’s a hassle (for us) •Coping with prebuilt .jar files •Versioning •TestingThursday, November 8, 12
  • ... and it’s limited (for you) •No private or final field access •Incremental builds are imperfect •ProGuardThursday, November 8, 12
  • ... but it’s smoking fast! (for your users) •Startup time for Square Wallet on one device improved from ~5 seconds to ~2 secondsThursday, November 8, 12
  • Different Platforms are Different •HotSpot •Android •GWTThursday, November 8, 12
  • HotSpot: Java on the Server •The JVM is fast •Code bases are hugeThursday, November 8, 12
  • Android •Battery powered •Garbage collection causes jank •Slow reflection, especially on older devices •Managed lifecycleThursday, November 8, 12
  • GWT •Code size really matters •Compiler does full-app optimizations •No reflection. github.com/tbroyer/sheathThursday, November 8, 12
  • API Design in the GitHub age •Forking makes it easy to stay small & focusedThursday, November 8, 12
  • javax.inject public @interface Inject {} ! public @interface Named { ! String value() default ""; ! } ! public interface Provider { ! T get(); ! } ! public @interface Qualifier {} ! public @interface Scope {} ! public @interface Singleton {}Thursday, November 8, 12
  • ! public interface Lazy<T> { ! T get(); ! } ! public interface MembersInjector<T> { ! void injectMembers(T instance); ! } ! public final class ObjectGraph { ! public static ObjectGraph create(Object... modules); ! public ObjectGraph plus(Object... modules); ! public void validate(); ! public void injectStatics(); ! public <T> T get(Class type); dagger ! ! } public <T> T inject(T instance); ! public @interface Module { ! Class[] entryPoints() default { }; ! Class[] staticInjections() default { }; ! Class[] includes() default { }; ! Class addsTo() default Void.class; ! boolean overrides() default false; ! boolean complete() default true; ! } ! public @interface Provides { ! enum Type { UNIQUE, SET } ! Type type() default Type.UNIQUE; ! }Thursday, November 8, 12
  • Introduction Motivation Using Dagger Inside Dagger Wrapping UpThursday, November 8, 12
  • Guice •Still the best choice for many apps •May soon be able to mix & match with DaggerThursday, November 8, 12
  • Dagger •Dagger 0.9 today! •Dagger 1.0 in 2012ish •Friendly open source.Thursday, November 8, 12
  • square.github.com/daggerThursday, November 8, 12
  • Questions? squareup.com/careers swank.ca corner.squareup.com jwilson@squareup.comThursday, November 8, 12