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 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
}
type MySQLAccessService struct {
Conns int `inject:"mySqlConns, optional:32"`
}
type BigDataService struct {
DBa DatabaseAccessService `inject:"dba"`
}
...
func main() {
inj := linker.New()
inj.Register(
linker.Component{Name: "dba", Value: &MySQLAccessService{}},
linker.Component{Name: "", Value: &BigDataService{}},
linker.Component{Name: "mySqlConns", Value: int(msconns)},
...
)
inj.Init(ctx)
...
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 FieldType `inject:"compName"`
Field FieldType `inject:""`
Field1 FieldType `inject:"aaa, optional"`
Field2 FieldType `inject:", optional"`
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:
- 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. - For components, which implement linker.PostConstructor interface, the
PostConstruct()
function will be called. - 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 - 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()