what to do with "normal" parameters in constructor

114 views
Skip to first unread message

ich du

unread,
Jul 8, 2011, 7:25:47 AM7/8/11
to google...@googlegroups.com
it is probably a general DI question but i guess i am not in wrong place.

i just make my first guice steps and i begin to like it, but i just stumbled on refactoring a non DI constructor to DI-style here is a general example:

before DI

public class MyClass {
    private C1 c1 = new C1impl();
    private C2 c2 = new C2impl();
    private SomeClass parameter;
    public MyClass(SomeClass parameter){
        this.parameter =parameter;
   ...
   }
...
}
 after DI:

public class MyClass {
    private C1 c1;
    private C2 c2;
    private SomeClass parameter;
    @Inject
    public MyClass(SomeClass parameter, C1 c1, C2 c2){
        this.parameter =parameter;
        this.c1=c1;
        this.c2=c2;
   ...
   }
...
}
Without "parameter" all would be fine (to get an instance of MyClass i would use a Provider). But how to get a provider of MyClass (i don't want parameter to be injected but all others).
new MyClass(parameter, providerC1.get(),providerC2.get()) does not look nice (i want to get rid of all "new"). setting "parameter" via a setter isn't a option if MyClass relies on a non-null "parameter". So what is best practice to solve this problem?

thx in advance

Fred Faber

unread,
Jul 8, 2011, 7:37:22 AM7/8/11
to google...@googlegroups.com
I think you're looking for assisted inject:

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/2abvXhxbxDYJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

ich du

unread,
Jul 8, 2011, 9:23:25 AM7/8/11
to google...@googlegroups.com, ffa...@faiser.com
that's right -Thanx!

are there any alternatives - besides the setter?
at first sight an additional interface + the binding is much boilerplate for a single parameter. - i thought i could reduce  boilerplate with DI ;-) (i have to admit that Guice helped to reduce boilerplate)

Fred Faber

unread,
Jul 8, 2011, 9:54:41 AM7/8/11
to google...@googlegroups.com
can you give an example of how you would create an instance?

ich du

unread,
Jul 11, 2011, 5:30:56 AM7/11/11
to google...@googlegroups.com, ffa...@faiser.com
in my special case i came to conclusion to refactor it. First i have to admit that my Problem was on Client Side: I have an "Activity" that needs the "Place" to get some tokens from it to be able to initialize it self. But now i get the place via PlaceController.getWhere(). And this is an "gined" instance - could be injected easily.
But i know i will need assisted injection soon. The Problem was that before you pointed to it i thought about self made alternatives (like field with setter). And now i am somehow confused if assisted injection should be used always (injecting all dependencies) or if there are use cases where it's better not to use assisted injection.

Reply all
Reply to author
Forward
0 new messages