inversify

package module
v0.0.0-...-9e1d617 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2019 License: MIT Imports: 6 Imported by: 0

README

Go-Inversify - Dependency Injection Tool for Go

Build Status Go Report Card

Yet another dependency injection system for Go inspired by InversifyJS.

Installation

Go get
go get github.com/alekns/go-inversify
Features
  • Bind (and rebind) any value and types to any values
  • Abstract factory (in terms of interface{} - fast), typed factory (slow) with normal types
  • Singletons (lazy)
  • Optional dependencies (resolved as nil)
  • Named dependencies (multi-bindings on single value or type)
  • Checking on cycle dependencies (panic on Build)
  • Containers merging
  • Containers hierarchy
  • Autowire structure
  • Modules

Examples

TODO

Values
  container := inversify.NewContainer("name of container")

  container.Bind(1).To("Hello")
  container.Build()

  container.Get(1) == "Hello"


  container.Rebind(1).To("world")
  container.Build()

  container.Get(1) == "world"
Factories, singleton and optional dependencies
  container.Bind(1).To("Hello")
  container.Bind(2).To(" world")

  // abstract - using of Any
  container.Bind(3).ToFactory(func (word1, word2, optDep Any) Any {
    return word1.(string) + word2.(string), nil
  }, 1, 2, inversify.Optional(4))

  // or through typed function

  container.Bind(3).ToTypedFactory(func (word1, word2 string, optDep Any) string {
    // optDep == nil
    return word1 + word2, nil
  }, 1, 2, inversify.Optional(4)).InSingletonScope()

  // resolved

  container.Build()

  container.IsBound(3) == true
  container.Get(3) == "Hello world"

Named dependencies
  container := inversify.NewContainer("name of container")

  container.Bind(1).To("empty")
  container.Bind(1, "dev").To("Hello")
  container.Bind(1, "prod").To("world")
  container.Build()

  container.Get(1) == "empty"
  container.Get(1, "dev") == "Hello"
  container.Get(1, "prod") == "world"
Merge
  mergedContainer := container1.Merge(container2)
  mergedContainer.Build()
Hierarchy
  baseContainer := Container()
  ...
  subContainer1 := Container()
  ...
  subContainer2 := Container()
  ...

  subContainer1.SetParent(baseContainer)
  subContainer1.Build()

  subContainer2.SetParent(baseContainer)
  subContainer2.Build()
Autowire
  type AppConfig struct {}

  type App struct {
    Values map[string]interface{}  `inversify:"strkey:values"`

    Config *AppConfig `inversify:""`

    TaskRepository ITaskRepository `inversify:"named:xorm"`
    Scheduler      IScheduler      `inversify:"optional"`

    Billing  BillingService // no injection
  }

  container.Bind("values").To(map[string]interface{}{ ... })
  container.Bind((*AppConfig)(nil)).To(&AppConfig{ ... })
  container.Bind((*ITaskRepository)(nil), "xorm").ToFactory(func() (Any, error) {
    ...
  })

  app := &App{}
  container.AutowireStruct(app)
Modules
  authModule := NewModule("auth").
      Register(func(c ContainerBinder) error {
            c.Bind()
            return nil
      }).
      UnRegister(func(c ContainerBinder) error {
            // c.Unbind()
            return nil
      })

  container.Load(authModule)
  container.Load(otherModule)
  container.Build()

  container.Unload(authModule)
  container.Unload(otherModule)
  container.Build()

Documentation

Overview

Package go-inversify provides dependency injection for Go.

See the [README](https://github.com/alekns/goinversify/blob/master/README.md) for more details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutowireStruct

func AutowireStruct(container Container, structure Any) error

AutowireStruct injects dependencies based on annotations and types

Types

type Any

type Any = interface{}

Any specifies abstract type

func Named

func Named(dep Any, name string) Any

Named declares dependency that could have multiply resolves with distinct names

func Optional

func Optional(dep Any) Any

Optional declares dependency as optional (no errors occurred when dependency not resolved)

type AnyReturnFunc

type AnyReturnFunc = interface{}

AnyReturnFunc defines pointer to an abstract function that returns only one single value

type Binding

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

Binding holds factory, specified dependencies and resolved dependencies

func (*Binding) InSingletonScope

func (b *Binding) InSingletonScope()

InSingletonScope declares dependency as singleton

func (*Binding) To

func (b *Binding) To(obj Any) *Binding

To binds to any object that can converted to interface{}

func (*Binding) ToFactory

func (b *Binding) ToFactory(factoryMethod Any, dependencies ...Any) *Binding

ToFactory binds to abstract function with specified dependencies

func (*Binding) ToTypedFactory

func (b *Binding) ToTypedFactory(factoryMethod Any, dependencies ...Any) *Binding

ToTypedFactory binds to typed function with specified dependencies

type Container

type Container interface {
	// Bind declares dependency (make a panic if already binded)
	Bind(Any, ...string) *Binding
	// Rebind re-declares dependency (make a panic if not exists)
	Rebind(Any, ...string) *Binding
	// Unbind removes dependency
	Unbind(Any, ...string) Container

	// Get resolves dependency
	Get(Any, ...string) (Any, error)
	// Get resolves dependency and make a panic if error was produced
	MustGet(Any, ...string) Any
	// IsBound check existences of dependency
	IsBound(Any, ...string) bool

	// Build and resolves dependencies
	Build()

	// Merge with another container and returns new container
	Merge(container Container, name string) Container
	// SetParent supports for hierarchical DI systems
	SetParent(Container)

	// GetParent gets parent of container
	GetParent() Container

	// Load binds module
	Load(*Module) error

	// UnLoad unbinds module
	UnLoad(*Module) error
}

Container holds dependencies graph

func NewContainer

func NewContainer(name string) Container

NewContainer .

type ContainerBinder

type ContainerBinder interface {
	// Bind declares dependency (make a panic if already binded)
	Bind(symbol Any) *Binding

	// Unbind removes dependency
	Unbind(symbol Any)

	// IsBound check existences of dependency
	IsBound(symbol Any) bool
}

ContainerBinder holds interface to encapsulate bindings

type FactoryFunc

type FactoryFunc func() (Any, error)

FactoryFunc defines abstract factory

type Module

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

Module .

func NewModule

func NewModule(name string) *Module

NewModule .

func (*Module) Register

func (mdl *Module) Register(callback func(ContainerBinder) error) *Module

Register .

func (*Module) UnRegister

func (mdl *Module) UnRegister(callback func(ContainerBinder) error) *Module

UnRegister .

type NAny

type NAny = []interface{}

NAny specifies slice of abstract types

type PairDependencyName

type PairDependencyName struct {
	Symbol Any
	Name   string
}

PairDependencyName holds dependency and name

func (PairDependencyName) String

func (pair PairDependencyName) String() string

Jump to

Keyboard shortcuts

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