di

package module
v0.0.0-...-bf15922 Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: MIT Imports: 5 Imported by: 0

README

build codecov Go Report Card codebeat badge

About

  • Heavily uses reflection
  • Capable of scoped resolution of dependencies

Installation

$ go get -u github.com/lebedevars/di

Usage

Basics

Pass a provider function and lifetime value to Register to teach the container how to build dependencies. Provider function must have 1 out-parameter. All of provider's arguments need to be registered as well.

c := di.NewContainer()
// *SomeDep has no dependencies
err := c.Register(func() *SomeDep {
  return NewSomeDep()
}, di.Singleton)

// *SomeOtherDep depends on *SomeDep
err = c.Register(func(someDep *SomeDep) *SomeOtherDep {
  return NewSomeOtherDep(someDep)
}, di.Singleton)

Build the container after setting it up. Build checks for errors in configuration, such as cyclic or missing dependencies. If everything is correct, it instantiates singletons and the container is ready for use:

err = c.Build()

Now you can use it to resolve dependencies. Call Invoke to call a function with resolved arguments or call Get to get a dependency instance:

err = c.Invoke(func(someDep *SomeDep, someOtherDep *SomeOtherDep) {
  // do stuff
})
  
val, err := c.Get(reflect.TypeOf(&SomeOtherDep{}))
typedVal := val.(*SomeOtherDep)

Scopes and lifetimes

Container supports the following dependency lifetimes:

  • Singleton - instantiated once per main container
  • Scoped - instantiated once per request
  • Transient - instantiated once per Invoke or Get call

To take advantage of Scoped resolution, create a container in request scope:

c = c.Scoped()

Such a container will cache Scoped dependencies and reuse them on Invoke and Get calls.

Container context

Container allows parameterized instantiation of depencencies. To use container's context, call WithContext:

c = c.WithContext("key", value)

To retrieve context parameters, pass a special type ContextParams as an argument in provider:

err := c.Register(func(params di.ContextParams) *Logger {
	return logger.With("id", params.GetValue("id"))
}, di.Scoped)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

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

Container is a DI container

func NewContainer

func NewContainer() *Container

NewContainer creates a new container

func (*Container) Build

func (c *Container) Build() error

Build checks dependency graph for cyclic dependencies, checks if all dependencies were registered and created singletons. Calling Build is required, otherwise Invoke and Get calls will return an error

func (*Container) Get

func (c *Container) Get(t reflect.Type) (interface{}, error)

Get returns dependency of type t

func (*Container) Invoke

func (c *Container) Invoke(invoker interface{}) error

Invoke calls invoker with resolved arguments

func (*Container) Register

func (c *Container) Register(provider interface{}, lifetime Lifetime) error

Register teaches the container how to resolve dependencies: provider's out-parameter needs all of its inner parameters to be instantiated. If ContextParams type is passed as an argument, it will give access to container's context parameters.

func (*Container) Scoped

func (c *Container) Scoped() *Container

Scoped returns new container in request scope

func (*Container) WithContext

func (c *Container) WithContext(key string, value interface{}) *Container

WithContext returns container with added contextParams values without changing the original one. Context allows to change how dependencies are instantiated. Context values can be retrieved in provider functions:

 err := c.Register(func(params di.ContextParams) *example {
		return newExample(params.GetValue("key").(string))
	}, Transient)

type ContextParams

type ContextParams map[string]interface{}

ContextParams represents container parameters

func (ContextParams) GetValue

func (contextParams ContextParams) GetValue(key string) interface{}

GetValue returns value from context params

type Lifetime

type Lifetime int

Lifetime determines the lifetime of dependencies and whether it can be retrieved from cache or should be instantiated again based on container's scope

const (
	// Singleton lifetime - instatiated once per main container
	Singleton Lifetime = 1
	// Scoped lifetime - instantiated once per container in request scope
	Scoped Lifetime = 2
	// Transient lifetime - instatiated once per call
	Transient Lifetime = 3
)

Jump to

Keyboard shortcuts

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