All Projects → logrange → linker

logrange / linker

Licence: Apache-2.0 license
Dependency Injection and Inversion of Control package

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to linker

injector
PSR-11 compatible injector
Stars: ✭ 33 (+0%)
Mutual labels:  dependency-injection, injector
wedi
[Deprecated] A lightweight dependency injection (DI) library for TypeScript, along with a binding for React.
Stars: ✭ 22 (-33.33%)
Mutual labels:  dependency-injection, injector
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (-9.09%)
Mutual labels:  dependency-injection, injector
dodrugs
A macro-powered dependency injector for Haxe
Stars: ✭ 29 (-12.12%)
Mutual labels:  dependency-injection, injector
Harrypotter
🧙🏻 Sample HarryPotter application based on MVVM architecture (ViewModel, LiveData, Repository, Coroutines, Koin or Dagger-Hilt)
Stars: ✭ 116 (+251.52%)
Mutual labels:  dependency-injection, lifecycle
catchflicks
🎬 Kitchen sink project for learning android concepts 🎬
Stars: ✭ 12 (-63.64%)
Mutual labels:  dependency-injection, lifecycle
common-injector
Heavily influenced by Angular and it's dependency injection. Inspired by Angular and Indiv.
Stars: ✭ 18 (-45.45%)
Mutual labels:  dependency-injection, injector
SocketHook
Socket hook is an injector based on EasyHook (win only) which redirect the traffic to your local server.
Stars: ✭ 38 (+15.15%)
Mutual labels:  dependency-injection, injector
Ulfberht
🗡️ A small but powerful & opinionated DI library. Written in Kotlin, and powered by annotation processing.
Stars: ✭ 234 (+609.09%)
Mutual labels:  dependency-injection, lifecycle
inject
[Archived] See https://github.com/goava/di.
Stars: ✭ 49 (+48.48%)
Mutual labels:  dependency-injection, injector
singlefile
featured cs:go internal hack, one file and less than 1000 lines.
Stars: ✭ 47 (+42.42%)
Mutual labels:  injector
inversify-koa-utils
inversify-koa-utils is a module based on inversify-express-utils. This module has utilities for koa 2 applications development using decorators and IoC Dependency Injection (with inversify)
Stars: ✭ 27 (-18.18%)
Mutual labels:  dependency-injection
di
Simple and yet powerful Dependency Injection for Go
Stars: ✭ 188 (+469.7%)
Mutual labels:  dependency-injection
solid
Solid Android components
Stars: ✭ 33 (+0%)
Mutual labels:  dependency-injection
Mimick.Fody
An integrated framework for dependency injection and aspect-oriented processing.
Stars: ✭ 15 (-54.55%)
Mutual labels:  dependency-injection
BESTV
Android TV App powered by TMDb. It is a easy way to find the best TV content, the top movies, series... all of that in your TV.
Stars: ✭ 49 (+48.48%)
Mutual labels:  dependency-injection
component-manager
component framework and dependency injection for golang
Stars: ✭ 21 (-36.36%)
Mutual labels:  dependency-injection
rsdi
Dependency Injection Container
Stars: ✭ 45 (+36.36%)
Mutual labels:  dependency-injection
aspectgo
Aspect-Oriented Programming framework for Go
Stars: ✭ 62 (+87.88%)
Mutual labels:  injector
test-tools
Improves PHPUnit testing productivity by adding a service container and self-initializing fakes
Stars: ✭ 25 (-24.24%)
Mutual labels:  dependency-injection

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()

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].