Guice newbie question: creating N objects on-demand

2008-10-16 Thread Andrew Clegg
Hey folks, I'm just getting started with Guice (and DI in general). I have a question, hope it's not too dense. I have a method that looks something like this in its first incarnation (_predictors is a collection): public boolean dispatch() { for( PredictorProfile predictor :

Re: Guice newbie question: creating N objects on-demand

2008-10-16 Thread Bob Lee
Assuming you need to inject stuff into DispatchHandler and you don't want Dispatcher to know about DispatchHandler's deps, here's how I would do it. public class Dispatcher { private final DispatchHandler.Factory handlerFactory; // package-private for unit testing @Inject

Re: Guice newbie question: creating N objects on-demand

2008-10-16 Thread Andrew Clegg
Okay... You took me by surprise a bit, I was expecting something based on ProviderDispatchHandler. Like injecting a ProviderDispatchHandler as one of the arguments to the Dispatcher constructor (or a setter). Have I got the wrong end of the stick? Is that not what Providers are meant for?

Re: Guice newbie question: creating N objects on-demand

2008-10-16 Thread Bob Lee
Providers work when everything is injected, but in this case, you have a run time argument. Mixing injected dependencies with run time arguments in a statically typed-fashion requires you to roll your own factory/builder. You could just add a predictor setter to DispatchHandler and then just

Re: Guice newbie question: creating N objects on-demand

2008-10-16 Thread jordi
i was about to tell you also about injecting ProviderDispatcherHandler when i saw Bob's solution.. I guess if you inject that provider you can't tell him about the Predictor, at least at construction time.. jordi On Thu, Oct 16, 2008 at 6:24 PM, Andrew Clegg [EMAIL PROTECTED]wrote: Okay...

Re: Guice newbie question: creating N objects on-demand

2008-10-16 Thread Andrew Clegg
Well the predictors are being handed to the dispatcher like this: @Inject public int registerPredictors( PredictorProfileList predictors ) { if( _predictors == null ) _predictors = predictors; else _predictors.addAll( predictors );

Re: Guice newbie question: creating N objects on-demand

2008-10-16 Thread Bob Lee
On Thu, Oct 16, 2008 at 9:42 AM, Andrew Clegg [EMAIL PROTECTED]wrote: Granted that would disguise the fact that DispatchHandlers are Threads but is that necessarily a bad thing? Or is there another reason to avoid doing it this way? Thanks for all the feedback -- much appreciated... That

Re: Guice newbie question: creating N objects on-demand

2008-10-16 Thread Andrew Clegg
2008/10/16 Bob Lee [EMAIL PROTECTED]: That looks fine to me. You've effectively inverted the design, turning DispatchHandler into a thread factory. Great, that's roughly how I was intending it to begin with, hence not wanting another level of indirection in there really. Nice one for putting

Re: Guice newbie question: creating N objects on-demand

2008-10-16 Thread Bob Lee
Oh, Jesse also has AssistedInject which can help with this stuff, but I still like to kick it old school. He can tell you more. Bob On Thu, Oct 16, 2008 at 9:16 AM, Bob Lee [EMAIL PROTECTED] wrote: Assuming you need to inject stuff into DispatchHandler and you don't want Dispatcher to know