Skip to content
/ inji Public

a dependency inject container for golang (golang inject), objects will be closed on a reverse order of their creation

License

Notifications You must be signed in to change notification settings

teou/inji

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

inji

inji is a dependency injection container for golang.
it 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 Te struct {
	Target int `inject:"target"`
}

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

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

type Dep struct {
	Test *Te `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.RegisterOrFail("target", 123)
	//test will be auto created, test.Start will be called, then dep.Start(if any)
	inji.RegisterOrFail("dep", (*Dep)(nil))
	test, _ := inji.Find("test")
	fmt.Println("find test", test)
	dep, _ := inji.Find("dep")
	fmt.Println("find dep", dep)
}

About

a dependency inject container for golang (golang inject), objects will be closed on a reverse order of their creation

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published