Essentially what you have here is code duplication, which, as you point out, can easily be solved using regular refactoring techniques. So that would be the first and probably best option. This is also one of the things I like about Guice. It recognizes that configuration is also code, and you can refactor it easily.
Besides that, you could try overriding bindings using Guice 2.0 (trunk) and http://code.google.com/p/google-guice/source/browse/trunk/src/com/google/inject/util/Modules.java Hope this helps Robbie On Mon, Sep 8, 2008 at 11:33 PM, Mikkel Petersen <[EMAIL PROTECTED]> wrote: > > Sorry I'm not sure I'm explaining me well enough but the important > thing is that > > binder.install(new PersonModule("Bob")) > binder.install(new PersonModule("Bill")); > > will fail because both modules makes a binding to a Phone, even though > the Phone binding is only important inside each PersonModule. > > > > On 8 Sep., 22:55, "Logan Johnson" <[EMAIL PROTECTED]> wrote: > > What do you want to happen here? One Phone that's shared among your > Person > > instances, or each Person gets its own Phone, or something entirely > > different? > > > > On Mon, Sep 8, 2008 at 4:48 PM, Mikkel Petersen <[EMAIL PROTECTED]> > wrote: > > > > > I dont know if this makes sense, but there seems to be a problem when > > > you need multiple instances of > > > the same object, created using the same modules. > > > For example: > > > you have the mainmodule and the sub module will create the same kind > > > of object, only with a slight difference. > > > They will bind to different places in the application though. > > > > > The object sto inject into : > > > class MyApp { > > > @Inject @Named("Bob") > > > Person person1; > > > @Inject @Named("Bill") > > > Person person2; > > > } > > > > > class MyPerson { > > > String name; > > > @Inject Phone phone; > > > } > > > > > The main module : > > > //install other modules > > > binder.install(module1); > > > binder.install(module2); > > > //install person modules > > > binder.install(new PersonModule("Bob")) > > > binder.install(new PersonModule("Bill")); > > > > > The person module: > > > //..configure person > > > class PersoModule { > > > String name; > > > public PersonModule(String name) { > > > this.name = name; > > > } > > > configure(Binder binder) { > > > //configure other stuff > > > binder.bind(Phone.class).toInstance(new CellPhone()); > > > //bind person > > > > > > binder.bind(Person.class).annotatedWith(Names.named(name).toInstance(new > > > EmployeePerson(name); > > > } > > > } > > > > > The binding will fail because > > > binder.bind(Phone.class).toInstance(new CellPhone()); > > > > > Will be called twice. I know that it could be bind once in the main > > > module, but this is a simple example, imagine much more complicate > > > configuration in the PersonModule. > > > Problem is that what goes on after binder.install() always touches the > > > calling module, even though in this case, the Phone binding is only > > > relevant for the PersonModule and the objects created here. > > > I'd like a command like bindLocally : > > > > > binder.bindLocally(Phone.class).toInstance(new CellPhone()); > > > > > That binding will only touch objects created in this module, not any > > > calling modules or modules called. > > > > > I tried to look at scopes but it doesnt seem to have anything to do > > > with this. > > > > > Thanks. > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
