dix

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2020 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 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 Go added in v0.0.7

func Go(data ...interface{})

Go the dix must be ok

func Graph

func Graph() string

Graph dix graph

func Init added in v0.0.9

func Init(opts ...Option) error

Init ...

func New

func New(opts ...Option) *dix

func Start added in v0.0.13

func Start() error

func Stop added in v0.0.13

func Stop() error

Types

type Option

type Option func(c *Options)

func WithAllowNil

func WithAllowNil(nilValueAllowed bool) Option

func WithInvoker

func WithInvoker(invoker invokerFn) Option

func WithRand

func WithRand(r *rand.Rand) Option

func WithStrict added in v0.0.9

func WithStrict(strict bool) Option

type Options added in v0.0.9

type Options struct {
	Rand            *rand.Rand
	InvokerFn       invokerFn
	NilValueAllowed bool
	Strict          bool
}

type StartCtx added in v0.0.13

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

type StopCtx added in v0.0.13

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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