protip: If you frequently have to bind the same type with different
parameters, make yourself a helper method and get that TypeLiteral noise out
of your configure() methods.
static <T> TypeLiteral<IMyMapper<T>> mapper(Class<T> mapped) {
return new TypeLiteral<IMyMapper<T>>() {};
}
void configure() {
bind(mapper(Npc.class)).to(NpcMapper.class);
}
If you find yourself repeating the whole binding with different type
parameters, make a helper that does the whole binding.
You might find you want to make your own fluent binding DSL to keep your
modules clean. One of my projects has a lot of messaging plumbing that
needs to be set up, so I have something that looks like this:
new ChannelBinder(binder())
.in(Singleton.class)
.channel(MessageA.class)
.channel(MessageB.class, Names.named("hello"))
.channel(MessageC.class);
Each channel() call hides a PrivateModule with three or four bindings.
warp-servlet's binding DSL is a nice example to follow.
This IMO is one of the really fantastic benefits of Guice's Java-based
wiring over, say, XML-based wiring. It's trivial to abstract away
repetition and unclutter your view of the application.
On Tue, Apr 14, 2009 at 5:09 PM, jordi <[email protected]> wrote:
> that's a java defect or not provided feature. instead use
> TypeLiteral<http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/TypeLiteral.html>
>
> your code should be something like :
>
> TypeLiteral<ICache<Npc>> inpc = new TypeLiteral<ICache<Npc>>() {};
> TypeLiteral<Cache<Npc>> npc = new TypeLiteral<Cache<Npc>>() {};
>
> bind(inpc).to(npc);
>
> and similar for the other binding
>
> hope this helps!
>
> jordi
>
>
> On Tue, Apr 14, 2009 at 10:57 PM, aemami <[email protected]> wrote:
>
>>
>> Hi, I am trying to accomplish the following:
>>
>> bind(ICache<Npc>.class).to(Cache<Npc>.class);
>>
>> and
>>
>> bind(IMyMapper<Npc>.class).to(NpcMapper.class);
>>
>> This doesn't seem possible in Java, or is it?
>>
>> Keep in mind I'm not a java guy, I'm used to using Ninject (which was
>> patterned after Google Guice) and C#.
>>
>> 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
-~----------~----~----~----~------~----~------~--~---