• I still haven’t really understood the use (and use case) of “dependency injection” (and it feels to me I read now everything about dependency injection I could find), to me it seems to be yet another ProblemFactory.

    • Single responsibility principle: is your GetData() function responsible for getting data? Or is it responsible for creating a new database connection and also using that to go get the data?

      Start naming your functions by what they really do. When you see the word “and” in your function name, you know your function is responsible for too much.

      Dependency injection is the difference between CreateDatabaseConnectionAndGetData() and GetData(connection ConnectionType).

      In the first example, that function will always connect to the specific db that you hard coded in it. It probably has to also read in a config file to get the connection details. Maybe you should name it ReadConfigAndCreateDatabaseConnectionAndGetData()?

      In the second example, I can pass in a MySQL connection or PostgreSQL connection, or some dummy connection for testing.

      Keep all that nasty dirty untestable code in one place and spare your business logic from owning all of that.

      • Thanks for the write up, but as I said, I know and I’ve read all about that already. I still cannot see, why a simple function argument and an interface isn’t enough (you can probably already call that “dependency injection” if you want to get fancy)

        I guess I have just divorced with OOP and the “necessary” “design patterns”…

        Things are more simple and less boilerplaty now for me :).

            • You’re gonna have a tough time talking to others about your code if you don’t agree on common terminology. Function invocation is just function invocation, it doesn’t say anything about the form of the parameters or composition. Dependency injection is a well known and commonly understood method of facilitating decoupling and composition by passing a function’s dependencies as parameters. From your comments you’re talking about the second, but refusing the name, for… reasons?

              • I guess I’m a little bit too long already in the functional/data-driven world (after being a decade in OO languages (IMO too long…)). In OOP you may need a separate term for that.

                But I think it’ just not really necessary in functional programming, it’s just another parameter when calling a function, that may be a somewhat type-constrained generic (for testing e.g. with a mock implementation).

                I mean function parameters are somewhat dependencies anyway, so should I call all my parameters now dependencies and invocation “injection”?

                • Thought you were OP for a second there, as they were talking about composability. Whether it’s dependency injection or not depends on what shape your parameters take. If you’re doing functional programming and you’re passing handlers and connections etc. as params, that’s dependency injection. If you’re only passing strings and objects and such and the function has to do a bunch of logic to decide how to handle its params, that’s not dependency injection.

            • “Dependency injection” is just a term for providing a function or method with its dependencies rather than making the function go and gather them itself.

              It’s (typically) done through parameters, but it’s still more specific than just invoking a function. It describes how that function was written and the reasoning for certain parameters. To the other commenter’s point, you’ll have a hard time communicating about your code with other developers if you refuse to use the term dependency injection just because you don’t like OOP.

    • Generally speaking the use case is writing tests. If your tests just call all the dependencies and new directly then it’s harder to write tests for your specific component while avoiding setting up a whole bunch of stuff (to make all those other classes work). By requiring all the dependencies to be provided to the class, you can swap them out at test time for something else that’s easier to work with.

      That said, IMO it’s a symptom of problems in language design. Using DI is only necessary because languages like C# don’t make it easy to mock out new or classes used directly, so we resort to wrapping everything in interfaces and factories to avoid those features and replace them with ones that are easier to mock. If the language was designed such that those features were easy to replace during testing then DI probably wouldn’t be a thing.

      • It’s probably a general symptom of what people call OOP nowadays, in a more functional composeable world (where I’m living in currently). You just use function parameters and interfaces (or as Rust calls it “Traits”). But I still think in OOP, this is enough as well and the dataflow is more clear.