Dependency Injection

About me

Alejandro Vidal Rodriguez
Agile Engineer @TribalScale

We are HIRING!!!!!

Agenda

  • What is DI

  • What are we trying to solve

  • Building our own DI

  • Dagger

  • Structure a dagger project

  • Testing

Lets build the best app ever

App with no features but injection

Repository with steps by tags


    git clone git@github.com:goofyahead/dagger2structureSample.git

    git checkout step-1

Unleash the beast

android {
    ....
    dependencies {
        compile "com.google.dagger:dagger:$DAGGER_VERSION"
        annotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
        compile "com.google.dagger:dagger-android:$DAGGER_VERSION"
        compile "com.google.dagger:dagger-android-support:$DAGGER_VERSION"
    }
}

Well done!

What is DI


Engine engine = new Engine();
List<Wheel> wheels = new LinkedList<>;
wheels.add(new Wheel());
Color color = New Color("red");
Brake brakes = New DiskBrakes();

Car myCar = new Car(engine, wheels, color, brakes);

// I just want a car, can someone give it to me?
Car myCar = CarFactory.giveMeACar();

with

or


@Inject Car car;

@Inject
public Car (engine, brakes, ...) {
    //assign
}

@Inject
public void generateCar(engine, brakes, ...) {
    //use & assign
}

What are we trying to solve?


// Decoupling things

View <-> Presenter <-> Repository

Lots of unnecessary passing down objects and relying on static methods and classes


public method(Thing one, Another stuff) {
    Helper.doSomething(one, stuff);
}


//// static helper class

public static doSomething(Thing one, Another stuff){
    dependency.process(one);
    anotherDependency.process(stuff);
    return 1;
}

Lets do our own DI!

Our own anotations

the obscure side of reflection

@Dependency Stuff stuff;

public void method(A a) {
    //magic happens
    stuff.do(a);
}

Structure of a dagger project

@Module
public abstract class UserListModule {

//    @Provides
//    static UserListContract.View provideView(){
//        return new UserListActivity();
//    }

    @Binds
    public abstract UserListContract.View bindView(UserListActivity mainActivity);

    @Provides
    static UserListContract.Presenter providePresenter(UserListContract.View view) {
        return new UserListPresenter(view);
    }
}
@Component(modules = {
        AndroidInjectionModule.class,
        ContextModule.class,
        ActivityModule.class
})
public interface BaseComponent extends AndroidInjector<DaggerApplication> {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);
        BaseComponent build();
    }
}

careful there...

I'm in love!!

How to do testing!

@Module
public class FlombulatorTestModule {

    @Provides
    Flumbolator provideTestFlombulator() {
        System.out.println("I the mocked flumbolator have been summoned, behold my mock power");
        Flumbolator flum = Mockito.mock(Flumbolator.class);
        when(flum.frumbolateMe()).thenReturn("flumbolate test");
        return flum;
    }
}

Mock everything

public class TestApplication extends Application implements HasDispatchingActivityInjector {
    @Inject
    DispatchingAndroidInjector<Activity> dispatchingActivityInjector;

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("HEY IM UP TESTING APPLICATION COMPONENT ==========================+>");
        DaggerTestApplicationComponent.create().inject(this);
    }

    @Override
    public DispatchingAndroidInjector<Activity> activityInjector() {
        return dispatchingActivityInjector;
    }
}

Beware of responsibility

Questions?

contact

goofyahead@gmail.com

https://www.linkedin.com/in/alejandrovidalrodriguez

https://medium.com/@goofyahead​

https://github.com/goofyahead

Dependency injection

By Alejandro Vidal Rodriguez

Dependency injection

  • 33