Otaku – Cedric's blog

submited by
Style Pass
2022-05-12 02:30:04

It’s fascinating to me that so many years after the concept of Dependency Injection (DI) became popular, so many people still don’t seem to understand what it is and why it’s such a fundamental idea that transcends languages.

No, passing dependencies as parameters is not DI and it’s actually a terrible way of building software. Let’s explore why.

Let’s imagine a typical application that starts at main() and then ends up calling a function you are working on, let’s call it “sendPacket()“:

Next, you decide you want to log whenever a packet is sent, so you need a logger. The traditional, non DI (and wrong) way to do this would be to instantiate this logger as high as possible (so it can be used by other functions) and then passed down:

Java and Kotlin have a great mechanism for DI (they are not the only ones, but I’ll focus on them since their approach is fairly well known by now).

Leave a Comment