I'm building some kind of framework and the developers have to specify the type of server they want to use. The
IServer<?> is the interface of the server : it is parametized.
The framework itself only needs the wildcard version of the
IServer<?> instance to be injected in its classes. In fact, it doesn't even know the type of the Server that will actually be used. So :
bind(new TypeLiteral<IServer<?>>(){}).to(getServerClass()).in(Scopes.SINGLETON);
The dev would define
a getServerClass() method like :
protected Class<? extends IServer<?>> getServerClass() {
return SomeServer.class; // SomeServer implements IServer<TheTypeTheDevWants>
}
But the dev will also need a
typed version,
IServer<TheTypeTheDevWants>, to be injected in
his classes. So :
bind(new TypeLiteral<IServer<TheTypeTheDevWants>>(){}).to(getServerClass()).in(Scopes.SINGLETON);
First question : does this actual create two instances or only one? Two I guess and this is my first problem. How could I bind the same instance to both types?
Second question :
Would it be possible to ask the dev to define a
getServerType() method, that would return "
TheTypeTheDevWants", in a way or another, so the framework can use it to create the two bindings
by itself? Something like :
bind(new TypeLiteral<IServer<?>>(){}).to(getServerClass()).in(Scopes.SINGLETON);
bind(new TypeLiteral<IServer<getServerType()>>(){}).to(getServerClass()).in(Scopes.SINGLETON); // Black magic required here!
Is there any way for the framework to bind
IServer<TheTypeTheDevWants> dynamically, without knowing "
TheTypeTheDevWants" at compile time?
I try to make the life easier for the dev. I'd like the framework to do both binding by itself here.
Is it possible?
I hope my questions makes sense! :-)