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(ctx context.Context, function any, opts ...Option) error
- func Factory(ctx context.Context, constructor any, opts ...Option) error
- func Fill(ctx context.Context, receiver any) error
- func Implementation(ctx context.Context, implementation any, opts ...Option) error
- func Reset(ctx context.Context)
- func Resolve(ctx context.Context, receiver any, opts ...Option) error
- func Singleton(ctx context.Context, constructor any, opts ...Option) error
- type Binding
- type Constructor
- type Container
- type Context
- type FillingOption
- type NamingOption
- type Option
- type Options
- type Provider
- 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 ¶
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
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
Constructor implements a `Construct()` method which is called either after binding to container in case of singleton or after factory method was called.
type Container ¶
type Container interface { Singleton(constructor any, opts ...Option) error Factory(constructor any, opts ...Option) error Implementation(implementation any, 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 Context ¶ added in v1.2.2
type Context interface { SetContainer(Container) Context Container() Container SetResolver(Resolver) Context Resolver() Resolver Visualize() []string Raw() context.Context }
Context describe DI context propagator capabilities
type FillingOption ¶ added in v1.2.0
type FillingOption interface {
SetFill(bool)
}
FillingOption supports setting a fill flag
type NamingOption ¶
type NamingOption interface {
SetName(...string)
}
NamingOption supports setting a name
type Options ¶
type Options interface {
Apply(Option)
}
Options represents a target for applying an Option
type Provider ¶ added in v1.2.3
Provider is an abstraction of an entity that provides something to Container
type Resolver ¶
type Resolver interface { With(implementations ...any) Resolver Resolve(receiver any, opts ...Option) error Call(function any, opts ...Option) error Fill(receiver any) error }
Resolver implements methods for several implementation resolution scenarios
func NewResolver ¶
NewResolver constructs a Resolver against one or more Containers
type ReturnOption ¶
type ReturnOption interface {
SetReturn(...any)
}
ReturnOption supports setting a return target