2

I have large Android ViewModel classes that tend to have a lot of dependencies (most of them are DAOs from Room, one per SQLite table). Some have more than 10 dependencies.

This is fine but the @Inject constructor is bloated with arguments, and contains only boilerplate code to set the injected members from the constructor arguments.

I wanted to switch to "regular" injected members, identified individually with an @Inject annotation, like other (dumb) classes.

This fails for Android related classes (although ViewModels are advertised as non-Android dependent, e.g. they don't use the Android framework) such as activities and fragments.

The workaround for that is to use a factory, which is injected from the Application class using the nice HasActivityInjector, HasServiceInjector, etc. interfaces.

Dagger doesn't provide any HasViewModelInjector, so if I persist in injecting members individually instead of injecting the constructor, here's what I'm given:

error: [dagger.android.AndroidInjector.inject(T)] XXXViewModel cannot be provided without an @Inject constructor or from an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.

If I create a module that has a @Provides annotation to create the ViewModel, this doesn't inject individual members.

Did I miss something (my last sentence is what's most important in my question) or is it simply not possible to inject members, and I have to inject the constructor?


A bit of code.

What I want:

class MyViewModel extends ViewModel {
    @Inject
    MyDao myDao;
}

versus what I need to do:

class MyViewModel extends ViewModel {
    private final MyDao myDao;

    @Inject
    MyViewModel(MyDao myDao) {
        this.myDao = myDao;
    }
}

First block of code (what I want) requires this method in a module:

@Provides
MyViewModel provideMyViewModel() {
    return new MyViewModel();
}

but in this case the myDao field is null. How to inject the @Inject-annotated members?

I want to avoid the use of the 2nd block of code, which tends to create a huge constructor bloated with many arguments, should I need to inject a lot of members.

3
  • I have read this nice answer that explains that we should Always prefer constructor injection or provide it from a module where you can do additional setup steps; however this doesn't cover the member injection. Apr 23, 2018 at 23:45
  • You can use my method, stackoverflow.com/a/53956997/7558125 Dec 28, 2018 at 10:36
  • @PratikMhatre: thanks, but I switched to Koin, which has a much simpler API. Dec 28, 2018 at 13:35

1 Answer 1

4

There are multiple ways of injection and I think you are referring to field injection. Field injection, unlike constructor injection, must be triggered manually. To do that, define a method in your component with the view model as parameter.

void inject(ViewModel viewModel)

And then call this method from your view model constructor perhaps.

class MyViewModel extends ViewModel {
    private final MyDao myDao;

    @Inject
    MyDao myDao;

    public MyViewModel() {
        MyComponent mycomponent = DaggerMyComponent.....
        myComponent.inject(this);
    }
}
2
  • Nice, I knew that I needed to inject manually, I just couldn't figure it out. Apr 24, 2018 at 6:09
  • What I actually did is that I edited the InjectableViewModelFactory (that is found everywhere online) to be injected with the application instance (so that I can access the application component), and then call application.getComponent().inject((MyViewModel) creator.get()). Your answer greatly helped me, thanks! Apr 24, 2018 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.