Unfortunately, this problem has little to do with Guice as such, and more to do with your program design. You need to be able to decide *which* instance is injected in place of each of those three interfaces. If everything was in the same object graph then you can construct a special scope. If you had some other context (like the http session or request) then you could scope it similarly. Otherwise the idea is rather meaningless... =(
A common interface is probably a good idea. Dhanji. On Mon, Sep 7, 2009 at 5:37 AM, Ahab <[email protected]> wrote: > > Have recently started using DI and selected Guice to let us do this, > and liking it a lot. Dramatically improved our code structure & > style, without hardly any overhead. I have hit a problem however and > could do with some guidance. I am expecting that I have not read the > Guice docs correctly, am following very bad practice (!) or am re- > inventing the wheel. > > Does anyone else find that the closer your get the heart of your > application, the more you need to use classes that implement more that > one interface. For example you want a CashMachine that is a Runnable, > a CashDispenser and a CashDepositBox. If I use DI, via Guice, I am > encouraged to obviously get and reference all my objects as the > interfacve - which, by my book, is a very good practice. I want to do > this. However, if I create my CashMachine as a Runnable, it means I > cannot see it as a CashDispenser, but if I create it as CashDispense > I cannot see it as CashDepositBox. > > I know that in Guice if you have a singleton you can bind it to > multiple interfaces, but ONLY if it is a Singleton i.e. once the > object is created you will keep getting a reference to the same one > again and again. But what if you want to create an object on demand > but still retain these three interfaces onto the same object? > > ???? What is the best practice here? > > If people say "Don't use objects that implement multiple interfaces!" > then will be disappointed - seems like a lot to give up in order to > use DI. > > If the answer is to refer to your object as a CashMachine, I will be > even more disappointed as this very non-DI!! > > Thank you in advance for any thoughts - and hope someone can help me > out. > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
