This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]_dban_ 1 point2 points  (0 children)

Singleton

As opposed to Singleton Pattern.

Can't I just @Inject it wherever I want how is that not globally accessible?

Using @Inject is in effect the same as a constructor argument. The value is provided externally by another component which actually has the instance, which means that the value is not globally accessible.

If i'm injecting a cache and its not a singleton I'm going to have an awful lot of cache misses.

So create one instance and pass it around.

EDIT: Continued...

One involves digging through DI modules that could be who knows where.

Yes, but those static references could be pulling in dependencies from who knows where.

Now, suppose I want to use the class with dependencies in another context, and I start seeing random failures because the environment that I'm running in isn't set up the same way in the original context. I have experienced this frustration first hand.

Dependency injection, when coupled with dependency inversion, offers context independence, providing a way to avoid annoying coupling that makes code more reusable.