I had a problem because I did not use a dependency in the appropriate
scope. The dependency was *request scoped* but I injected it directy in a
*singleton
*object... Fun stuff followed!
I was wondering about best practices to help reduce those wrong scopes bugs.
Would it be a good pattern to always inject the *provider* of a dependency
instead of the dependency itself? Would it be way too overkill in your
opinion?
And would always using providers, even for singleton dependencies, impact
performance significantly?
Because (and correct me if I'm wrong) doing so prevents all wrong scopes
issues.
Ex :
----------------------
public class MyClass implements IMyClass
{
private final Provider<INoScopeBasedDependency>
noScopeBasedDependencyProvider;
private final Provider<ISingletonBasedDependency>
singletonBasedDependencyProvider;
private final Provider<IRequestBasedDependency>
requestBasedDependencyProvider;
@Inject
public MyClass(Provider<INoScopeBasedDependency>
noScopeBasedDependencyProvider,
Provider<ISingletonBasedDependency>
singletonBasedDependencyProvider;,
Provider<IRequestBasedDependency>
requestBasedDependencyProvider)
{
this.noScopeBasedDependencyProvider =
noScopeBasedDependencyProvider;
this.singletonBasedDependencyProvider =
singletonBasedDependencyProvider;
this.requestBasedDependencyProvider =
requestBasedDependencyProvider;
}
@Override
public INoScopeBasedDependency getNoScopeBasedDependency()
{
return this.noScopeBasedDependencyProvider.get();
}
@Override
public ISingletonBasedDependency getSingletonBasedDependency()
{
return this.singletonBasedDependencyProvider.get();
}
@Override
public IRequestBasedDependency getRequestBasedDependency()
{
return this.requestBasedDependencyProvider.get();
}
}
----------------------
What do you think? Is that crazy?
Are there other ways to help preventing those scope related bugs?
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.