di

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2018 License: Apache-2.0 Imports: 9 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

View Source
const (
	StartTimeout = 30 * time.Second
	StopTimeout  = 30 * time.Second
)

Variables

This section is empty.

Functions

func Inject added in v1.1.0

func Inject(dstPtr interface{}, mfuncs ...ModuleFunc) error

Inject creates a context and injects dependencies into public struct fields.

func MustInject added in v1.1.0

func MustInject(dstPtr interface{}, mfuncs ...ModuleFunc)

MustInject creates a context and injects dependencies into public struct fields, or panics on an error.

Types

type App added in v1.1.0

type App struct {
	Context      *Context
	Logger       Logger
	StartTimeout time.Duration
	StopTimeout  time.Duration
}

App provides a start/stop lifecycle and a graceful shutdown. Usually, users should call app.Run() which starts the services in toplogical order from dependencies to dependants. Then blocks until a SIGINT/SIGKILL signal arrives, and stops the services in reverse order.

func NewApp added in v1.1.0

func NewApp(modules ...ModuleFunc) (*App, error)

NewApp creates a new application from modules.

func (*App) Run added in v1.1.0

func (app *App) Run() error

Run starts the application, awaits a stop signal and then stops the application.

func (*App) Start added in v1.1.0

func (app *App) Start(ctx context.Context) error

Start starts the services which implement the Starter interface.

func (*App) Stop added in v1.1.0

func (app *App) Stop(ctx context.Context) error

Stop stops the services which implement the Stopper interface.

type Context added in v1.1.0

type Context struct {
	Modules       map[string]*Module
	Providers     map[reflect.Type]*Provider
	Instances     map[reflect.Type]interface{}
	InstanceSlice []interface{} // Ordered from dependencies to dependants.
}

Context is a dependency injection context.

func NewContext added in v1.1.0

func NewContext(mfuncs ...ModuleFunc) (*Context, error)

NewContext creates a context and initializes all instances from its providers.

func (*Context) Get added in v1.1.0

func (ctx *Context) Get(dstPtr interface{}) bool

Get returns an instance from this context of a given type.

func (*Context) Inject added in v1.1.0

func (ctx *Context) Inject(structPtr interface{})

Inject injects dependencies into public struct fields.

func (*Context) MustGet added in v1.1.0

func (ctx *Context) MustGet(dstPtr interface{})

GetMust returns an instance from this context of a given type or panics if absents.

type Logger added in v1.1.0

type Logger interface {
	Println(v ...interface{})
}

Logger is an application logger.

type Module

type Module struct {
	Name      string
	Imports   []ModuleFunc
	Providers []*Provider
	Deps      []reflect.Type
}

Module groups providers, dependencies and imports.

func (*Module) Add added in v1.1.0

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

Add ands a new provider.

func (*Module) AddInstance

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

AddInstance adds a new instance provider.

func (*Module) Dep added in v1.1.0

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

Dep adds a dependency which will be provided at a context level, not via imported modules.

func (*Module) Import

func (m *Module) Import(module ModuleFunc)

Import adds another module to this module dependencies.

type ModuleFunc

type ModuleFunc func(*Module)

ModuleFunc defines a module provider.

func (ModuleFunc) Name

func (m ModuleFunc) Name() string

type Provider added in v1.1.0

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

Provider creates a service instance.

func (*Provider) String added in v1.1.0

func (c *Provider) String() string

type Starter added in v1.1.0

type Starter interface {
	Start() error
}

Starter is a service which should be started on an application startup.

type Stopper added in v1.1.0

type Stopper interface {
	Stop() error
}

Stopper is a service which should be stopped on an application shutdown.

Jump to

Keyboard shortcuts

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