Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/logrange/linker
- Owner: logrange
- License: apache-2.0
- Created: 2018-12-04T23:56:34.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-02-21T03:17:07.000Z (11 months ago)
- Last Synced: 2024-07-31T20:52:22.140Z (5 months ago)
- Topics: dependency-injection, injector, ioc-framework, lifecycle
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 35
- Watchers: 5
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - linker - A reflection based dependency injection and inversion of control library with components lifecycle support. (Miscellaneous / Dependency Injection)
- zero-alloc-awesome-go - linker - A reflection based dependency injection and inversion of control library with components lifecycle support. (Miscellaneous / Dependency Injection)
- awesome-go-extra - linker - 12-04T23:56:34Z|2020-06-25T19:18:10Z| (Microsoft Office / Dependency Injection)
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 shutdowningPlease 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[: