Status: New
Owner: ----

New issue 346 by aragos: AssistedInject:  Multi-type factories
http://code.google.com/p/google-guice/issues/detail?id=346

Assisted inject currently allows only such factory interfaces to be used
whose methods all return exactly one type.  In addition, that type can be
specified when initializing the factory
(FactoryProvider.newFactory(MyFactory.class, MyType.class)).

 From my experience, it would reduce boilerplate code greatly if (a) the
return type for a factory method could be inferred from its actual return
type, and (b) a factory interface can contain multiple methods that return
different types.

This is a proposal for a new factory provider that allows creation of
factories that fulfill both of the above definitions and still remains
flexible.  I dubbed these factories multi-type factories, and the
proxy-generator a MultiFactoryProvider.  A very early draft (neither fully
tested nor implemented, but illustrating some of the idea) can be found at:
  http://codereview.appspot.com/25084/show.  I'll update the draft as I add
more features/tests.

bind(MyFactory.class).toProvider(MultiFactoryProvider.newFactory(MyFactory.class));

Note that the draft currently does not contain the ability to specify
bindings for return types in module configurations, which would (probably)
look like this:

FactoryBinder factoryBinder = new FactoryBinder();
// Causes "Animal getAnimal(String name);" to use Dog(@Assisted String
name) constructor.
factoryBinder.bind(Animal.class).to(Dog.class);

// Causes "@Furry Animal getAnimal(String name);" to use Dog(@Assisted
String name) constructor.
factoryBinder.bind(Animal.class).annotatedWith(Furry.class).to(Cat.class);

bind(MyFactory.class).toProvider(MultiFactoryProvider.newFactory(AnimalFactory.class,
factoryBinder));

= or =

bind(MyFactory.class).toProvider(MultiFactoryProvider.newFactory(AnimalFactory.class,
new FactoryBinder(Animal.class, Furry.class, Cat.class)));

Please let me know what you think!

--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" 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-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to