How to inject dependencies when creating objects via reflection..

327 views
Skip to first unread message

Jigar Gosar

unread,
May 21, 2009, 4:18:58 AM5/21/09
to google-guice
* How Injecting dependencies when creating "objects" via reflection
and the "object" also has a constructor parameters.

* details:

I have a Hierachy (tree) of classes.

class Base{
Base(String s){...}
}

class Child1{
Child1(String s){super(s)}
}

class Child2{
Child1(String s){super(s)}
}


and so on, not that class Child1 also has its own children.


* Till now I was creating the classes via reflection and invoking the
string parameter constructor.

* now the base class has a dependency that I want guice to inject.

class Base{
Base(String s, Dep d){...}
}

class Child1{
Child1(String s, Dep d){super(s)}
}

class Child2{
Child1(String s, Dep d){super(s)}
}

* I have read up on assisted inject, but am not sure on how to use is
to solve this problem. Since I am using reflection to to create the
objects of this class Hierarchy.

* Note: the reason I am using reflection to create objects is because
the type of the object to be instantiated is not known till runtime. I
receive strings from socket and need to wrap them into objects before
processing them (I have simplified the actual requirement for sake of
brevity).

* so the broader question is how do I create objects of class whose
type is not known till runtime and it has dependinecies and also
constructor parameters.

* and kudos for the 2.0 release :)

David Stenglein

unread,
May 21, 2009, 3:00:10 PM5/21/09
to google...@googlegroups.com
Jigar,

AssistedInject allows you to have a single factory interface for
creating all of your instances, even if they have different
dependencies in the constructor arguments. As long as the other
arguments can be supplied through Injection, it doesn't matter how
many there are; there just needs to be a match between the arguments
of the create(...) method of your factory interface and the Assisted
arguments of each types constructor.

Given this, all you need is some way to call up the factory for an
instance you need and you can get that instance without reflection.

The grouping of all of your implementations could be done with a
MapBinder (http://code.google.com/p/google-guice/wiki/Multibindings).
All you need to do is choose a key to select your implementation
(which you probably have already).

-Dave




-Dave

http://code.google.com/p/google-guice/wiki/Multibindings

Alen Vrecko

unread,
May 21, 2009, 3:16:18 PM5/21/09
to google-guice
Here are some examples of using Assisted Inject and Map Binder

http://groups.google.com/group/google-guice/browse_thread/thread/3c86c7712deae803/50bd5af95622e49b

Maybe it will give you some ideas.

Cheers,
Alen

Jigar Gosar

unread,
May 22, 2009, 12:28:57 AM5/22/09
to google...@googlegroups.com
Thanks Alan and David for replying.

* I knew about assisted inject, but had not thought about using a
collection to map classes to factories created by assisted inject
inside the module itself.

* thanks a lot this really solved my problem and I don't have to
resort to reflection.


Thanks again.
--
Jigar Gosar
http://jigar.org/blog
http://simplegtd.com
http://madcow.tumblr.com
Reply all
Reply to author
Forward
0 new messages