digo

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 3 Imported by: 0

README

digo

A Denpdency-Injection Container for Golang

Quick Start

Singleton
di := digo.NewDIGo()

_ = di.SingletonFunc(func() int { return 316 })
_ = di.SingletonFunc(func(i int) string { return fmt.Sprint(i) })

p := new(struct {Name string;Age  int})
_ = di.FillClass(p)
dump.Dump(p)		
// p => *struct{ Name string; Age int } ({
//	"Name": "316",
//	"Age": 7
// } 

_, _ = di.InvokeFunc(func(age int, name string) {
	println(age, name)
	// 316 316
})

Demo

https://github.com/Kretech/xgo/blob/master/digo/digo_test.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TypeOf  = reflect.TypeOf
	ValueOf = reflect.ValueOf
)
View Source
var (
	ErrNotBound = errors.New(`build not-bound types`)
)

Functions

This section is empty.

Types

type DIGo

type DIGo interface {
	// 	注册单例
	//
	// 	s := new(T)
	// 	di.Singleton(s)
	Singleton(instancePtr interface{}) (err error)

	// 通过闭包方式注册
	// 闭包的第一个返回值的类型作为注册的 T
	// 闭包的参数,会根据类型从容器中取数据
	//
	// 	di.SingletonFunc(func(a *A) (T) {
	// 		// a will build first
	// 		return T{}
	// 	})
	SingletonFunc(fn interface{}) (err error)

	// MapSingleton 表示从接口到实体的映射
	//
	// di.MapSingleton(op.Writer, os.Stdout)
	MapSingleton(T Type, V Value) (err error)

	// 利用容器提供的默认数据赋值
	// 	ptr := new(T)
	// 	di.Build(ptr)
	InvokePtr(ptr interface{}) (err error)

	// 利用容器提供的默认数据调用函数
	// 	di.Invoke(func(a *A, b *B) {
	// 		// a, b built
	// 	}
	InvokeFunc(fn interface{}) (fnOut []interface{}, err error)

	// 利用容器提供的默认数据填充对象的成员
	//
	//
	FillClass(classPtr interface{}) (err error)
}

func NewDIGo

func NewDIGo() DIGo

NewDIGo

type DImpl

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

func NewDiGoImpl

func NewDiGoImpl() *DImpl

func (*DImpl) BindFunc

func (this *DImpl) BindFunc(fn interface{})

func (*DImpl) FillClass

func (di *DImpl) FillClass(classPtr interface{}) (err error)

func (*DImpl) InvokeFunc

func (di *DImpl) InvokeFunc(fn interface{}) (fnOut []interface{}, err error)

func (*DImpl) InvokePtr

func (di *DImpl) InvokePtr(ptr interface{}) (err error)

func (*DImpl) MapSingleton

func (di *DImpl) MapSingleton(T Type, V Value) (err error)

func (*DImpl) Singleton

func (di *DImpl) Singleton(instancePtr interface{}) (err error)

func (*DImpl) SingletonFunc

func (di *DImpl) SingletonFunc(fn interface{}) (err error)

type Hook

type Hook uint16

type Kind

type Kind = reflect.Kind

type Type

type Type = reflect.Type

type Value

type Value = reflect.Value

Jump to

Keyboard shortcuts

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