-
Notifications
You must be signed in to change notification settings - Fork 55
MembersInjector for late injection #130
Comments
This is not possible with the library, no. But you don't really need a library to do this. You can members inject the instance and then set whatever other fields you want directly. The reason assisted inject only works for constructor injection is that you cannot ask Dagger to partially call a constructor and then partially supply the other arguments. |
But. public final class TheClass_AssistedFactory implements TheClass.Factory {
private final Provider<ConstructorDep> constructorDep;
private final MembersInjector<TheClass> membersInjector;
@Inject
public TheClass_AssistedFactory(
Provider<ConstructorDep> constructorDep,
MembersInjector<TheClass> membersInjector) {
this.constructorDep = constructorDep;
this.membersInjector = membersInjector;
}
@Override
public MViewModel create(AssistedDep assistedDep) {
TheClass obj = new TheClass(
constructorDep.get(),
assistedDep);
membersInjector.injectMembers(obj);
return obj;
}
} Nice thing about this is, that if you don't have any Or shouldn't there be at least some warning? |
Oh, you're saying you want members injection to be performed on constructor-injected types? I see. I suppose that's reasonable for consistency of behavior. I thought you wanted assisted injection on a type that was solely members injected. Can I ask why you are doing this? Members injection is a truly terrible thing. |
I agree, members injection seems like a terrible thing. The reason is android ViewModel with inheritance:
I know, that it seem like a terrible thing, but also if (for some reason) you have members injection without inheritance, it's the same - no warning, crashes at runtime. I was checking briefly how the MembersInjector works and for specific class it includes even parent MembersInjector, e.g for This means, that injecting |
So one trouble here is that AssistedInject is not Dagger-specific and thus cannot depend on the |
I see the issue. I was thinking, that it would be possible to extend the pure AssistedInject processor to generate the code, but then not sure, how to pass the information about what to generate. a) pass some data from module to module somehow? I don't like either option, but not sure how else could be solved |
FYI this is no longer a problem as AssistedInjection has moved to Dagger and all that's left is inflation injection. We can add special handling for |
AssistedInject only works for construction injection.
Dagger on the other hand is able to inject stuff after construction with
*_MembersInjector
for some@Inject lateinit var dep: Dependency
.Is it somehow possible to have this for AssistedInject?
The text was updated successfully, but these errors were encountered: