I reread your original post, so I think I understand a little bit better what you're trying to do. My suggestion of passing input data directly through the call chain may not make sense since it sounds like you'd need to pass data through a number of different classes that don't care about that data, just to pass it to your leaf node. What you lose (to some degree) by using Guice or any DI framework is the ability to construct your objects from within your business logic, this is actually a good thing in the end (imo). In your case you could let Guice construct your A object, then use a setter to pass data from E to A, I think that's where you were going in your follow-up post, although I don't know if I would recommend that approach.
I don't think there's anything wrong with using RequestScoped variables if it fits your use-case. If the input data is really scoped to the HTTP request, then I think it would definitely make sense. If the input data comes directly from the HTTP request, then you can actually access the ServletRequest from your Guice module using an @Provides method, or implement a custom Provider<A> that gets the ServletRequest injected and pulls out the necessary data to construct A. You probably wouldn't want to put too much logic into the Provider implementation (like hitting a DB or something), but if its just extracting/manipulating the http request data, I've had good luck doing that. This page has some good examples if you haven't seen it already: http://code.google.com/p/google-guice/wiki/ServletModule and http://code.google.com/p/google-guice/wiki/ProviderBindings Not sure if this is much help to you, it's hard for me to advise without seeing the real world example. On Tuesday, April 2, 2013 10:09:09 AM UTC-5, caseybasichis wrote: > > Hi Scott, > > Thank you for you're reply. I haven't been able to figure this out. > > I get the request scope approach. I was thinking in terms of the session > scope, but request would be less like setting global variables to be picked > up. > > In the second solution, given a chain like D(C(B(A))) do you mean: > > Call 'D.input(value);' which in turn calls 'C.input(value)' all the way > up to A where it is needed. > > I know that is generally bad practice, but is it more or less required > here if making a Request scope global object is undesirable? > > Thank, > Casey > > > > On Monday, April 1, 2013 9:59:11 PM UTC-7, scott wrote: >> >> >> I'm not completely following your code example, but I think I have a >> basic understanding of your problem and I think I may have had similar >> issues in the past. You have an input parameter you want to pass down >> through your call chain, and you have an output you want to return back up >> your call chain. I don't think Guice really makes this anymore difficult, >> and it may have some things you can leverage to make it easier. >> >> How would you go about doing this if you weren't using Guice? If you're >> wanting to pass parameters to objects via their constructor, well you are >> delegating to Guice and your Guice modules to construct your object graph >> so your options are slightly limited. If you are in the context of >> receiving an HTTP request, you could use a @RequestScoped variable to store >> the data, then access the @RequestScoped variable to construct the leaf >> object. If you are not in the context of an HTTP request, but all the calls >> are happening in the same thread, then you could use a ThreadLocal variable >> to store the data. Or you could get fancy and implement a CustomScope, >> although this is generally not recommended and you would likely need to >> leverage ThreadLocal or some global/static mechanism to make it work >> anyway. In any of these cases, you will likely need to delay the creation >> of your leaf object until you've had a chance to set the input data (in >> RequestScoped or ThreadLocal), you can do this by using a Guice Provider to >> get an instance of the leaf object, then just make sure you call the >> Provider sometime after the input data has been set. >> >> Another (maybe too obvious?) solution, would be to just pass the input >> parameter via a typical method invocation, and return the output through >> the return. Is there a reason why you can't do that? You can separate >> constructing your system from using it by using Guice to construct your >> object graph, but then calling methods with parameters and return values to >> pass values up and down the call stack. >> >> >> >> On Saturday, March 30, 2013 11:21:36 AM UTC-5, caseybasichis wrote: >>> >>> Hi. >>> >>> So i realize this is a kind of circular dependency. >>> >>> How do you all go about taking a serial DI chain that takes an input and >>> output? >>> >>> The only method i've been able to come up with is sticking a session >>> scope object at the top of each chain so I can pass it a global value as an >>> input for it to send down the chain. This seems like an awful practice. >>> >>> On Thursday, March 28, 2013 8:19:46 AM UTC-7, caseybasichis wrote: >>>> >>>> Hi, >>>> >>>> class E { >>>> public: >>>> E(D); >>>> setLeafData(AData); >>>> getE(); >>>> }; >>>> >>>> class D { >>>> public: >>>> D(C) >>>> .. >>>> >>>> class A { >>>> public: >>>> A(); >>>> setAData(AData) >>>> getAData() >>>> } >>>> >>>> myE = inject->E >>>> >>>> myE.setLeafData("blah") >>>> myE.getE(); >>>> >>>> Here is a basic idea. It's not Java, but the concept is a fundamental >>>> question. >>>> >>>> On Thursday, March 28, 2013 7:52:05 AM UTC-7, Fred Faber wrote: >>>>> >>>>> Could you sketch a quick concrete example to show this in code real >>>>> quick? >>>>> >>>>> On Thu, Mar 28, 2013 at 10:29 AM, caseybasichis >>>>> <[email protected]>wrote: >>>>> >>>>>> Hi, >>>>>> >>>>>> I am struggling to understand how to give parameter information to >>>>>> leaf nodes, from the root node in a DI setup similar to Guice. >>>>>> >>>>>> I have a class that takes a set of A's and generates E's. >>>>>> (E(D(C(B(A))))) >>>>>> >>>>>> E is the injected, the rest are instantiated by the DI, A is an 'A >>>>>> provider' or 'A factory' >>>>>> >>>>>> E is the interface, how can I get a path, or other const data from a >>>>>> function in E to A, to start the function chain? >>>>>> >>>>>> In other words I have some simple data A, that gets processed through >>>>>> a function chain, down to an E class which generates an E_Data object >>>>>> which >>>>>> is then used in the root of my application. >>>>>> >>>>>> Do I string functions through E,D,C,B to get A's reference? That >>>>>> seems wrong. >>>>>> >>>>>> Reversing the chain make the path simple A(B(C(D(E))))), but the only >>>>>> way to get the output seems to be to attach a singleton to E like >>>>>> A(B(C(D(E(SQL)))))). >>>>>> >>>>>> That seems like inversion-of-inversion of control, but it also seems >>>>>> like a data manager could be made at the session scope that E could take >>>>>> as >>>>>> a constructor. >>>>>> >>>>>> What do I have flipped? >>>>>> >>>>>> Thanks, >>>>>> >>>>>> Casey >>>>>> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "google-guice" group. >>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>> send an email to [email protected]. >>>>>> To post to this group, send email to [email protected]. >>>>>> Visit this group at http://groups.google.com/group/google-guice?hl=en >>>>>> . >>>>>> For more options, visit https://groups.google.com/groups/opt_out. >>>>>> >>>>>> >>>>>> >>>>> >>>>> -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/google-guice?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
