inject

package module
v0.0.0-...-d6750bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2018 License: Apache-2.0 Imports: 6 Imported by: 1

README

Dependency Injector

A dependency injector which extends facebook injector functionatlity. The injector supports life-cycle pattern what allows to initialize and de-initialize components in a specific order.

Typical usage workflow looks like this:

import "github.com/jrivets/inject"
...
func main() {
	injector := inject.NewInjector()
	defer injector.Shutdown()
	
	injector.Register(&inject.Component{comp1, "comp1"}, &inject.Component{comp2, "comp2"} ...)
	injector.Construct()
	... 
}

Injections rules are the same as described in https://godoc.org/github.com/facebookgo/inject

LifeCycler interface

The inject.LifeCycler component should support 3 functions:

type LifeCycler interface {
	DiPhase() int
	DiInit() error
	DiShutdown()
}

DiPhase() returns an initialization phase for the component (components with lower values are initialized before components with higher values) DiInit() the component initialized. Will be called when all fields are injected. DiShutdown() the component de-initializer. It will be called in injctor.Shutdown() call. The de-initialization process is done in reverse order - components with higher phase values are de-initialized before components with lower ones.

If a life-cycler panics or reports an error during initialization, all previously initialized components will be de-initialized in the reverse or their initialization order and injector.Construct() will panic then.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Component

type Component struct {
	Component interface{}
	Name      string
}

func (*Component) String

func (c *Component) String() string

type Injector

type Injector struct {
	// contains filtered or unexported fields
}

Use the Injector to provide dependency injection (DI) design pattern. The Injector can construct and populate Components (a structs which fields are tagged by `inject: "..."` directive).

The following workflow is suppossed to be done:

  1. Create an injector by injector := newInjector() call.
  2. Register components like injector.Register(&Component{comp, "the component name"} ...)
  3. Do the injection by injector.construct(). Be sure that the call CAN CONSTRUCT components THAT WERE NOT REGISTERED, but refferred by. So, for example, if a Component fields requires injection with empty name tag (`inject:""`) AND there is no registered component with the name (empty), the injector will create a new instance of the refferred component.
  4. execute the command and shutdown the injector just before exit from the program

The injector supports LifeCycler pattern which allows to initialize components that implement the interface in a specific order and de-initialize them in reverse of the initialization order.

func NewInjector

func NewInjector(logger gorivets.Logger, loggerFb gorivets.Logger) *Injector

func (*Injector) Construct

func (i *Injector) Construct()

func (*Injector) Debugf

func (i *Injector) Debugf(format string, v ...interface{})

func (*Injector) Register

func (i *Injector) Register(comps ...*Component)

func (*Injector) RegisterMany

func (i *Injector) RegisterMany(comps ...interface{})

func (*Injector) RegisterOne

func (i *Injector) RegisterOne(ifs interface{}, name string)

func (*Injector) Shutdown

func (i *Injector) Shutdown()

type LifeCycler

type LifeCycler interface {
	DiPhase() int
	DiInit() error
	DiShutdown()
}

A component can implement the interface when the component has a life-cycle All life-cyclers are ordered by DiPhase value after creation and injection. Components with lower value are initialized first

type PostConstructor

type PostConstructor interface {
	DiPostConstruct()
}

A component can implement the interface to be notified by the injector after all dependencies are injected

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL