News for hackers. Stuff that matters.
Re:One Year After Switching from Java to Go: Our Experiences by RussianCow on Wednesday February 19, 2025 @03:34AM (#43099943)
If you're doing "dependency injection" by just passing arguments to functions/modules, you're not really doing dependency injection—you're doing "dependencies" without the "injection" part. I'm not saying that DI necessitates a ton of magic, but you need at least a small framework for specifying dependencies and injecting them into your modules dynamically.
Re:One Year After Switching from Java to Go: Our Experiences by com2kid on Wednesday February 19, 2025 @04:16AM (#43100207)
[delayed]
Re:One Year After Switching from Java to Go: Our Experiences by cryptos on Wednesday February 19, 2025 @04:26AM (#43100273)
But that "framework" could be a simple factory function.
Re:One Year After Switching from Java to Go: Our Experiences by lucumo on Wednesday February 19, 2025 @05:23AM (#43100588)
No, that's wrong.

DI requires that the deps come from outside, not that it's dynamically created. The opposite is that dependencies are created inside the unit. DI is about which part of the code owns the dependency. With DI it's some parent component, without DI it's the component itself.

DI with magic can simplify the management of component lifecycles, but it's entirely possible to do it without.

Re:One Year After Switching from Java to Go: Our Experiences by gf000 on Wednesday February 19, 2025 @05:44AM (#43100709)
No, you can pass (inject) the necessary dependencies to the constructor of an object at creation time, and then simply use that object instance everywhere. The only thing frameworks do is "solve" the dependency graph and instantiate stuff in the correct order.

This is also the most common/preferred way Spring et alia implements their "framework-aided" DI, so that you can write unit tests easily without bootstrapping a Spring context (and it is just well-designed vanilla Java code).

Re:One Year After Switching from Java to Go: Our Experiences by simiones on Wednesday February 19, 2025 @07:05AM (#43101162)
"Injection" in "dependency injection" simply refers to getting your dependencies from outside instead of building them yourself.

Let's take the case of an object which represents a simple CRUD web service that needs to talk to a database to get some data. Here is what it looks like without dependency injection:

  func NewService(databaseHostname, databaseLoginSecret string) (WebService, error) {
    databaseConn, err := databse.NewConn(databaseHostname, databaseLoginSecret)
    if err != nil {
        return WebService{}, fmt.Errorf("Failed to create database conn for WebService: %w", err)
    }
    return WebService{
        databaseConn: databaseConn
    }, nil
  }
And here is what it looks like with dependncy injection:

  func NewService(databaseConn DatabaseConn) WebService {
    return WebService{
        databaseConn: databaseConn
    }
  }
This is the only concept: don't build your own dependencies, get them from outside.

Ideally then there is a place in your application, possibly in `main()`, where all of the base services are initialized, in the required order, and references are passed between them as needed. This can include "factory" style objects if some of these need to be initialized on-demand.