an activity/fragment via a constructor. • If fragment, it’s not really best to pass via newInstance() fragment pattern, either. • A custom setter method is an option, but the “two-step” construction is an anti-pattern.
guidance from Bob Lee) • Initially targeted resource constrained environments, such as Android. • Try to solve the configuration problems of guice (fail at compile time, not runtime).
at Google. • Improve performance through code generation. ◦ This allowed for better ProGuard support. • Strong focus on traceability - knowing exactly why a configuration error has occurred.
public final class DaggerCoffeeComponent implements CoffeeComponent { private CoffeeModule coffeeModule; ... @Override public CoffeeMaker maker() { return new CoffeeMaker( Preconditions.checkNotNull( coffeeModule.providesHeater(), "Cannot return null from a non-@Nullable @Provides method"), Preconditions.checkNotNull( coffeeModule.providesPump(), "Cannot return null from a non-@Nullable @Provides method")); } public static final class Builder { … } }
public final class DaggerCoffeeComponent implements CoffeeComponent { private CoffeeModule coffeeModule; ... @Override public CoffeeMaker maker() { return new CoffeeMaker( Preconditions.checkNotNull( coffeeModule.providesHeater(), "Cannot return null from a non-@Nullable @Provides method"), Preconditions.checkNotNull( coffeeModule.providesPump(), "Cannot return null from a non-@Nullable @Provides method")); } public static final class Builder { … } }
public final class DaggerCoffeeComponent implements CoffeeComponent { private CoffeeModule coffeeModule; ... @Override public CoffeeMaker maker() { return new CoffeeMaker( Preconditions.checkNotNull( coffeeModule.providesHeater(), "Cannot return null from a non-@Nullable @Provides method"), Preconditions.checkNotNull( coffeeModule.providesPump(), "Cannot return null from a non-@Nullable @Provides method")); } public static final class Builder { … } }
public final class DaggerCoffeeComponent implements CoffeeComponent { private CoffeeModule coffeeModule; ... @Override public CoffeeMaker maker() { return new CoffeeMaker( Preconditions.checkNotNull( coffeeModule.providesHeater(), "Cannot return null from a non-@Nullable @Provides method"), Preconditions.checkNotNull( coffeeModule.providesPump(), "Cannot return null from a non-@Nullable @Provides method")); } public static final class Builder { … } }
over singleton creation. • You can only have one Custom Scope per component. • We could create application scoped instances, but it would be more performant to only create/keep objects when you need them. (think “local” singletons)
and keep scopes separate. • Child component is added to parent component. • Because you are adding the child component to the parent, you must manage the life of the child component.
modular, and testable. • The hardest part of Dagger 2 is understanding what is happening under the covers. • Dagger 2, for all of it’s quirks, is still one of the most performant DI frameworks.