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
  • 1
    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
  • 10
    “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.
  • 1
    Basically, instead of creating the objects you need inside your function or class, you receive them as parameters (or as constructor parameters in a class).

    This is useful in unit testing, where you want to test only the behavior of your function/class. If you have dependency injection you can write mocks/stubs for your dependencies and pass those as parameters to your function/class. This let's you control the behavior of your dependency so it does not interfere with your test.
  • 0
    A more concrete example: an authentication service that needs a users repository to check if a user exists.

    If you create the user repository inside your authentication service class, when you're testing the service you're using a real repository, which needs access to the DB. The database state will interfere with your tests, so ideally you'd want to fake the data that is returned by the repository.

    If you have DI, you can write a fake implementation of the repository that always returns a specified user (or does anything you want) and pass it to the service class during the test. The key here is to make it predictable so that you can test the behavior of the service without any interference with the repository's behavior.

    NOTE: if you don't know what services and repositories are, services do business logic (like moving money between user accounts) or application logic (like authentication) and repositories basically make queries to a database, that's all.
  • 4
    TIL: I've been doing dependency injection for years...
  • 1
    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.
  • 1
    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