Alejandro Vidal Rodriguez
Agile Engineer @TribalScale
We are HIRING!!!!!
What is DI
What are we trying to solve
Building our own DI
Dagger
Structure a dagger project
Testing
App with no features but injection
Repository with steps by tags
git clone git@github.com:goofyahead/dagger2structureSample.git
git checkout step-1android {
....
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"
}
}
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
}
// Decoupling things
View <-> Presenter <-> RepositoryLots 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;
}the obscure side of reflection
@Dependency Stuff stuff;
public void method(A a) {
//magic happens
stuff.do(a);
}@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();
}
}
@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;
}
}
contact
goofyahead@gmail.com
https://www.linkedin.com/in/alejandrovidalrodriguez
https://medium.com/@goofyahead​
https://github.com/goofyahead