Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/logrange/linker

Dependency Injection and Inversion of Control package
https://github.com/logrange/linker

dependency-injection injector ioc-framework lifecycle

Last synced: 3 months ago
JSON representation

Dependency Injection and Inversion of Control package

Awesome Lists containing this project

README

        

# Linker

[![Go Report Card](https://goreportcard.com/badge/github.com/logrange/linker)](https://goreportcard.com/report/github.com/logrange/linker) [![codecov](https://codecov.io/gh/logrange/linker/branch/master/graph/badge.svg)](https://codecov.io/gh/logrange/linker) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/logrange/linker/blob/master/LICENSE) [![GoDoc](https://godoc.org/github.com/logrange/linker?status.png)](https://godoc.org/github.com/logrange/linker)

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](https://www.logrange.io/blog/linker.html) for some details.

Linker is used by [Logrange](https://github.com/logrange/logrange), please take a look how it is used [there](https://github.com/logrange/logrange/blob/be1cc8dc0ae8fa9154eec91bea33cd2105509e11/server/server.go#L53).

```golang

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: "[,optional[: