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
On Thu, May 21, 2009 at 4:18 AM, Jigar Gosar <[email protected]> wrote:
>
> * 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 :)
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to