You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Provide constructor injection and setter injection (?) #204
We currently force the users to have at least package private (default) scopes for their fields. Some users might not appreciate that. We could allow constructor injection / setter injection, this way :
I vote for it. Great idea.
Le 25 mai 2012 16:54, "Pierre-Yves Ricau" < reply@reply.github.com>
a écrit :
We currently force the users to have at least package private (default)
scopes for their fields. Some users might not appreciate that. We could
allow constructor injection / setter injection, this way :
additionally there should be only one bean/view/whatever set by one setter (with the usual void return)
potential cause of unexpected NPE: not having an ensured injection order might be a source of bugs if one does more than setting a value in an injection method (e.g. accessing another, not yet set, value)
constructor
might be a really nice feature as it would allow to have final beans
but i would not call it "setter injection" but rather "method injection" allowing any name for the method and use the parameter name as default for annotation values like ViewById.resName(). this would allow usage as advanced @AfterXXX annotations as you could have a snippet like this
@Extra("MY_EXTRA_KEY")
voidreactOnExtra(StringextraValue) {
// do stuff with extraValue
}
that currently has to be
@Extra("MY_EXTRA_KEY")
StringextraValue;
@AfterExtrasvoidreactOnExtra() {
// do stuff with extraValue
}
while the code saved is minimal in this example the upper way would not pollute the class scope with a variable only used in one method.
but as mentioned above, this could also cause unexpected issues (mostly NullPointerExceptions) when not used carefully. one should only use this as setter or only work with the value that got injected or are otherwise ensured to be set.
8 months without feedback. I just started to play with method injection for @Bean. I have a working sample. I also tested with multiple values to inject. while that worked, there are issues with the ability to provide a implementation class in the @Bean.value() attribute. so i think it is the best to stay with the one injected element per method approach mentioned above.
@WonderCsabo actualy i'm not sure why i wrote that. iirc there was the discussion if we want to have annotations on a parameter and we decided to have them after this comment.
@dodgex it is a good idea. :) I see no problems with it. Actually i think it is much more natural then the method annotation. This is used by every DI system.
Yeah, the param, of course. Becase the value actually will be injected to the parameter. Just like with @Receiver.Extra, or @Inject in guice. Also, this way you can inject several parameter to the same method even when you use Bean.value().
they use the same call/return value. but in this case it is the other way. we would need to have 1 call with multiple parameters. but yeah, that should be doable in a similar way...
I guess the problem that we already registered the same annotation for fields. For now create a new annotation (@BeanSetter), we can solve the architecture issue later.
Activity
mathieuboniface commentedon May 25, 2012
I vote for it. Great idea.
Le 25 mai 2012 16:54, "Pierre-Yves Ricau" <
reply@reply.github.com>
a écrit :
pyricau commentedon Oct 19, 2012
Note: I'm note sure what's best, the setter annotation could also be directly on the setter instead of being on the argument :
The latter is more common, but the former gives more flexibility.
giejay commentedon Mar 11, 2013
Any progress on this isue?
mathieuboniface commentedon Mar 11, 2013
Hi @giejay,
This feature is not scheduled for 3.0, so I guess not any work has been started on this.
However, you're really welcome to contribute :)
dodgex commentedon Oct 21, 2014
i had some thoughts about this issue.
setter
constructor
finalbeanssuper()might require static helper methods somewhere).I think i'd give the setter injection via annotation on method a try with code like @pyricau suggested.
but i would not call it "setter injection" but rather "method injection" allowing any name for the method and use the parameter name as default for annotation values like
ViewById.resName(). this would allow usage as advanced@AfterXXXannotations as you could have a snippet like thisthat currently has to be
while the code saved is minimal in this example the upper way would not pollute the class scope with a variable only used in one method.
but as mentioned above, this could also cause unexpected issues (mostly NullPointerExceptions) when not used carefully. one should only use this as setter or only work with the value that got injected or are otherwise ensured to be set.
dodgex commentedon Jun 7, 2015
8 months without feedback. I just started to play with method injection for
@Bean. I have a working sample. I also tested with multiple values to inject. while that worked, there are issues with the ability to provide a implementation class in the@Bean.value()attribute. so i think it is the best to stay with the one injected element per method approach mentioned above.WonderCsabo commentedon Jun 7, 2015
Sorry, @dodgex i did not saw this issue.
Why? We already have multiple annotations working just fine, and i just added two more.
dodgex commentedon Jun 7, 2015
@WonderCsabo actualy i'm not sure why i wrote that. iirc there was the discussion if we want to have annotations on a parameter and we decided to have them after this comment.
btw: if you want to see the current implementation you can see it here: https://github.com/dodgex/androidannotations/tree/204_method_injection
WonderCsabo commentedon Jun 7, 2015
@dodgex it is a good idea. :) I see no problems with it. Actually i think it is much more natural then the method annotation. This is used by every DI system.
dodgex commentedon Jun 7, 2015
@WonderCsabo uhm, i'm not sure if i understand your comment. what is more natural? annotation on param?
WonderCsabo commentedon Jun 7, 2015
Yeah, the param, of course. Becase the value actually will be injected to the parameter. Just like with
@Receiver.Extra, or@Injectin guice. Also, this way you can inject several parameter to the same method even when you useBean.value().dodgex commentedon Jun 7, 2015
okay. shall i create something like
@Bean.Implas parameter annotation? that way i could implement multiple beans per method injection.WonderCsabo commentedon Jun 7, 2015
What is the problem with this?
😕
4 remaining items
WonderCsabo commentedon Jun 7, 2015
You can check out how
@ViewByIdand@Clickworks. Actually they are reusing the samefindViewById()call, if it is possible.dodgex commentedon Jun 7, 2015
they use the same call/return value. but in this case it is the other way. we would need to have 1 call with multiple parameters. but yeah, that should be doable in a similar way...
WonderCsabo commentedon Jun 7, 2015
Yeah, but the idea is the same about lazy generating the method call itself.
BTW, you can check out google/dagger and transfuse projects to get some ideas.
dodgex commentedon Jun 7, 2015
hmm for some reason the annotations on param only get validated, but not processed. do you have an idea why?
WonderCsabo commentedon Jun 7, 2015
I guess the problem that we already registered the same annotation for fields. For now create a new annotation (
@BeanSetter), we can solve the architecture issue later.dodgex commentedon Jun 7, 2015
i debugged it: the problem is that the ModelProcessor is currently not able to handle method parameters.
WonderCsabo commentedon Jun 7, 2015
Eh, sorry. Our current paramter handlers do nothing, the "parent" method handler is doing the processing actually.
We should modify ModelProcessor. We should also modify it to be able to use the same annotation on different element types.
dodgex commentedon Jun 7, 2015
i quickly hacked it a bit and now it processes the annotations, regardless of thier location (field, method or param)
dodgex commentedon Jun 7, 2015
its working! :)
dodgex commentedon Jun 7, 2015
this one works too:
currently this generates one instance of the bean per injection...
WonderCsabo commentedon Jun 7, 2015
Great work! 👍 Maybe this is the time to open a WIP PR, because we are not specifying the feature, but the implementation.
dodgex commentedon Jun 7, 2015
doing some rough cleanup before creating PR. some parts of the code are a bit hacky ;)
WonderCsabo commentedon Dec 20, 2015
Setter injection is implemented. Constructor injection only makes sense in case of beans. If there is need for that, we will open a new issue.