Comment by da...@doubledowninteractive.com:

There's a massive conceptual problem regarding @Singleton scope and default scope. A @Singleton that depends on a default scope object is wholly incompatible, just as much as having @Singleton depend on an object with @Session or @Request scope. The javadoc states that @Singleton indicates thread safety: that's about as good as Java gets in terms of a binding contract between your objects and thread safety and immutability (oh, scala, I miss you).

This is exactly what thang...@gmail states above... except that the problem isn't with using the universally accepted singleton pattern, the problem is the notion that using the default scope is in any way useful or safe. Or architecturally valid. For the reasons he stated. Unless you're new to Java, coming from a PHP worldview, and every single object is default scoped and each thread uses a whole new instance of your application's objects.

The whole point of DI is to remove tight, static coupling between objects. This is done to provide proper abstraction and promoting good unit testing (that means readable, clean code). At some point, your application has some object that calls a method on another object that has been injected. If that object is not @Singleton scoped, you probably had to call Injector.getInstance(). That call right there is a tight coupling, breaking the DI paradigm, introducing non-unit-testable code.

The idea that performance is some sort of valid excuse for using the default scope is absolute nonsense. Object construction is not a performance issue. If your objects take too long to construct, you're doing too much in your constructor. If your application takes a few seconds to start up... you don't really have a performance issue.

Calling Injector.getInstance() is bad. Mixing @Singleton and default scope is a confusing, hidden contract. Sure, you know what you did, but does your team? After you leave? After they leave?

Guice ought to throw an error at startup if a @Singleton depends on any other scope. I cannot think of a situation where allowing them to be mixed would be intended.

For more information:
http://code.google.com/p/google-guice/wiki/Scopes

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to google-guice-dev@googlegroups.com.
To unsubscribe from this group, send email to 
google-guice-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en.

Reply via email to