Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API

From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
If you have a class that needs other objects to do it’s job you’re in a tricky situation.
It’s harder to debug if something goes wrong and more difficult to test.
To fix this dependency injection is where you insist that any other objects get created by their own classes and then handed over / “injected” -
“Dependency Injection” is a 25-dollar term for a 5-cent concept.
Dependency injection means giving an object its instance variables. Really. That’s it.
Cited from https://jamesshore.com/v2/blog/... -
You have a glass in room A and some whiskey in room B. You also have a room mate in room C.
The dependency injection will get all those things to you. Now, you only need to tell your room mate to make a glass of whiskey, instead of walking around the house yourself.
Of course, if there are other dependencies, like your room mate needs to wash the glass first, or needs to take a shower, the dependency injection will automagically find the most effective routine to make your desires come true. -
I think others have explained it pretty well, so I won't repeat, but I'll give an example:
var socket = createSocket();
var chatHandler = new ChatHandler(socket);
congrats 🎉, you've just "injected" the socket dependency into the chat handler.
imo it's one of those "hey im trying to look fancy with all my buzzwords" words. -
rittmann1014yDI can be used for front devs too, in mobile apps we also have to use it. After you understand what is and how to use, make a front app with it, you will not regret
-
So apparently I've been using DI this entire time and didnt know it.
The thought was "why the hell would I use a mess of complex inheritance mechanisms, when I can just pass in objects that take care of everything?"
And now here we are.
I'm not a web dev, but I hear a lot of talk about dependency injection from fellow devs on the web scene. Wtf is it? Please explain it to my smooth brain
question