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
-~----------~----~----~----~------~----~------~--~---