specialized version of the Inversion of Control pattern where the concern being inverted is the process of obtaining the needed dependency.” - Microsoft Dagger is Dependency Injection Framework!
validation (easier to follow through). • No use of reflection as a result code is easy to trace and better performance almost 13% compared to Dagger 1 (according to Google’s benchmark).
to evaluate location vendors, to see how accurately they can identify a store when user walks into it. • Code snippets in this presentation are from that application. • Code - https://github.kdc.capitalone.com/android-wallet/GratisPilot
of that class and requests to resolve it’s dependency. • Can only be applied to Field (non-private & non-static), Constructor and Method. • Injection order : Constructor, Field and Method. • Only one constructor of the class can be annotated.
your modules. • Publish binding – functionality used across the app. • Internal binding – all that are not-publish binding or dependency use to construct publish binding.
statusValidator; @Inject public DemoClass() { } } @Module public class ApplicationModule { @Provides DemoClass demoClass() { return new DemoClass(); } } statusValidator will be null
comes from the factory @Override public T get() { // double-check idiom from EJ2: Item 71 Object result = instance; if (result == UNINITIALIZED) { synchronized (this) { result = instance; if (result == UNINITIALIZED) { instance = result = factory.get(); } } } return (T) result; }
//Regular } @Provides public OkHttpClient providesFactualOkHttpClient(String factual_key) { // OAuth } Unsatisfied dependencies for type [OkHttpClient] with qualifiers… // In class @Inject OkHttpClient regularOkHttp;
NetworkModule.class }) public interface ApplicationComponent { // Members-injection void inject (GratisPilotApplication gratisPilotApplication); // Provision methods Context context(); Realm realm(); } • Allows Dagger to inject and resolve dependency of that class. • Generally class that you don’t control creation like Application, Activity will declared here.
public interface ApplicationComponent { // Members-injection void inject (GratisPilotApplication gratisPilotApplication); // Provision methods Context context(); Realm realm(); } • Scoped component forces @Modules and dependencies with @Inject to have same scope or no scope. • Unscoped components can not depend on scoped components.
and add modules with real or mock objects as needed. • Define a common interface without component annotation. • Build flavor specific components extending common interface.
and add modules with real or mock objects as needed. • Define a common interface without component annotation. • Build flavor specific components extending common interface. • Build flavor specific modules.
and add modules with real or mock objects as needed. • Define a common interface without component annotation. • Create flavor specific components extending common interface. • Create flavor specific modules. • Extend main application class for your flavor and overwrite method to build graph.
classA { @Inject DemoClass demoClassInClassA; } public class classB { @Inject DemoClass demoClassInClassB; } public class DemoClass { @Inject public DemoClass() { } } How many instance of DemoClass will be created? 2 Why? - Since the binding is not scoped, dagger will generate simple factory for this class that will return new object for every injection.
DemoClass { @Inject public DemoClass() { } } public class classA { @Inject DemoClass demoClassInClassA; } public class classB { @Inject DemoClass demoClassInClassB; } How many instance of DemoClass will be created? 1 Why? Dagger creates ScopedProvider.create(DemoClass_Factory.create()); which will maintain scope as long as new component is not created.
{ @Inject public DemoClass() { } } //Module @Provides DemoClass demoClass() { return new DemoClass(); } public class classA { @Inject DemoClass demoClass; } public class classB { @Inject DemoClass demoClass; } How many instance of DemoClass will be created? 2
{ @Inject public DemoClass() { } } //Module @Provides DemoClass demoClass() { return new DemoClass(); } How many instance of DemoClass will be created? 2 Tip: Binding provided by @Module takes precedence. public class classA { @Inject DemoClass demoClass; } public class classB { @Inject DemoClass demoClass; }
{ @Inject public DemoClass() { } } //Module @Provides @PerApplication DemoClass demoClass() { return new DemoClass(); } How many instance of DemoClass will be created? 1 public class classA { @Inject DemoClass demoClass; } public class classB { @Inject DemoClass demoClass; } Given - @Component is scoped with @PerApplication
{ @Inject public DemoClass() { } } //Module @Provides @PerApplication DemoClass demoClass() { return new DemoClass(); } How many instance of DemoClass will be created? 1 public class classA { @Inject DemoClass demoClass; } public class classB { @Inject DemoClass demoClass; } Given - @Component is scoped with @PerApplication
{ @Inject public DemoClass() { } } //Module @Provides @PerActivity DemoClass demoClass() { return new DemoClass(); } How many instance of DemoClass will be created? public class classA { @Inject DemoClass demoClass; } public class classB { @Inject DemoClass demoClass; } Error – Binding in module can be without scope or same scope as Component. So here it can only be @PerApplication or nothing. Given - @Component is scoped with @PerApplication
Context context; //@Inject public LogExporter(Context context) { this.context = context; } } How many instance of LogExporter will be created? Error - Cannot be provided without an @Inject constructor or from an @Provides- annotated method.