dix

package module
v0.1.28 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2021 License: MIT Imports: 11 Imported by: 7

README

dix 中文

dix is a dependency injection tool based on the design of dig.

The main difference between dix and dig is that dix can complete more complex dependency injection management and better dependency isolation relying on namespace, and dix can also dynamically bind objects to dependent functions, object change notifications, etc.

dix is very convenient to use, through Dix you can publish and update objects and monitor binding, etc.

CHANGELOG

example

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"time"

	"github.com/pubgo/dix"
	"github.com/pubgo/xerror"
)

type m11 struct {
}

func (t *m11) Hello1() {
	fmt.Println("Hello1")
}

type M1 interface {
	Hello1()
}

type Hello interface {
	Hello()
}

type testHello struct {
	i int
}

func (t testHello) Hello() {
	fmt.Println("config testHello")
}

type Config struct {
	Prefix string
}

func (Config) Hello() {
	fmt.Println("Hello Config")
}

type MM struct {
	Cfg *Config `dix:"test"`
	Abc string
}

func (MM) Hello() {
	fmt.Println("Hello MM")
}

func init() {
	xerror.Panic(dix.Dix(func(h *testHello) {
		fmt.Println("h *testHello")
	}))

	xerror.Exit(dix.Dix(func(h Hello) {
		h.Hello()
	}))

	xerror.Exit(dix.Dix(func(cfg MM) (*log.Logger, error) {
		fmt.Println("cfg *Config")
		fmt.Println(cfg.Cfg)
		return log.New(os.Stdout, cfg.Cfg.Prefix, log.Llongfile), nil
	}))

	xerror.Exit(dix.Dix(func(l *log.Logger) {
		fmt.Println(l)
		l.Print("You've been invoked1")
	}))

	type ll struct {
		L *log.Logger `dix:""`
		H Hello       `dix:"test"`
	}

	xerror.Exit(dix.Dix(func(l ll) {
		fmt.Println(l)
		l.L.Print("You've been invoked2")
		l.H.Hello()
	}))
}

type M2 interface {
	A()
}

type m22 struct {
	l *log.Logger
}

func (t *m22) A() {
	t.l.Println("AAA")
}

func NewM22(l *log.Logger) M2 {
	return &m22{l: l}
}

func init() {
	xerror.Exit(dix.Dix(func(l *log.Logger) (map[string]M2, error) {
		l.Println("m22 start")
		return map[string]M2{"hello": NewM22(l)}, nil
	}))

	type nss struct {
		M2 M2 `dix:"hello"`
	}

	xerror.Exit(dix.Dix(func(l nss) {
		log.Println("nss start")
		l.M2.A()
	}))
}

func main() {
	defer xerror.RespExit()

	i := 0
	for {
		var cfg Config
		xerror.Exit(json.Unmarshal([]byte(fmt.Sprintf(`{"prefix": "[foo%d] "}`, i)), &cfg))
		xerror.Panic(dix.Dix(map[string]*Config{"test": &cfg}))
		fmt.Printf("cfg: %#v\n", cfg)

		fmt.Println(dix.Graph())
		fmt.Print("==================================================================================\n")
		time.Sleep(time.Second)
		xerror.Exit(dix.Dix(&testHello{i: i}))
		fmt.Println(dix.Graph())
		//time.Sleep(time.Second)
		var log1 *log.Logger
		xerror.Panic(dix.Invoke(&log1))
		fmt.Printf("log1: %#v\n", log1)

		var cfg1 *Config
		xerror.Panic(dix.Invoke(&cfg1, "test"))
		fmt.Printf("cfg1: %#v\n", cfg1)

		// 接口类型
		//= &MM{Abc: "hello MM"}

		type nn struct {
			Struct1 Hello `dix:""`
			MM      string
		}
		var struct1 = &nn{MM: "ssss"}
		xerror.Panic(dix.Invoke(struct1))
		struct1.Struct1.Hello()
		fmt.Println(struct1.MM)

		xerror.Panic(dix.Dix(&m11{}))
		fmt.Println(dix.Graph())

		var mmm struct {
			M1 M1 `dix:""`
		}
		xerror.Panic(dix.Invoke(&mmm))
		mmm.M1.Hello1()
		i++
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dix

func Dix(data ...interface{}) error

Dix ... Deprecated: use Provider instead

func FireResource added in v0.1.26

func FireResource() error

func Graph

func Graph() string

Graph dix graph

func Init added in v0.0.9

func Init(opts ...dix_opts.Option) error

Init ...

func Inject added in v0.1.27

func Inject(data interface{}, ns ...string) error

Inject 注入对象 ns: namespace

func Invoke added in v0.1.16

func Invoke(data interface{}, ns ...string) error

Invoke 注入对象 ns: namespace Deprecated: use Inject instead

func Json added in v0.1.10

func Json() map[string]interface{}

Json dix json graph

func New

func New(opts ...dix_opts.Option) *dix

New dix new

func Provider added in v0.1.24

func Provider(data ...interface{}) error

Provider ...

func ProviderResource added in v0.1.26

func ProviderResource(data ...func(_ *Resource) (interface{}, error)) error

Types

type Resource added in v0.1.26

type Resource struct{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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