SlideShare a Scribd company logo
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.com
Purpose of QCon
- to empower software development by facilitating the spread of
knowledge and innovation
Strategy
 - practitioner-driven conference designed for YOU: influencers of
change and innovation in your teams
- speakers and topics driving the evolution and innovation
- connecting and catalyzing the influencers and innovators
Highlights
- attended by more than 12,000 delegates since 2007
- held in 9 cities worldwide
Introduction
                            Motivation
                           Using Dagger
                           Inside Dagger
                           Wrapping Up




Thursday, November 8, 12
•Guice
               Jesse       •javax.inject
                           •Android Core Libraries



Thursday, November 8, 12
Thursday, November 8, 12
We’re Hiring!
      squareup.com/careers



Thursday, November 8, 12
Introduction
                            Motivation
                           Using Dagger
                           Inside Dagger
                           Wrapping Up




Thursday, November 8, 12
Motivation for
                           Dependency
                             Injection

                    •Decouple concrete from concrete
                    •Uniformity

Thursday, November 8, 12
Dependency
                             Injection
                             Choices
                    •PicoContainer
                    •Spring
                    •Guice
                    •javax.inject
Thursday, 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. codegen


Thursday, 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=143
Thursday, November 8, 12
No black t-shirt
                             necessary


Thursday, 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              Heater




Thursday, November 8, 12
Motivation for
                                  Dagger
                    •It's like Guice, but with speed
                           instead of features

                           also...



                    •Simple
                    •Predictable
Thursday, November 8, 12
Introduction
                            Motivation
                           Using Dagger
                           Inside Dagger
                           Wrapping Up




Thursday, November 8, 12
Dagger?




Thursday, November 8, 12
DAGger.




Thursday, November 8, 12
DAGger.

                           Directed
                           Acyclic
                           Graph

Thursday, 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
                    •Multibindings


Thursday, 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 Up




Thursday, November 8, 12
Graphs!



Thursday, November 8, 12
Creating Graphs
                    •Compile time
                     •annotation processor

                    •Runtime
                     •generated code loader
                     •reflection
Thursday, 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 runtime


Thursday, November 8, 12
dagger-compiler



                    (cc) creative commons from flickr.com/photos/discover-central-california/8010906617
Thursday, 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
                    •Testing


Thursday, November 8, 12
... and it’s limited
                                (for you)

                    •No private or final field access
                    •Incremental builds are imperfect
                    •ProGuard


Thursday, 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 seconds




Thursday, November 8, 12
Different Platforms
                          are Different

                    •HotSpot
                    •Android
                    •GWT


Thursday, November 8, 12
HotSpot:
                    Java on the Server

                    •The JVM is fast
                    •Code bases are huge


Thursday, November 8, 12
Android

                    •Battery powered
                    •Garbage collection causes jank
                    •Slow reflection, especially on older
                           devices
                    •Managed lifecycle

Thursday, November 8, 12
GWT
                    •Code size really matters
                    •Compiler does full-app
                           optimizations
                    •No reflection.

               github.com/tbroyer/sheath

Thursday, November 8, 12
API Design
                       in the GitHub age

                    •Forking makes it easy to stay small
                           & focused




Thursday, 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 Up




Thursday, November 8, 12
Guice

                    •Still the best choice for many apps
                    •May soon be able to mix & match
                           with Dagger




Thursday, November 8, 12
Dagger

                    •Dagger 0.9 today!
                    •Dagger 1.0 in 2012ish
                    •Friendly open source.



Thursday, November 8, 12
square.github.com/dagger




Thursday, November 8, 12
Questions?


  squareup.com/careers                         swank.ca
  corner.squareup.com              jwilson@squareup.com


Thursday, November 8, 12

More Related Content

Viewers also liked

MVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixMVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mix
Florina Muntenescu
 
Giới thiệu chung về Du học Úc
Giới thiệu chung về Du học ÚcGiới thiệu chung về Du học Úc
Giới thiệu chung về Du học Úc
Công ty Du học Toàn cầu ASCI
 
Js高级技巧
Js高级技巧Js高级技巧
Js高级技巧
fool2fish
 
Opinn aðgangur að vísindaefni
Opinn aðgangur að vísindaefniOpinn aðgangur að vísindaefni
Opinn aðgangur að vísindaefni
University of Iceland
 
ഒരു അണ്‍സര്‍വ്വേ പ്രദേശത്തെ ഭൂപടനിര്‍മ്മാണപരിശ്രമം - കൂരാച്ചുണ്ടു് ഗ്രാമപഞ്ചാ...
ഒരു അണ്‍സര്‍വ്വേ പ്രദേശത്തെ ഭൂപടനിര്‍മ്മാണപരിശ്രമം - കൂരാച്ചുണ്ടു് ഗ്രാമപഞ്ചാ...ഒരു അണ്‍സര്‍വ്വേ പ്രദേശത്തെ ഭൂപടനിര്‍മ്മാണപരിശ്രമം - കൂരാച്ചുണ്ടു് ഗ്രാമപഞ്ചാ...
ഒരു അണ്‍സര്‍വ്വേ പ്രദേശത്തെ ഭൂപടനിര്‍മ്മാണപരിശ്രമം - കൂരാച്ചുണ്ടു് ഗ്രാമപഞ്ചാ...
Jaisen Nedumpala
 
Depurando Aplicações PHP com XDebug e FirePHP - SETI 2008
Depurando Aplicações PHP com XDebug e FirePHP - SETI 2008Depurando Aplicações PHP com XDebug e FirePHP - SETI 2008
Depurando Aplicações PHP com XDebug e FirePHP - SETI 2008
Jefferson Girão
 
Ic tmagic tm clevedon
Ic tmagic tm clevedonIc tmagic tm clevedon
Ic tmagic tm clevedon
Martin Burrett
 
Oscars after - party
Oscars after - partyOscars after - party
Oscars after - party
Makala D.
 
Balonmán touro
Balonmán touroBalonmán touro
Balonmán touro
davidares1
 
CEIP DE LAREDO //PROXECTO VALORA\\
CEIP DE LAREDO //PROXECTO VALORA\\CEIP DE LAREDO //PROXECTO VALORA\\
CEIP DE LAREDO //PROXECTO VALORA\\
Abraham Domínguez Cuña
 
Php basics
Php basicsPhp basics
Php basics
hamfu
 
Letter s presentatie
Letter s presentatieLetter s presentatie
Letter s presentatie
cmagarry
 
برندسازی بین المللی احمدرضا اشرف العقلایی Dba7-mahan- کارآفرینی
برندسازی بین المللی احمدرضا اشرف العقلایی  Dba7-mahan- کارآفرینیبرندسازی بین المللی احمدرضا اشرف العقلایی  Dba7-mahan- کارآفرینی
برندسازی بین المللی احمدرضا اشرف العقلایی Dba7-mahan- کارآفرینی
Ashrafologhalaei Ahmadreza
 
Understanding Product/Market Fit
Understanding Product/Market FitUnderstanding Product/Market Fit
Understanding Product/Market Fit
Gabor Papp
 

Viewers also liked (16)

MVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixMVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mix
 
Giới thiệu chung về Du học Úc
Giới thiệu chung về Du học ÚcGiới thiệu chung về Du học Úc
Giới thiệu chung về Du học Úc
 
Js高级技巧
Js高级技巧Js高级技巧
Js高级技巧
 
Opinn aðgangur að vísindaefni
Opinn aðgangur að vísindaefniOpinn aðgangur að vísindaefni
Opinn aðgangur að vísindaefni
 
ഒരു അണ്‍സര്‍വ്വേ പ്രദേശത്തെ ഭൂപടനിര്‍മ്മാണപരിശ്രമം - കൂരാച്ചുണ്ടു് ഗ്രാമപഞ്ചാ...
ഒരു അണ്‍സര്‍വ്വേ പ്രദേശത്തെ ഭൂപടനിര്‍മ്മാണപരിശ്രമം - കൂരാച്ചുണ്ടു് ഗ്രാമപഞ്ചാ...ഒരു അണ്‍സര്‍വ്വേ പ്രദേശത്തെ ഭൂപടനിര്‍മ്മാണപരിശ്രമം - കൂരാച്ചുണ്ടു് ഗ്രാമപഞ്ചാ...
ഒരു അണ്‍സര്‍വ്വേ പ്രദേശത്തെ ഭൂപടനിര്‍മ്മാണപരിശ്രമം - കൂരാച്ചുണ്ടു് ഗ്രാമപഞ്ചാ...
 
Depurando Aplicações PHP com XDebug e FirePHP - SETI 2008
Depurando Aplicações PHP com XDebug e FirePHP - SETI 2008Depurando Aplicações PHP com XDebug e FirePHP - SETI 2008
Depurando Aplicações PHP com XDebug e FirePHP - SETI 2008
 
WONDERFUL
WONDERFULWONDERFUL
WONDERFUL
 
Ic tmagic tm clevedon
Ic tmagic tm clevedonIc tmagic tm clevedon
Ic tmagic tm clevedon
 
Oscars after - party
Oscars after - partyOscars after - party
Oscars after - party
 
Balonmán touro
Balonmán touroBalonmán touro
Balonmán touro
 
CEIP DE LAREDO //PROXECTO VALORA\\
CEIP DE LAREDO //PROXECTO VALORA\\CEIP DE LAREDO //PROXECTO VALORA\\
CEIP DE LAREDO //PROXECTO VALORA\\
 
Php basics
Php basicsPhp basics
Php basics
 
Letter s presentatie
Letter s presentatieLetter s presentatie
Letter s presentatie
 
برندسازی بین المللی احمدرضا اشرف العقلایی Dba7-mahan- کارآفرینی
برندسازی بین المللی احمدرضا اشرف العقلایی  Dba7-mahan- کارآفرینیبرندسازی بین المللی احمدرضا اشرف العقلایی  Dba7-mahan- کارآفرینی
برندسازی بین المللی احمدرضا اشرف العقلایی Dba7-mahan- کارآفرینی
 
20120319 aws meister-reloaded-s3
20120319 aws meister-reloaded-s320120319 aws meister-reloaded-s3
20120319 aws meister-reloaded-s3
 
Understanding Product/Market Fit
Understanding Product/Market FitUnderstanding Product/Market Fit
Understanding Product/Market Fit
 

More from C4Media

Streaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live VideoStreaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live Video
C4Media
 
Next Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileNext Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy Mobile
C4Media
 
Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020
C4Media
 
Understand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsUnderstand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java Applications
C4Media
 
Kafka Needs No Keeper
Kafka Needs No KeeperKafka Needs No Keeper
Kafka Needs No Keeper
C4Media
 
High Performing Teams Act Like Owners
High Performing Teams Act Like OwnersHigh Performing Teams Act Like Owners
High Performing Teams Act Like Owners
C4Media
 
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaDoes Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
C4Media
 
Service Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideService Meshes- The Ultimate Guide
Service Meshes- The Ultimate Guide
C4Media
 
Shifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDShifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CD
C4Media
 
CI/CD for Machine Learning
CI/CD for Machine LearningCI/CD for Machine Learning
CI/CD for Machine Learning
C4Media
 
Fault Tolerance at Speed
Fault Tolerance at SpeedFault Tolerance at Speed
Fault Tolerance at Speed
C4Media
 
Architectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsArchitectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep Systems
C4Media
 
ML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.js
C4Media
 
Build Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerBuild Your Own WebAssembly Compiler
Build Your Own WebAssembly Compiler
C4Media
 
User & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleUser & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix Scale
C4Media
 
Scaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeScaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's Edge
C4Media
 
Make Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereMake Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home Everywhere
C4Media
 
The Talk You've Been Await-ing For
The Talk You've Been Await-ing ForThe Talk You've Been Await-ing For
The Talk You've Been Await-ing For
C4Media
 
Future of Data Engineering
Future of Data EngineeringFuture of Data Engineering
Future of Data Engineering
C4Media
 
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreAutomated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
C4Media
 

More from C4Media (20)

Streaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live VideoStreaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live Video
 
Next Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileNext Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy Mobile
 
Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020
 
Understand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsUnderstand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java Applications
 
Kafka Needs No Keeper
Kafka Needs No KeeperKafka Needs No Keeper
Kafka Needs No Keeper
 
High Performing Teams Act Like Owners
High Performing Teams Act Like OwnersHigh Performing Teams Act Like Owners
High Performing Teams Act Like Owners
 
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaDoes Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
 
Service Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideService Meshes- The Ultimate Guide
Service Meshes- The Ultimate Guide
 
Shifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDShifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CD
 
CI/CD for Machine Learning
CI/CD for Machine LearningCI/CD for Machine Learning
CI/CD for Machine Learning
 
Fault Tolerance at Speed
Fault Tolerance at SpeedFault Tolerance at Speed
Fault Tolerance at Speed
 
Architectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsArchitectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep Systems
 
ML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.js
 
Build Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerBuild Your Own WebAssembly Compiler
Build Your Own WebAssembly Compiler
 
User & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleUser & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix Scale
 
Scaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeScaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's Edge
 
Make Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereMake Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home Everywhere
 
The Talk You've Been Await-ing For
The Talk You've Been Await-ing ForThe Talk You've Been Await-ing For
The Talk You've Been Await-ing For
 
Future of Data Engineering
Future of Data EngineeringFuture of Data Engineering
Future of Data Engineering
 
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreAutomated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
 

Recently uploaded

Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 

Recently uploaded (20)

Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 

Dagger: A Fast Dependency Injector for Android and Java

  • 1. Jesse Wilson from Square Dagger A fast dependency injector for Android and Java. Thursday, November 8, 12
  • 2. 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
  • 3. Presented at QCon San Francisco www.qconsf.com Purpose of QCon - to empower software development by facilitating the spread of knowledge and innovation Strategy - practitioner-driven conference designed for YOU: influencers of change and innovation in your teams - speakers and topics driving the evolution and innovation - connecting and catalyzing the influencers and innovators Highlights - attended by more than 12,000 delegates since 2007 - held in 9 cities worldwide
  • 4. Introduction Motivation Using Dagger Inside Dagger Wrapping Up Thursday, November 8, 12
  • 5. •Guice Jesse •javax.inject •Android Core Libraries Thursday, November 8, 12
  • 7. We’re Hiring! squareup.com/careers Thursday, November 8, 12
  • 8. Introduction Motivation Using Dagger Inside Dagger Wrapping Up Thursday, November 8, 12
  • 9. Motivation for Dependency Injection •Decouple concrete from concrete •Uniformity Thursday, November 8, 12
  • 10. Dependency Injection Choices •PicoContainer •Spring •Guice •javax.inject Thursday, November 8, 12
  • 11. (cc) creative commons from flickr.com/photos/getbutterfly/6317955134/ Thursday, November 8, 12
  • 12. (cc) creative commons from flickr.com/photos/wikidave/2988710537/ Thursday, November 8, 12
  • 13. We still love you, Froyo •Eager vs. lazy graph construction •Reflection vs. codegen Thursday, November 8, 12
  • 14. “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=143 Thursday, November 8, 12
  • 15. No black t-shirt necessary Thursday, November 8, 12
  • 16. (cc) creative commons from flickr.com/photos/mike_miley/5969110684/ Thursday, November 8, 12
  • 17. CoffeeApp •Know everything at CoffeeMaker build time. •Easy to see how Pump dependencies are Thermosiphon used & satisfied Heater Thursday, November 8, 12
  • 18. Motivation for Dagger •It's like Guice, but with speed instead of features also... •Simple •Predictable Thursday, November 8, 12
  • 19. Introduction Motivation Using Dagger Inside Dagger Wrapping Up Thursday, November 8, 12
  • 22. DAGger. Directed Acyclic Graph Thursday, November 8, 12
  • 23. (cc) creative commons from flickr.com/photos/uscpsc/7894303566/ Thursday, November 8, 12
  • 24. (cc) creative commons from flickr.com/photos/uscpsc/7894303566/ Thursday, November 8, 12
  • 25. coffee maker heater thermosiphon pump (cc) creative commons from flickr.com/photos/uscpsc/7894303566/ Thursday, November 8, 12
  • 26. CoffeeApp CoffeeMaker Pump Thermosiphon Heater (cc) creative commons from flickr.com/photos/uscpsc/7894303566/ Thursday, November 8, 12
  • 27. ! 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
  • 28. Declare Dependencies class Thermosiphon implements Pump { private final Heater heater; @Inject Thermosiphon(Heater heater) { this.heater = heater; } ... } Thursday, November 8, 12
  • 29. Declare Dependencies class CoffeeMaker { @Inject Heater heater; @Inject Pump pump; ... } Thursday, November 8, 12
  • 30. Satisfy Dependencies @Module class DripCoffeeModule { @Provides Heater provideHeater() { return new ElectricHeater(); } @Provides Pump providePump(Thermosiphon pump) { return pump; } } Thursday, November 8, 12
  • 31. 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
  • 32. Neat features •Lazy<T> •Module overrides •Multibindings Thursday, November 8, 12
  • 33. 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
  • 34. 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
  • 35. Multibindings @Module class TwitterModule { @Provides(type=SET) SocialApi provideApi() { ... } } @Module class GooglePlusModule { @Provides(type=SET) SocialApi provideApi() { ... } } ... @Inject Set<SocialApi> Thursday, November 8, 12
  • 36. Introduction Motivation Using Dagger Inside Dagger Wrapping Up Thursday, November 8, 12
  • 38. Creating Graphs •Compile time •annotation processor •Runtime •generated code loader •reflection Thursday, November 8, 12
  • 39. Using Graphs •Injection •Validation •Graphviz! Thursday, November 8, 12
  • 40. 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
  • 41. 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
  • 42. Validation •Eager at build time •Lazy at runtime Thursday, November 8, 12
  • 43. dagger-compiler (cc) creative commons from flickr.com/photos/discover-central-california/8010906617 Thursday, November 8, 12
  • 44. javax.annotation.processing Built into javac Foolishly easy to use. Just put dagger-compiler on your classpath! Thursday, November 8, 12
  • 45. It’s a hassle (for us) •Coping with prebuilt .jar files •Versioning •Testing Thursday, November 8, 12
  • 46. ... and it’s limited (for you) •No private or final field access •Incremental builds are imperfect •ProGuard Thursday, November 8, 12
  • 47. ... but it’s smoking fast! (for your users) •Startup time for Square Wallet on one device improved from ~5 seconds to ~2 seconds Thursday, November 8, 12
  • 48. Different Platforms are Different •HotSpot •Android •GWT Thursday, November 8, 12
  • 49. HotSpot: Java on the Server •The JVM is fast •Code bases are huge Thursday, November 8, 12
  • 50. Android •Battery powered •Garbage collection causes jank •Slow reflection, especially on older devices •Managed lifecycle Thursday, November 8, 12
  • 51. GWT •Code size really matters •Compiler does full-app optimizations •No reflection. github.com/tbroyer/sheath Thursday, November 8, 12
  • 52. API Design in the GitHub age •Forking makes it easy to stay small & focused Thursday, November 8, 12
  • 53. 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
  • 54. ! 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
  • 55. Introduction Motivation Using Dagger Inside Dagger Wrapping Up Thursday, November 8, 12
  • 56. Guice •Still the best choice for many apps •May soon be able to mix & match with Dagger Thursday, November 8, 12
  • 57. Dagger •Dagger 0.9 today! •Dagger 1.0 in 2012ish •Friendly open source. Thursday, November 8, 12
  • 59. Questions? squareup.com/careers swank.ca corner.squareup.com jwilson@squareup.com Thursday, November 8, 12