dagger2-example
dagger2-example copied to clipboard
Remove boiler plate code by creating more boilerplate code?
So we've actually tried to use this with our android project, in a common android application you would have many activities and fragments. It seems you need to add an inject method for each of those activities now? Or am i using this incorrectly? Same thing with adapters etc, we have to add inject method for every adapter subclass we create.
This seems to create more issues than it's solving
Hi @martintreurnicht Give it a try: https://speakerdeck.com/jakewharton/dependency-injection-with-dagger-2-devoxx-2014 It could open your mind ;)
We've implemented it on a branch, but we still have that exact issue. You can have injections on super classes, everything needs to be on the base class or everything needs to be on the super class, which every class is passed to the injection method, this doesn't seem like a good solution. We've had a huge bug for days where we just couldn't figure out why one of the IntentService classes didn't want to run it's injections, then we realized that the injection method we were using was for the super class and thus it was processing injections on the super level not the base class. This can cause stupid errors in your code that dagger doesn't even warn you about. We've actually written our own injection previously that uses reflection, it works pretty well but we wanted to use dagger2 so that we don't have to support it anymore but damn how i miss how easy it is to use that framework compared to dagger2
Instead of having all your activities in a module:
@Module(
injects = { Activity1.class, Activity2.class }
)
you can now have them in the component:
public interface ApplicationComponent {
void inject(BaseActivity object);
}
and have all your activities extend BaseActivity
public class BaseActivity extends Activity {
@Inject protected Something something;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((Application) getApplication()).getComponent().inject(this);
}
}
If you need another injected object in Activity3
, add it in the component:
public interface ApplicationComponent {
void inject(BaseActivity object);
void inject(Activity3 object);
}
public class Activity3 extends BaseActivity {
@Inject protected AnotherThing anotherTing;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((Application) getApplication()).getComponent().inject(this);
something.ok();
anotherThing.ok();
}
}
It would be nice to have some generator for all those things. To avoid creating tons of inject() methods manually
@deviant-studio Honestly, with Intellij/Android Studio it takes 3 keystrokes to create an inject()
method. It doesn't seem so time consuming to me.