Skip to content
/ di Public

An experimental Go 1.20+ generic dependency injection package based on type parameters and reflection.

License

Notifications You must be signed in to change notification settings

zekroTJA/di

Repository files navigation

di

An experimental package providing a dependency injection container based on type parameters and reflection.

The implementation is using a multitree DAG to allow multiple references of same dependencies while disallowing circular dependencies.

Primarily, this package is designed to replace the currently used dependency injection container sarulabs/di in my Discord bot project shinpuru.

Warning
The state of the package is in a very early experimental stage and the API might experience breaking changes in future updates.

Example

type Database interface {
    // ...
}

type Config interface {
    // ...
}

type WebServer interface {
    // ...
}

type MySql struct {
    Config Config `di:"singleton"`
}

type EnvConfig struct {}

type WebServerImpl struct {}

func main() {
    c := di.NewContainer()

    MustRegister[WebServer, WebServerImpl](c)
    MustRegister[Database, MySql](c)
    MustRegister[Config, EnvConfig](c, RegisterOptions[EnvConfig]{
        Setup: func(c *Container) (*EnvConfig, error) {
            return readFromEnv("MYAPP_"), nil
        },
    })

    ws := MustGet[WebServer](c)
    ws.Run()
}

Advantages

  • Strongly typed API due to the usage of type parameters.
  • Simple API.
  • Ability to use singleton as well as transistent dependencies.

Disadvantages

  • Teardown currently not implemented for transistent dependencies.
  • Experimental state.
  • Dependency fields must be public fields to be accessible via reflection.

About

An experimental Go 1.20+ generic dependency injection package based on type parameters and reflection.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages