Awesome Open Source
Awesome Open Source

Build Status codecov

inji

  • a dependency injection container for golang.
  • auto store and register objects into a graph.
  • struct pointer dependency will be auto created if not found in graph.
  • when closing graph, every object will be closed on a reverse order of their creation.

use


package main

import (
	"fmt"

	"github.com/teou/inji"
)

type Test struct {
	Target int `inject:"target"`
}

func (t *Test) Start() error {
	fmt.Println("start", t.Target)
	return nil
}

func (t *Test) Close() {
	fmt.Println("close", t.Target)
}

type Dep struct {
	Test *Test `inject:"test"`
}

func (d *Dep) Close() {
	fmt.Println("close Dep", d.Test)
}

func main() {
	inji.InitDefault()
	//dep.Close, test.Close will be called orderly
	defer inji.Close()
	inji.Reg("target", 123)
	//test will be auto created, test.Start will be called, then dep.Start(if any)
	dep := inji.Reg("dep", (*Dep)(nil)).(*Dep)
	fmt.Println("find dep", dep)
}


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
go (15,172
golang (3,889
dependency-injection (222
container (142
injection (80
dependency (28