google-code-export / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
2 stars 1 forks source link

Shortcuts in configuration #406

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
In a module configuration could it be possible to implement some shortcuts
? I don't find it straight to think about all workarounds for these
builders, since they are not standardized.

Typically I would think of such examples:

bind(MyInterface.class).named("blah").to(MyClass.class);
bind(MyInterface.class).toFactory(MyFactory.class);
Multibinder<Snack> setBinder = setBinderOf(Snack.class);
MapBinder<String,Snack> mapBinder = mapBinderOf(String.class,Snack.class);

being shortcuts for

bind(MyInterface.class).annotatedWith(Names.named("blah")).to(MyClass.class);
bind(MyInterface.class).toProvider(FactoryProvider.newFactory(MyFactory.class,
MyInterface.class));
Multibinder<Snack> setBinder = Multibinder.newSetBinder(binder(), Snack.class);
MapBinder<String,Snack> mapBinder = MapBinder.newMapBinder(binder(),
String.class, Snack.class);

This last methods require indeed the assisted injection package, but can't
it simply be documented that we need that jar-file in the case where we
plan to use the that method? If I remember well, this is possible in Java.

With this kind of writing, we can even add some extension like:

setBinderOf(Snack.class).named("snacks").toFactory(...);
or
Multibinder<Snack> setBinder = setBinderOf(Snack.class).annotatedWith(...);
// setBinder.whatever()

Original issue reported on code.google.com by ogregoire on 22 Jul 2009 at 1:08

GoogleCodeExporter commented 9 years ago
This adds unnecessary methods to the core API.  The core of Guice is designed &
intended to easy to follow and understanding, and focused on bindings.  
Extensions
add additional capabilities to it.  If you would like to create shortcuts to 
reduce
your typing, create some abstract module extensions that reduce your 
boilerplate code
and consider offering them as an additional extension to Guice.  If people find 
them
useful and begin to use them, then you've just created a great extension.

Original comment by sberlin on 2 May 2010 at 12:34