Dependency Injection and Inversion of Control package

Linker

Go Report Card Build Status codecov License GoDoc

Linker is Dependency Injection and Inversion of Control package. It supports the following features:

  • Components registry
  • Automatic dependency injection of the registered components
  • Components lifecycle support via PostConstructor, Initializer and Shutdowner interfaces implementations
  • Post-injection notification
  • Automatic ordering of components initialization
  • Circular dependency detection
  • Components shutdowning

Please refer to this blogpost for some details.

Linker is used by Logrange, please take a look how it is used there.

import (
     "github.com/logrange/linker"
)

type DatabaseAccessService interface {
    RunQuery(query string) DbResult
}

// MySQLAccessService implements DatabaseAccessService
type MySQLAccessService struct {
	// Conns uses field's tag to specify injection param name(mySqlConns)
	// or sets-up the default value(32), if the param is not provided 
    Conns int `inject:"mySqlConns, optional:32"`
}

type BigDataService struct {
	// DBa has DatabaseAccessService type which value will be injected by the injector
	// in its Init() function, or it fails if there is no appropriate component with the name(dba)
	// was registered...
    DBa DatabaseAccessService `inject:"dba"`
}
...

func main() {
    // 1st step is to create the injector
    inj := linker.New()
	
    // 2nd step is to register components
    inj.Register(
		linker.Component{Name: "dba", Value: &MySQLAccessService{}},
		linker.Component{Name: "", Value: &BigDataService{}},
		linker.Component{Name: "mySqlConns", Value: int(msconns)},
		...
	)
	
	// 3rd step is to inject dependecies and initialize the registered components
	inj.Init(ctx)
	
	// the injector fails-fast, so if no panic everything is good so far.
	
	...
	// 4th de-initialize all compoments properly
	inj.Shutdown()
}

Annotate fields using fields tags

The inject tag field has the following format:

inject: "<name>[,optional[:<defaultValue]]"

So annotated fields can be assigned using different rules:

// Field will be assigned by component with registration name "compName",
// if there is no comonent with the name, or it could not be assigned to the type 
// FieldType, panic will happen
Field FieldType `inject:"compName"`

// Field will be assigned by component with any name (indicated as ""), which could be 
// assigned to the FieldType. If no such component or many matches to the type, 
// panic will happen.
Field FieldType `inject:""`

// If no components match to either Field1 or Field2 they will be skipped with 
// no panic. Ambigious situation still panics
Field1 FieldType `inject:"aaa, optional"`
Field2 FieldType `inject:", optional"`

// Default values could be provided for numeric and string types. The 
// default values will be assigned if no components match to the rules
NumFld int `inject:"intFld, optional: 21"`
StrFld string `inject:"strFld,optional:abc"`

Create the injector

Injector is a main object, which controls the components: registers them, initializes, checks and provides initialization and shutdown calls.

inj := linker.New()

Register components using names or anonymously

Component is an object that can be used for initialization of other components, or which requires an initialization. Components can have different types, but only fields of components, with 'pointer to struct' type, could be assigned by the Injector. Injector is responsible for the injection(initialization a component's fields) process. All components must be registered in injector via Register() function before the initialization process will be run.

Initialize components

When all components are registered Init() function of the Injector allows to perform initialization. The Init() function does the following actions:

  1. Walks over all registered components and assigns all tagged fields using named and unnamed components. If no matches or ambiguity happens, the Init() can panic.
  2. For components, which implement linker.PostConstructor interface, the PostConstruct() function will be called.
  3. For components, which implements linker.Initializer interface, the Init(ctx) function will be called in a specific order. The initialization order is defined as following: less dependent components are initialized before the components
  4. If circular dependency between registered components is found, Init() will panic.

Shutting down registered components properly

Properly initialized components could be shut-down in back-initialization order by calling Shutdown() function of the injector. Components, that implement linker.Shutdowner interface, will be called by the Shutdown()

Owner
Logrange
High performance data aggregation system
Logrange
Similar Resources

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

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

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

Aug 1, 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

A builder πŸ”¨ for binding evil program 😈 and normal document 🐣

A builder πŸ”¨ for binding evil program 😈 and normal document 🐣

GoFileBinder A builder πŸ”¨ for binding evil program 😈 and normal document 🐣 Usage Clone this repo and build GoFileBinder.go first, then start: ./GoFi

Dec 12, 2022

Di - A (very) WIP Go 1.18+ generic dependency injection package based on type reflection

di A (very) WIP Go 1.18+ generic dependency injection package based on type refl

Apr 26, 2022

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

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

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

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

πŸ¦„πŸŒˆ 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

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

Code your next Go web project with (a) Mojito! No matter if its an API or a website, go-mojito assists you with dependency injection, simple routing, custom request / response objects and template rendering

 Code your next Go web project with (a) Mojito! No matter if its an API or a website, go-mojito assists you with dependency injection, simple routing, custom request / response objects and template rendering

Go-Mojito is a super-modular library to bootstrap your next Go web project. It can be used for strict API-only purposes as well as server-side renderi

May 1, 2022

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

πŸ›  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

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

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
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
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
πŸ›  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
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
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
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
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
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