dix

package module
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2021 License: MIT Imports: 12 Imported by: 9

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 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")
}

func init() {
	dix.Go(func(h *testHello) {
		fmt.Println("h *testHello")
	})

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

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

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

	type ll struct {
		L *log.Logger
		H Hello
	}
	xerror.Exit(dix.Dix(func(l ll) {
		l.L.Print("You've been invoked")
		l.H.Hello()
	}))
}

func main() {
	i := 0
	for {
		var cfg Config
		xerror.Exit(json.Unmarshal([]byte(fmt.Sprintf(`{"prefix": "[foo%d] "}`, i)), &cfg))
		dix.Go(&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)
		i++
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dix

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

Dix ...

func Graph

func Graph() string

Graph dix graph

func Init added in v0.0.9

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

Init ...

func Invoke added in v0.1.16

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

Invoke 获取对象

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

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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