2

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

Comments
  • 0
    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”
  • 3
    Google this simple shit or gtfo
  • 8
    “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/...
  • 5
    @aviophile chill bru some people like more interactive explanations
  • 1
    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.
  • 3
    TIL: I've been doing dependency injection for years...
  • 0
    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.
  • 0
    @electrineer I read it, it made me angry and disappointed.
  • 0
    DI 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
  • 0
    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.
Add Comment