inject

package module
v0.0.0-...-71841af Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2017 License: BSD-3-Clause Imports: 4 Imported by: 0

README

inject GoDoc

Golang injection framework based on https://github.com/facebookgo/inject

Usage

See how it works in inject_test.go by running go test

func (s *InjectTestSuite) TestWeave() {
	driver := example.Driver{}
	farmer := example.Farmer{}
	master := example.Master{}
	myLogger := example.MyLogger{}
	tillageMachine := example.TillageMachine{}

	depMap := map[interface{}][]string{
		&myLogger: []string{
			"logger",
		},
		&driver: []string{
			"example.Master.Transport",
		},
		&farmer: []string{
			"example.Master.Food",
		},
		&tillageMachine: []string{
			"example.TillageMachine.Machine",
		},
		&master: []string{},
	}

	graph, err := Weave(depMap)
	s.NoError(err)

	master.Food.GetRice()
	master.Transport.Fly("C++", "Go")

	f := graph[reflect.TypeOf(&example.Farmer{})].(*example.Farmer)
	f.Machine.Run(5)
}

Documentation

Overview

Package inject is the improved version of https://github.com/facebookgo/inject.

Improvements are listed as below:

  1. More intuitive declaration about dependency relations.
  2. Support constructor.
  3. Return the initialized dependency graph (retrieve object outside self scope).

The usage example is in the `inject_test.go`, run `go test` and the output should be like as below

[:~ … /inject] $ go test
2017/07/31 22:36:21 Tillage 3 hours
2017/07/31 22:36:21 Got rice
2017/07/31 22:36:21 Boeing787 Fly from C++ to Go
2017/07/31 22:36:21 Tillage 5 hours
PASS
ok      github.com/browny/inject        0.018s

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Weave

func Weave(depMap map[interface{}][]string) (map[reflect.Type]interface{}, error)

Weave sets up dependencies and returns the result graph.

`depMap` is the map describing the dependency relations. The key of depMap is the reference to the dependency providing object. The value is the list of dependency requiring objects.

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