syringe

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2015 License: MIT Imports: 2 Imported by: 1

README

Syringe Build Status Coverage Status

Installation

To install Syringe:

go get github.com/solher/syringe

Usage

main.go

package main

import (
	"log"
	"net/http"

	"github.com/dimfeld/httptreemux"
	"github.com/solher/syringe"
)

func main() {
	syringe.Default.Inject()
	router := httptreemux.New()

	controller := &Controller{}
	syringe.Default.GetOne(controller)

	router.POST("/", controller.Handler)

	log.Fatal(http.ListenAndServe(":3000", router))
}

controller.go

package main

import (
	"net/http"

	"github.com/solher/syringe"
)

func init() {
	syringe.Default.Register(NewController)
}

type Controller struct {
	m *Model
}

func NewController(m *Model) *Controller {
	return &Controller{m: m}
}

func (c *Controller) Handler(w http.ResponseWriter, r *http.Request, params map[string]string) {
	c.m.Action()
	w.WriteHeader(http.StatusOK)
}

model.go

package main

import "github.com/solher/syringe"

func init() {
	syringe.Default.Register(NewModel)
}

type Model struct {
	s *Store
}

func NewModel(s *Store) *Model {
	return &Model{s: s}
}

func (m *Model) Action() {
	m.s.DBAction()
}

store.go

package main

import "github.com/solher/syringe"

func init() {
	syringe.Default.Register(NewStore)
}

type Store struct{}

func NewStore() *Store {
	return &Store{}
}

func (s *Store) DBAction() {}

Features

  • Dependency graph builder using constructors (see facebookgo/inject if you don't use constructors)
  • Circular dependencies resolver

License

MIT

Documentation

Overview

Package syringe provide tools to build dependency graphs using constructors.

Index

Constants

This section is empty.

Variables

View Source
var Default = New()

Default provide a quick access to an instanciated injector.

Functions

This section is empty.

Types

type Syringe

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

Syringe is a dependency injector. The deps field contains the dependencies and constructors to inject.

func New

func New() *Syringe

New returns a new Syringe object.

func (*Syringe) Get

func (p *Syringe) Get(depStruct interface{}) error

Get injects the fields of an indirected struct passed as argument with dependencies corresponding to their type.

func (*Syringe) GetOne

func (p *Syringe) GetOne(obj interface{}) error

GetOne injects an empty pointer passed as argument with a dependency corresponding to its type.

func (*Syringe) Inject

func (p *Syringe) Inject() error

Inject builds the dependency graph. It is capable to resolve circular dependencies using stub injection.

Example:

  dep1 struct {
	  dep *dep2
  }

  dep2 struct {
	  dep *dep1
  }

  func newDep1(dep *dep2) *dep1 {
	  return &dep1{dep: dep}
  }

  func newDep2(dep *dep1) *dep2 {
  	return &dep2{dep: dep}
  }

In this example, the injector will call the two constructors with stub params and then will replace their value with the instanciated dependencies.

That method can be seen as not safe because it would override eventual modifications done in the constructors.

That being said, it feels like the standard way of doing things as it is a very specific problem that can't be fixed by a "by hand" injection. (True ?)

func (*Syringe) Register

func (p *Syringe) Register(dependencies ...interface{}) error

Register takes one, multiple, or a slice of dependencies and register them into the injector.

func (*Syringe) SafeInject

func (p *Syringe) SafeInject() error

SafeInject builds the dependency graph as a human would do by hand. Therefore, it is not capable to resolve circular dependencies.

Jump to

Keyboard shortcuts

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