Open jakub-bochenski opened 1 month ago
For reference: https://stackoverflow.com/q/78966548/1237617
Off the top of my head, MyDataService
needs to be an @AssistedInject
constructor for a few reasons:
@Assisted
so that it knows which parameters
will be provided by the user (you can't base this off of just the type, e.g. with
@AssistedInject MyClass(@Assisted String, String)
Dagger wouldn't know which String
to inject and which is
assisted without the proper annotations.You could enable this on a provider method
@AssistedInject @Provides MyDataService myDataServiceProvider(@Assisted String foo, String bar) {
return myDataService(foo,bar)
}
That's a fair request.
I'll leave this open as a feature request for implementing an @AssistedProvides
equivalent of @AssistedInject
.
In the mean time, isn't a valid workaround here to define a class that uses the @AssistedInject
constructor, then provides a method which calls the static function and returns the instance (perhaps maintaining a reference to it if necessary)?
Still like the idea of @AssistedProvides
(if we do have that, it would also make sense to have an @AssistedBinds
as well IMO, I've got some instances where that would be useful (trying to keep the implementation of a class internal to some modules, but it requires some assisted parameters), but I can also see how that would be quite complicated, and @AssistedProvides
should also be able to be used in such a scenario anyway.
In the mean time, isn't a valid workaround here to define a class that uses the @AssistedInject constructor, then provides a method which calls the static function and returns the instance (perhaps maintaining a reference to it if necessary)?
Sure it's doable (as in "will work"), but the point is reducing the amount of boilerplate. I think it's not reducing boilerplate.
Personally in this case I just write the MyDataServiceFactory manually.
I have a factory method (e.g. provided by 3rd party, so I can't change it to constructor):
I would like to crate an assisted factory out of it
I don't think it's currently possible.
It seems to be this should be possible. I don't see why I should only be able to use assisted inject with constructors.