Live data from Hacker News

Dependency injection in Go with Uber-go/fx

vincent.composieux.fr

71–80 of 96 posts

Re: Dependency injection in Go with Uber-go/fx

#71
This feels unnecessarily complex. I fell in love with Go after years of dealing with Spring's DI, and I haven't found a situation since where dependency injection would have made my life any easier.

I dunno, maybe it'd be more useful for a more generic framework but I wouldn't use this in any of my code.

Re: Dependency injection in Go with Uber-go/fx

#72
post #2

I've used dependency injection heavily in Java in previous jobs, and, later, spent a few years doing Golang at another job. I never missed it. What does dependency injection give you that a simple combination of Singletons, Constructors, and Factories doesn't? I feel like the only thing you get is the ability to combine multiple independent dependency trees without having to make a sane structure. Kind of like, what…

I feel the same way. IoC is a great pattern, but DI frameworks that try to magically sort out dependencies are just overkill especially in go imho

Re: Dependency injection in Go with Uber-go/fx

#73
post #63
post #4

Earlier quoted context omitted.

I came here to ask a similar question. I’ve spent most of my career writing C# where DI is prevalent. Over the Christmas holiday I picked up golang a bit. When I went to learn about DI for golang it seemed very counter to the principles of the language so I simply moved on. What am I missing out on?

Because golang doesn't have constructors, you're forced to implement functions that mimic them yourself. And the language still doesn't prevent you from directly instantiating the struct yourself, meaning it is always possible to bypass the "constructor functions". This is quite terrible and opens up your code to errors. Furthermore, DI frameworks also usually have lifecycle management, which is quite handy in many c…

Fine, but that applies to even data structures and other domain types, which even in a more classically-OO language you wouldn’t pipe through a DI framework. I think DI as a code organization concept is great, but if I have to write a factory for every struct anyway then it’s not any harder to simply use those as a rule and define module boundaries sensibly as a way to determine which code should be using new vs the factory methods.

You have a good point wrt lifecycle management, but I feel like that’s actually a separate class of problem.

Re: Dependency injection in Go with Uber-go/fx

#74

Earlier quoted context omitted.

> how do you know which members of the struct really need to be populated Interfaces + duck typing? Your function shouldn’t ask for the struct, but rather for an interface describing what it needs. Practically, though, I often just ask for the struct because I’m a slob.

An interface with many methods can have the same problem. If the function doesn't actually use every method in the interface, do you really need to implement them all? So then it might be better for the function to declare its own interface with just the methods it uses? But then, all the callers need to be changed if you decide to call another method. There's no principled solution to predicting what dependencies co…

An interface with many methods is already a bad design. limiting it to a handful methods is way easier to maintain. it's fine to return an object has implement many interfaces, but you really don't need to use them all on the input side.

Re: Dependency injection in Go with Uber-go/fx

#75

I've never been convinced by DI frameworks. I've always enjoyed the fact that the Go ecosystem leans away from them and I would hope things stay that way. In my experience they just obfuscate what should be a straightforward, explicit process of setting up your app in main. The less magic happening there the better.

Not just that, but I don't understand why users would be so eager to kill type safety in their applications. If i'm understanding, these injection methods are totally type-unsafe and construction errors are deferred until runtime? https://pkg.go.dev/go.uber.org/fx#Provide

I've used this framework. The exceptions being deferred until runtime were really frustrating.

If you really want to use DI in golang, for some reason, wire[1] at least still gives you type safety and compile-time failures.

1. https://github.com/google/wire

Re: Dependency injection in Go with Uber-go/fx

#76
post #12
post #2

I've used dependency injection heavily in Java in previous jobs, and, later, spent a few years doing Golang at another job. I never missed it. What does dependency injection give you that a simple combination of Singletons, Constructors, and Factories doesn't? I feel like the only thing you get is the ability to combine multiple independent dependency trees without having to make a sane structure. Kind of like, what…

> What does dependency injection give you that a simple combination of Singletons, Constructors, and Factories doesn't? Easy refactoring of an interface/constructor? I've been using go for a while, and I decided to follow the advise of not using a DI for my project. Every time I refactor a constructor, it is a fun hunt to make the same changes everywhere I'm using that constructor.

That's exactly what a Factory is for; you defer construction and pass around an object that knows how to create an instance. Then you're hunting at the root of your call tree to swap out a Factory instead of throughout your codebase to change a constructor call.

This is also what DI amounts to in practice. Frameworks abstract over it in the name of DRY, but at the same time introduce all the downsides of frameworks.

Re: Dependency injection in Go with Uber-go/fx

#77
I think we should also comment on the quality of the framework. I use a home grown approach too. It doesn’t mean I’m not hoping for something to come along and disrupt (for the better) my approach for the “right” abstraction.

That’s what is missing from the conversation: do the abstractions seem justified based on the problems it claims to solve.

Who knows, it could be as influential as jquery was for web development in 2006 (I’m exaggerating of course).

I just find the tone is generally dismissive and that robs viewers the chance to evaluate the tool within the problem space: DI injection for Go.

Re: Dependency injection in Go with Uber-go/fx

#78
post #36
post #12

Earlier quoted context omitted.

> What does dependency injection give you that a simple combination of Singletons, Constructors, and Factories doesn't? Easy refactoring of an interface/constructor? I've been using go for a while, and I decided to follow the advise of not using a DI for my project. Every time I refactor a constructor, it is a fun hunt to make the same changes everywhere I'm using that constructor.

Doesn't the compiler tell you exactly what needs to change?

It does, but having to go through every single error to fix the issues isn't that easy depending on the number of dependants.

Re: Dependency injection in Go with Uber-go/fx

#79
post #12

Earlier quoted context omitted.

> What does dependency injection give you that a simple combination of Singletons, Constructors, and Factories doesn't? Easy refactoring of an interface/constructor? I've been using go for a while, and I decided to follow the advise of not using a DI for my project. Every time I refactor a constructor, it is a fun hunt to make the same changes everywhere I'm using that constructor.

That's exactly what a Factory is for; you defer construction and pass around an object that knows how to create an instance. Then you're hunting at the root of your call tree to swap out a Factory instead of throughout your codebase to change a constructor call. This is also what DI amounts to in practice. Frameworks abstract over it in the name of DRY, but at the same time introduce all the downsides of frameworks.

A service still needs its own dependencies, thus forcing you to change its factories where needed. With a DI you re-define it in a single place.

Re: Dependency injection in Go with Uber-go/fx

#80
post #2

I've used dependency injection heavily in Java in previous jobs, and, later, spent a few years doing Golang at another job. I never missed it. What does dependency injection give you that a simple combination of Singletons, Constructors, and Factories doesn't? I feel like the only thing you get is the ability to combine multiple independent dependency trees without having to make a sane structure. Kind of like, what…

Fx author (in part) here - replied in a separate comment. I mostly agree with you, but DI offers some ecosystem-wide benefits that may be useful in some environments.

I want to say thanks to your team for open sourcing this library so the community can discuss. It’s ambitious and to me feels like a right size approach to the problem, for all the heartache around DI, it seems like it’s different strokes for different folks but that doesn’t mean we should all be taking a collective shit on the engineers who are trying their best to provide a way to organize service complexity strategically. Well done.
Post reply on HN