Documentation ¶
Overview ¶
Package di is a dependency injection library that is focused on clean API and flexibility. DI has two types of top-level abstractions: Container and Resolver. First one is responsible for accepting constructors and implementations and creating abstraction bindings out of them. Second implements different implementation resolution scenarios against one or more Containers.
Initially this library was heavily inspired by GoLobby Container (https://github.com/golobby/container) but since then had a lot of backwards incompatible changes in structure, functionality and API.
Index ¶
- Constants
- func Call(function interface{}, opts ...Option) error
- func Factory(constructor interface{}, opts ...Option) error
- func Fill(receiver interface{}) error
- func Implementation(implementation interface{}, opts ...Option) error
- func Reset()
- func Resolve(receiver interface{}, opts ...Option) error
- func Singleton(constructor interface{}, opts ...Option) error
- type Binding
- type Constructor
- type Container
- type FillingOption
- type NamingOption
- type Option
- type Options
- type Resolver
- type ReturnOption
Constants ¶
const DefaultBindName = "default"
DefaultBindName is the name that is used in containers by default when binding values.
Variables ¶
This section is empty.
Functions ¶
func Call ¶
Call takes a function, builds a list of arguments for it from the available bindings, calls it and returns a result.
func Fill ¶
func Fill(receiver interface{}) error
Fill takes a struct and resolves the fields with the tag `di:"..."`. Alternatively map[string]Type or []Type can be provided. It will be filled with all available implementations of provided Type.
func Implementation ¶ added in v1.1.1
Implementation receives ready instance and binds it to its REAL type, which means that declared abstract variable type (interface) is ignored
func Reset ¶
func Reset()
Reset deletes all the existing bindings and empties the container instance.
Types ¶
type Binding ¶
type Binding struct {
// contains filtered or unexported fields
}
Binding holds either singleton instance or factory method for a binding
type Constructor ¶ added in v1.2.0
type Constructor interface {
Construct() error
}
Constructor implements a `Construct()` method which is called either after binding to container in case of singleton, either after factory method was called.
type Container ¶
type Container interface { Singleton(constructor interface{}, opts ...Option) error Factory(constructor interface{}, opts ...Option) error Implementation(implementation interface{}, opts ...Option) error ListBindings(reflect.Type) (map[string]Binding, error) Reset() }
Container is responsible for abstraction binding
func NewContainer ¶
func NewContainer() Container
NewContainer creates a new instance of the Container
type FillingOption ¶ added in v1.2.0
type FillingOption interface {
SetFill(bool)
}
type NamingOption ¶
type NamingOption interface {
SetName(...string)
}
type Option ¶
type Option func(Options)
func WithReturn ¶
func WithReturn(returns ...interface{}) Option
type Resolver ¶
type Resolver interface { With(implementations ...interface{}) Resolver Resolve(receiver interface{}, opts ...Option) error Call(function interface{}, opts ...Option) error Fill(receiver interface{}) error }
func NewResolver ¶
type ReturnOption ¶
type ReturnOption interface {
SetReturn(...interface{})
}