Skip to content

Latest commit

 

History

History
155 lines (109 loc) · 2.68 KB

README.md

File metadata and controls

155 lines (109 loc) · 2.68 KB

CI Lint

Injector

GoLang dependency injection library to be used as a IoC container

Features

  • Container management for Go applications
  • Seamless dependency injection through tags and type matching
  • No manual type casting required
  • Module start/run phase
  • Module environment variable injection (using caarlos0/env)

Documentation

Installation

Run following inside project directory

 go get github.com/ashans/injector

Then import using

import "github.com/ashans/injector"

Examples

Container bind and resolve

package main

import "github.com/ashans/injector"

type InterfaceA interface {
	MethodA()
}
type ImplA struct{}

func (*ImplA) MethodA() {}

type StructB struct {
	a InterfaceA `inject:""`
}

func main() {
	c := injector.New()

	c.Bind(new(ImplA))
	c.Bind(new(StructB))

	err := c.ResolveDependencyTree()
	if err != nil {
		panic(err)
	}
}

Resolve with qualifier for multiple implementations

package main

import "github.com/ashans/injector"

type InterfaceA interface {
	MethodA()
}

type ImplA1 struct{}

func (*ImplA1) MethodA() {}

type ImplA2 struct{}

func (*ImplA2) MethodA() {}

type StructB struct {
	a1 InterfaceA `inject:"a1"`
	a2 InterfaceA `inject:"a2"`
}

func main() {
	c := injector.New()

	c.NamedBind(new(ImplA1), `a1`)
	c.NamedBind(new(ImplA2), `a2`)
	c.Bind(new(StructB))

	err := c.ResolveDependencyTree()
	if err != nil {
		panic(err)
	}
}

Resolve environment variables in modules

package main

import "github.com/ashans/injector"

type ExampleStruct struct{
	Port int `env:"APP_PORT" envDefault:"8080"` // Will inject 'APP_PORT' to Port 
}

func main() {
	c := injector.New()

	c.NamedBind(new(ExampleStruct))

	err := c.ResolveDependencyTree()
	if err != nil {
		panic(err)
	}
}

Run modules after dependency resolving

package main

import "github.com/ashans/injector"

type ExampleStruct struct {}
func (e *ExampleStruct) Run() error {
	// Module starting code goes here
	
	return nil
}
func main() {
    c := injector.New()

    c.Bind(new(ExampleStruct))

    err := c.ResolveDependencyTree()
    if err != nil {
        panic(err)
    }

    // Execute 'Run' method of modules that implements 'Runnable' interface
    err = c.RunModules()
    if err != nil {
        panic(err)
    }
}

Refer test cases for more examples