CaptainCodeman / clean-go

Clean Architecture Example in Go
279 stars 26 forks source link

Note on Dependency Injection #6

Open sagikazarmark opened 6 years ago

sagikazarmark commented 6 years ago

Thanks for this example, it's really great.

I would like to comment one thing though, namely the section about Dependency Injection.

You state that dependency injection is not always necessary, you even say one doesn't need it and say something about runtime performance costs.

The fact is though, that there are examples of dependency injection in this project as well, like the one here.

Dependency injection is almost always necessary to adhere the Dependency Inversion Principle which is one of the key pillars of Clean Architecture. It's the moment when you pass a set of dependencies from the outside to your object instead of creating them inside (thus avoiding hard coupling between them).

It is true that you can do dependency injection manually, but at certain level it can become cumbersome.

I believe you meant to say dependency injection containers (which would make perfect sense for most of this section).

I would also argue with the runtime performance hit point. Clearly dependency injection frameworks/containers always use some sort of hacky way to make DI user friendly, but that cost is usually paid at the very beginning of the application initialization and does not affect the runtime performance.

(I have to admit that I haven't worked much with Java or .NET, so I'm not familiar with their DIC solutions, but I'm pretty sure the situation is still much better than in case of script languages where initialization happens for each request and to make things faster production builds "compile" the container to avoid rebuilding the dependency tree for every request)

CaptainCodeman commented 6 years ago

Yeah, it was more for the Java / .NET crowd where it sometimes feels like half the development effort is configuring dependecy injection (containers) when, if you take a step back, they don't give you that much extra over a much simpler approach (manual factory approach)