dix

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: MIT Imports: 11 Imported by: 15

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.

CHANGELOG

example

package main

import (
	"encoding/json"
	"fmt"
	"github.com/pubgo/dix"
	"github.com/pubgo/xerror"
	"log"
	"os"
	"time"
)

type Hello interface {
	Hello()
}

type test1 struct {
	i int
}

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

type Config struct {
	Prefix string
}

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

func init() {
	xerror.Exit(dix.Dix(func(h *test1) {
		fmt.Println("h *test1")
	}))
	xerror.Exit(dix.Dix(func(h Hello) {
		h.Hello()
	}))
	xerror.Exit(dix.Dix(func(cfg *Config) (*log.Logger, error) {
		return log.New(os.Stdout, cfg.Prefix, log.Llongfile), nil
	}))
	xerror.Exit(dix.Dix(func(l *log.Logger) {
		l.Print("You've been invoked")
	}))
}

func main() {
	i := 0
	for {
		var cfg Config
		xerror.Exit(json.Unmarshal([]byte(fmt.Sprintf(`{"prefix": "[foo%d] "}`, i)), &cfg))
		xerror.Exit(dix.Dix(&cfg))
		fmt.Println(dix.Graph())
		fmt.Print("==================================================================================\n")
		time.Sleep(time.Second)
		xerror.Exit(dix.Dix(&test1{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

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
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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