You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I spent a week trying to figure out how to get ServletScopes.scopeRequest() working. The only code samples I could find were the unit tests and even then I found the API very confusing (see [1] for a detailed explanation).
We need to differentiate between two types of variables being passed to the Callable:
Injected stuff (configured by the main Module)
User-supplied arguments
This is similar to AssistedInject in that #1 comes from a Module and #2 comes from the user. The problem is that ServletScopes.scopeRequest()'s seedMap forces us to reference #2 in the Module (where it doesn't really belong).
I'd like to propose an alternative API (see attachment) with the following characteristics:
The main module is kept free of user-supplied variables.
Replace seedMap with a child Module. The Binder API is easier to use and more flexible.
In the old API, the user Callable is injected outside the request scope, while call() is invoked inside the request scope. This forces users to inject Provider<Foo> instead of Foo. The new API allows users to inject Foo directly.
What do you think?
[1] The current API is extremely fragile. If the user misconfigures the slightest thing in the Module he'll get a cryptic error or the wrong values will get injected.
Common configuration mistakes:
Define user-supplied variables in seedMap but not in main Module. Guice will complain:
No implementation for java.lang.String annotated with @com.google.inject.name.Named(value=username) was bound.
If user uses the following code (based on the unit tests):
From [email protected] on January 25, 2012 00:56:37
I spent a week trying to figure out how to get ServletScopes.scopeRequest() working. The only code samples I could find were the unit tests and even then I found the API very confusing (see [1] for a detailed explanation).
We need to differentiate between two types of variables being passed to the Callable:
This is similar to AssistedInject in that #1 comes from a Module and #2 comes from the user. The problem is that ServletScopes.scopeRequest()'s seedMap forces us to reference #2 in the Module (where it doesn't really belong).
I'd like to propose an alternative API (see attachment) with the following characteristics:
What do you think?
[1] The current API is extremely fragile. If the user misconfigures the slightest thing in the Module he'll get a cryptic error or the wrong values will get injected.
Common configuration mistakes:
No implementation for java.lang.String annotated with
@
com.google.inject.name.Named(value=username) was bound.bindConstant().annotatedWith(Names.named("username")).to("wrong_value");
bind(String.class).in(RequestScoped.class);
Username will get value "wrong_value".
* Put user-supplied variables into seedMap.
bind(String.class).annotatedWith(Names.named("username")).to(String.class).in(RequestScoped.class);
Anything else will result in runtime exceptions or the variable getting the wrong value. It's very unintuitive.
Attachment: gist
Testcase.java
Original issue: http://code.google.com/p/google-guice/issues/detail?id=680
The text was updated successfully, but these errors were encountered: