This is usually a thing you don't do with dependency injection.
Creating a concrete instance depending on some input is what you usually do with a factory pattern [1].

What you could do in guice is:
use multibinders to bind multiple implementations of the Transformer interface [2].
You would then loop over all Transformer implementations and ask each one of them if they can handle the curve type requested in the file name.

This way you have a plug-in like architecture where it is possible to add more implementations of Transformer at a later time.
Be aware that guice does not guarantee an order of the bound Transformer instances. If order matters you would have to sort the Set or Map of Transformer yourself.
In this approach guice does not make any decision on which implementation to use. Rather it offers you all implementation it knows and you must choose which one is appropriate.


[1] http://www.oodesign.com/factory-pattern.html
[2] http://code.google.com/p/google-guice/wiki/Multibindings



On 04/03/2014 06:29 AM, Dale Preston wrote:
I'm working on an application that I hope to use Guice with for dependency injection.  Up to now, I've simply used factory classes but I'd like to experience Guice and see if I can do a better job that way.  The application receives commodity price messages in a flat file, processes them using rules based on the type of price curve desired (coded in the file name), and sends the prices to a trading platform.  So, based on content in the file name, I inject into my MessageProcessor a Transformer (interface) implementation specific to the curve type.  

Everything I see in the documentation and getting started is around providing only one choice of a concrete class for a dependency, not a class based on a conditional.  Can anyone point me to or give an example of how or where in the Guice framework I make these choices?

Thanks,

Dale
--
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 google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.

--
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 google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.

Reply via email to