Dagger for dummies

  • View
    145

  • Download
    1

Embed Size (px)

Text of Dagger for dummies

ABUSINGDEPENDENCY INJECTIONWITH DAGGERFOR DUMMIESSal Daz (@sefford)Freakend 15new Flipper(new Gintonic(new Gin(new Alcohol())))FlipperGintonicGinAlcoholSHOULD I USE DI WITH MY PERSONAL/PRO PROJECT?

BASIC DAGGER FOR DUMMIESFlipperGinAlcoholGintonicTonicTrollingCodeGoogleEngineerTearsAndroid@Module( includes = RosieModule.class, addsTo = {AndroidModule.class, DrinkModule.class,CodingModule.class } injects = FlipperFragment.class, complete = true,)public class FlipperModule {

@Provides @Named(Tuenti) public FlipperInterface provideFlipper(Gintonic drink, Trolling trolling, Android android) { return new FlipperImpl(drink, trolling, android); }}public class FlipperFragment extends Fragment {

ObjectGraph graph;

@Inject FlipperInterface flipper; @Override public void onCreate(Bundle bundle) { graph = ObjectGraph.create(new FlipperModule()); graph.inject(this); FlipperInterface flipper2 = graph.get(Flipper.class); }IDSPISPOPD FOR SINGLETONS@Singleton

By definition, Singletons cannot be tested

Provides same instance of class for Graph

This instance is not staticADVANCED DAGGER FOR DUMMIESLazy lazyFlipper;

Creates the instance on lazyFlipper.get()

Returns always the same instance (doh)

Always done in UIThread!LAZY INITIALIZATIONSPROVIDING INSTANCESProvider flipperProvider;

Can help building your own Flipper army

Returns a different instance each time@Module( override = true; injects = FlipperFragment.class, complete = true,)public class FlipperTestModule {

@Provides @Singleton public FlipperInterface provideFlipper() { return mock(FlipperInterface.class); }}MOCKING INSTANCESDYNAMIC (SCOPED) INJECTIONpublic ObjectGraph extend() { return graph.plus(new ExtensionModule1(), new ExtensionModule2());}

STATIC INJECTIONReduce boilerplate and mantenibility by annotating classes and constructions

Compilation-validated graph

SCOPED INJECTIONUsing a SOLID approach, change implementations on runtime

Graph runtime, validated

Risk of BOOOOOOM

ABUSING INJECTION FOR DUMMIESCLASSIC EXAMPLE@Module(injects = LoginActivity.class, addsTo = AppModule.class)// @author Antonio Leiva public class LoginModule { private LoginView view;

public LoginModule(LoginView view) { this.view = view; }

@Provides @Singleton public LoginView provideView() { return view; }

@Provides @Singleton public LoginPresenter providePresenter(LoginView loginView, LoginInteractor loginInteractor) { return new LoginPresenterImpl(loginView, loginInteractor); }}

WHY STOPPING THERE?public class NoNfcModule() { @Provides public NfcInterfaceController provideNfcController() { return new NullNfcController(); }}public class NfcModule() { @Provides public NfcInterfaceController provideNfcController( NfcAdapter adapter) { return new NfcController(adapter); }}// This goes on Api15Module@Provides@Singletonpublic StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI15Controller();}// This goes on Api19Module@Provides@Singletonpublic StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI19Controller();}// This goes on Api21Module@Provides@Singletonpublic StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI21Controller();}ONE MORE CUP OF ABUSEpublic class ActiveProfileModule() { @Provides public ProfilePresenterInterface provideProfilePres(...) { return new ActiveProfilePresenter(basePresenter,...); }}public class RegularProfileModule() { @Provides public ProfilePresenterInterface provideProfilePres(...) { return new RegularProfilePresenter(basePresenter,...); }}QUESTIONS?THANKS!