di

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2016 License: Apache-2.0 Imports: 5 Imported by: 27

README

di

Simple Go dependency injection framework based on modules, module imports and constructors.

Installation

go get github.com/ivankorobkov/di

Usage

import (
    "log"
    "github.com/ivankorobkov/di"
)

// Define modules with your dependencies and constructors.
func DbModule(m *di.Module) {
    m.Add(func() *sql.DB { 
        db, err := sql.Open(...) 
        if err != nil {
            panic(err)
        }
        return db
    })
}
func HelloModule(m *di.Module) {
    m.Import(DbModule)
    m.Add(newHelloService)
}

// Example service with dependencies.
type Hello interface {
    HelloWorld() string
}
type helloService struct {
    db *sql.DB
}
func (h *helloService) HelloWorld() string {
    return "Hello, world"
}
func newHelloService(db *sql.Db) Hello {
    return &helloService{db: db}
}

// Build an object graph and initialize a struct.
// Dependencies are automatically injected into constructors.
func main() {
    var app struct {
        Hello Hello
    }
    di.Init(&app, HelloModule)

    msg := app.Hello.HelloWorld()
    log.Println(msg)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fill

func Fill(dstPtr interface{}, moduleFuncs ...ModuleFunc) error

Fill creates a new object graph and fills dstPtr public fields.

func MustFill

func MustFill(dstPtr interface{}, moduleFuncs ...ModuleFunc)

MustFill creates a new object graph and fills dstPtr public fields or panics on an error.

Types

type Constructor

type Constructor struct {
	Module *Module
	Name   string
	Type   reflect.Type
	Deps   []reflect.Type
	Func   func(args []interface{}) interface{}
}

Constructor is a single type specification in a module. It has a result type, dependencies, and a function which returns an instance using an object graph for dependencies.

func (*Constructor) String

func (c *Constructor) String() string

type Graph

type Graph struct {
	Package   *Package
	Instances map[reflect.Type]interface{}
}

Graph is an object graph initialized from a package of modules.

func New

func New(moduleFuncs ...ModuleFunc) (*Graph, error)

New creates a new object graph from module funcs.

func NewGraph

func NewGraph(p *Package) (*Graph, error)

NewGraph creates an object graph from a package.

func (*Graph) Fill

func (g *Graph) Fill(structPtr interface{})

Fill fills public fields in a struct with instances from this graph.

func (*Graph) MustGet

func (g *Graph) MustGet(i interface{}) interface{}

MustGet returns an instance from this graph of the same type as i.

func (*Graph) MustGetByType

func (g *Graph) MustGetByType(typ reflect.Type) interface{}

MustGetByType returns an instance from this graph of the given type.

type Module

type Module struct {
	Name         string
	Imports      []ModuleFunc
	Constructors []*Constructor
	PackageDeps  []reflect.Type
}

func (*Module) AddConstructor

func (m *Module) AddConstructor(f interface{})

Add adds a new function as a type constructor.

func (*Module) AddInstance

func (m *Module) AddInstance(instance interface{})

AddInstance adds an instance as a constructor.

func (*Module) AddInstanceWithFields

func (m *Module) AddInstanceWithFields(instance interface{})

AddInstanceWithFields adds an instance and all its public fields as constructors.

func (*Module) Import

func (m *Module) Import(f ModuleFunc)

Import imports a module which provides the dependencies of this module.

func (*Module) MarkPackageDep

func (m *Module) MarkPackageDep(dep interface{})

MarkPackageDep adds a dependency which must be resolved at a package level, not via imported modules.

func (*Module) MarkPackageDeps

func (m *Module) MarkPackageDeps(structWithDeps interface{})

MarkPackageDeps adds all struct fields as dependencies which must be resolved at a package level.

type ModuleFunc

type ModuleFunc func(m *Module)

func (ModuleFunc) Name

func (m ModuleFunc) Name() string

type Package

type Package struct {
	Modules      map[string]*Module
	Constructors map[reflect.Type]*Constructor
}

func NewPackage

func NewPackage(moduleFuncs ...ModuleFunc) (*Package, error)

func (*Package) MarshalDot

func (p *Package) MarshalDot() string

func (*Package) WriteDot

func (p *Package) WriteDot(io.Writer) error

Jump to

Keyboard shortcuts

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