Status: New
Owner: ----
New issue 687 by [email protected]: Allow for scope aliases
http://code.google.com/p/google-guice/issues/detail?id=687
Let's say I have a:
bindScope(MyScoped.class, new MyScope());
I wish to be able to:
bindScope(MyOtherScoped.class, MyScoped.class);
*****************************************************
Currently, there are a few ways to accomplish something like that (which I
list drawbacks here):
1. local var
{
MyScope myScope = new MyScope();
bindScope(MyScoped.class, myScope);
bindScope(MyOtherScoped.class, myScope);
}
Perfect, except this limits things to when both bindScopes are in the same
method, which is mostly useless.
2. global static
public static final MyScope MY_SCOPE = new MyScope();
{
bindScope(MyScoped.class, MY_SCOPE);
}
{
bindScope(MyOtherScoped.class, MY_SCOPE);
}
This is problematic for Modules.override, and it is also not possible if
MyScope is not stateless, and I wish to create multiple injectors in the
same JVM. It's also a problem if MyScope's constructor takes a param (not
known at static init time).
3. create new instances
{
bindScope(MyScoped.class, new MyScope());
}
{
bindScope(MyOtherScoped.class, new MyScope());
}
This is the least problematic one, but it's still inconvenient,
particularly if MyScope takes a constructor param that should be shared. It
also feels wrong to require multiple instances of the Scope to be created,
when all I really wanted was one per injector.
4. Store it in the Module where it's created
class MyModuleA extends AbstractModule {
public final MyScope myScope = new MyScope();
public void configure () {
bindScope(MyScoped.class, myScope);
}
class MyModuleB extends AbstractModule {
public void configure () {
MyModuleA a = new MyModuleA();
install(a);
bindScope(MyOtherScoped.class, a.myScope);
}
This, though, makes it not possible to install MyModuleA multiple times
(see http://code.google.com/p/guiceberry/issues/detail?id=21).
*****************************************************
The ideal place to create a scope instance is in a "configure" method. If
only I could create aliases to a defined scope annotation as needed, things
would be a lot easier.
--
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.