golang-runtime-di is a framework for runtime dependency injection in go

Go (lint + test) codecov Go Reference

golang-runtime-di

description

golang-runtime-di is a framework for runtime dependency injection in go.

usage

quickstart

  • add it to your go.mod: go get github.com/dbsystel/golang-runtime-di
  • create your components (interfaces are encouraged):
    type Producer interface {}
    type Producer1 interface { A() }
    type Producer1Impl struct {}
    func (p *Producer1Impl) A() {}
    
    type Producer2 struct {}
    
    type Producer3 string
    
    type Consumer struct {
      Producer1 `inject:""`               // inject by interface 
      *Producer2 `inject:""`              // inject by ptr
      Producer3 `inject:""`               // inject by value
      AllProducers []Producer `inject:""` // inject all known coercible components
    }
  • create a new scope, register your components:
    scope := &di.Scope{}
    scope.MustRegister(&ProducerImpl1{})                                          // Component instance
    scope.MustRegister(func () *Producer2 { return &Producer2{} })                // Component factory
    scope.MustRegister(func () (Producer3, error) { return Producer3("a"), nil }) // Component factory with error
  • Wire the target components:
    consumer := &Consumer{} 
    // kindly note: 
    // - you must use ptr here
    // - wired instances are not automatically registered
    scope.MustWire(consumer)

the inject tag

The inject tag marks a struct field to be injected by the DI.

There are following options on the tag:

  • optional: marks the dependency to be optional. this will not create an error if the dependency is not found in scope.
  • qualifier= : use a qualifier to resolve the dependency. by default the qualifier is empty, thus only unqualified instances are selected.
  • qualifier=*: resolve the dependency from any qualifier

Options may be combined, e.g.: optional,qualifier=squash.

dependency injection

scoping

Each scope is isolated, but be aware that if you wire a struct multiple times in different scopes, the dependencies maybe replace by each wiring, depending on the scopes registrations.

qualifiers

Qualifiers can be used to use the same dependency type more than once, the default qualifier is empty ("").
Example:

scope.MustRegister(&Component{}).WithQualifier("yay")

priorities

Priorities can be used to allow overriding of components, lower numbers denote higher priorities.
Example:

// The following factory will not even be called, if the injection of Dependency is requested
scope.MustRegister(func () (*Dependency, error) { return nil, errors.New("meh") }).WithPriority(1)
// Note: priority is higher
scope.MustRegister(&Dependency{}).WithPriority(-1)

registrations and wiring

For the registrations in a scope the following rules apply:

  • matching the dependency fields and registered components will be done using reflection (Type.AssignableTo() and Type.Implements(), resp.)
  • if you are using factory functions, factories for registered components will only be called if necessary
  • wiring of the components will only happen once when the component is to be injected the first time.

component resolution

The component resolution for injection sticks by the following rules (imperatively applied):

  • select registrations which can be coerced to the fields type
  • select the registrations matching the qualifier (skipped if qualifier=*)
  • order by priority
  • if single dependency injection (not slice):
    • select highest priority (= lowest number)
    • error if there's more than a single candidate with that priority
  • if dependency is required (not optional):
    • error if no candidates found

examples

This project comes with tested examples:

License

This project is licensed under Apache License v2.0, which is included in the repository.

Contributions

Contributions are very welcome, please refer to the contribution guide.

Code of conduct

Our code of conduct can be found here.

Owner
DB Systel GmbH
Official GitHub organisation of DB Systel GmbH, ICT-provider for the Deutsche Bahn.
DB Systel GmbH
Similar Resources

Compile-time Dependency Injection for Go

Wire: Automated Initialization in Go Wire is a code generation tool that automates connecting components using dependency injection. Dependencies betw

Jan 2, 2023

A dependency injection library that is focused on clean API and flexibility

Dependency injection DI is a dependency injection library that is focused on clean API and flexibility. DI has two types of top-level abstractions: Co

Oct 13, 2022

Golang PE injection on windows

GoPEInjection Golang PE injection on windows See: https://malwareunicorn.org/workshops/peinjection.html Based on Cryptowall's PE injection technique.

Jan 6, 2023

two scripts written in golang that will help you recognize dependency confusion.

two scripts written in golang that will help you recognize dependency confusion.

two scripts written in golang that will help you recognize dependency confusion.

Mar 3, 2022

golang auto wire code generator

Go-AutoWire helps you to generate wire files with easy annotate δΈ­ζ–‡ζ–‡ζ‘£ this project is base on wire but it did simplify the wire usage and make wire muc

Dec 2, 2022

Strict Runtime Dependency Injection for Golang

wire Wire is runtime depedency injection/wiring for golang. It's designed to be strict to avoid your go application running without proper dependency

Sep 27, 2022

hiboot is a high performance web and cli application framework with dependency injection support

Hiboot - web/cli application framework About Hiboot is a cloud native web and cli application framework written in Go. Hiboot is not trying to reinven

Nov 20, 2022

Go Dependency Injection Framework

Dingo Dependency injection for go Hello Dingo Dingo works very very similiar to Guice Basically one binds implementations/factories to interfaces, whi

Dec 25, 2022

A dependency injection based application framework for Go.

πŸ¦„ Fx An application framework for Go that: Makes dependency injection easy. Eliminates the need for global state and func init(). Installation We rec

Jan 3, 2023

The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | θ°’θ°’ https://github.com/kataras/iris/issues/1329 |

The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | θ°’θ°’ https://github.com/kataras/iris/issues/1329 |

News This is the under-development branch. Stay tuned for the upcoming release v12.2.0. Looking for a stable release? Head over to the v12.1.8 branch

Dec 28, 2022

πŸ¦„πŸŒˆ YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go.

πŸ¦„πŸŒˆ YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go.

πŸ¦„πŸŒˆ YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes.

Jan 4, 2023

How we can run unit tests in parallel mode with failpoint injection taking effect and without injection race

This is a simple demo to show how we can run unit tests in parallel mode with failpoint injection taking effect and without injection race. The basic

Oct 31, 2021

An additive dependency injection container for Golang.

Alice Alice is an additive dependency injection container for Golang. Philosophy Design philosophy behind Alice: The application components should not

Oct 16, 2022

Minimalistic, pluggable Golang evloop/timer handler with dependency-injection

Anagent Minimalistic, pluggable Golang evloop/timer handler with dependency-injection - based on codegangsta/inject - go-macaron/inject and chuckpresl

Sep 27, 2022

Generated dependency injection containers in go (golang)

Generated dependency injection containers in go (golang)

Generation of dependency injection containers for go programs (golang). Dingo is a code generator. It generates dependency injection containers based

Dec 22, 2022

πŸ›  A full-featured dependency injection container for go programming language.

DI Dependency injection for Go programming language. Tutorial | Examples | Advanced features Dependency injection is one form of the broader technique

Dec 31, 2022

A reflection based dependency injection toolkit for Go.

βš’οΈ dig A reflection based dependency injection toolkit for Go. Good for: Powering an application framework, e.g. Fx. Resolving the object graph during

Jan 1, 2023

Simple Dependency Injection Container

Simple Dependency Injection Container

πŸͺ£ gocontainer gocontainer - Dependency Injection Container πŸ“– ABOUT Contributors: RafaΕ‚ Lorenz Want to contribute ? Feel free to send pull requests!

Sep 27, 2022

Simple and yet powerful Dependency Injection for Go

Simple and yet powerful Dependency Injection for Go

goioc/di: Dependency Injection Why DI in Go? Why IoC at all? I've been using Dependency Injection in Java for nearly 10 years via Spring Framework. I'

Dec 28, 2022
Go Dependency Injection Framework

Dingo Dependency injection for go Hello Dingo Dingo works very very similiar to Guice Basically one binds implementations/factories to interfaces, whi

Dec 25, 2022
A dependency injection based application framework for Go.

?? Fx An application framework for Go that: Makes dependency injection easy. Eliminates the need for global state and func init(). Installation We rec

Jan 3, 2023
An additive dependency injection container for Golang.

Alice Alice is an additive dependency injection container for Golang. Philosophy Design philosophy behind Alice: The application components should not

Oct 16, 2022
Generated dependency injection containers in go (golang)
Generated dependency injection containers in go (golang)

Generation of dependency injection containers for go programs (golang). Dingo is a code generator. It generates dependency injection containers based

Dec 22, 2022
πŸ›  A full-featured dependency injection container for go programming language.

DI Dependency injection for Go programming language. Tutorial | Examples | Advanced features Dependency injection is one form of the broader technique

Dec 31, 2022
A reflection based dependency injection toolkit for Go.

βš’οΈ dig A reflection based dependency injection toolkit for Go. Good for: Powering an application framework, e.g. Fx. Resolving the object graph during

Jan 1, 2023
Simple Dependency Injection Container
Simple Dependency Injection Container

?? gocontainer gocontainer - Dependency Injection Container ?? ABOUT Contributors: RafaΕ‚ Lorenz Want to contribute ? Feel free to send pull requests!

Sep 27, 2022
Simple and yet powerful Dependency Injection for Go
Simple and yet powerful Dependency Injection for Go

goioc/di: Dependency Injection Why DI in Go? Why IoC at all? I've been using Dependency Injection in Java for nearly 10 years via Spring Framework. I'

Dec 28, 2022
Dependency Injection and Inversion of Control package

Linker Linker is Dependency Injection and Inversion of Control package. It supports the following features: Components registry Automatic dependency i

Sep 27, 2022
Compile-time dependency injection for Go

Dihedral Dihedral is a compile-time injection framework for Go. Getting started > go get -u github.com/dimes/dihedral Create a type you want injected

Jun 1, 2022