Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container interface {
Register(lifetime Lifetime, f interface{})
Resolve(dependencies ...interface{}) error
Fork() Container
}
Container is a dependency-injection container. New dependencies can be registered into the container using Register. Dependencies can be resolved from the container using Resolve. The container can be forked into a new lifetime using Fork.
func NewContainer ¶
func NewContainer() Container
NewContainer creates and returns a new, empty container. It is an alias for NewDefaultContainer.
type DefaultContainer ¶
type DefaultContainer struct {
// contains filtered or unexported fields
}
DefaultContainer is the default (and currently, only) implementation of Container.
func NewDefaultContainer ¶
func NewDefaultContainer() *DefaultContainer
NewDefaultContainer creates and returns a new, empty container using the default container implementation.
func (*DefaultContainer) Fork ¶
func (c *DefaultContainer) Fork() Container
Fork forks the container into a new container lifetime scope.
func (*DefaultContainer) Register ¶
func (c *DefaultContainer) Register(lifetime Lifetime, f interface{})
Register registers a resolver function into the container with the provided lifetime.
func (*DefaultContainer) Resolve ¶
func (c *DefaultContainer) Resolve(dependencies ...interface{}) error
Resolve resolves from the container into the provided interface pointers. Resolve will return an errors that arise will resolving dependencies. If an error ocurrs, no guarantees are made around what is and is not resolved.
type InstancePerContainerScope ¶
type InstancePerContainerScope struct {
// contains filtered or unexported fields
}
InstancePerContainerScope calls the embedded resolver the first time Resolve is called. Any subsequent calls to Resolve will simply return the instance that was first resolved. InstancePerContainerScope is thread-safe.
func NewInstancePerContainerScope ¶
func NewInstancePerContainerScope(resolver *Resolver, lifetime Lifetime) *InstancePerContainerScope
NewInstancePerContainerScope creates a new InstancePerContainer lifetime scope using the provided resolver.
func (*InstancePerContainerScope) GetLifetime ¶
func (s *InstancePerContainerScope) GetLifetime() Lifetime
GetLifetime simply returns the lifetime for this scope.
func (*InstancePerContainerScope) GetUnderlyingResolver ¶
func (s *InstancePerContainerScope) GetUnderlyingResolver() *Resolver
GetUnderlyingResolver simply returns the underlying resolver for this scope.
type InstancePerDependencyScope ¶
type InstancePerDependencyScope struct {
// contains filtered or unexported fields
}
InstancePerDependencyScope calls the embedded resolver every time Resolve is called.
func NewInstancePerDependencyScope ¶
func NewInstancePerDependencyScope(resolver *Resolver) *InstancePerDependencyScope
NewInstancePerDependencyScope creates a new InstancePerDependency lifetime scope using the provided resolver.
func (*InstancePerDependencyScope) GetLifetime ¶
func (s *InstancePerDependencyScope) GetLifetime() Lifetime
GetLifetime simply returns the lifetime for this scope.
func (*InstancePerDependencyScope) GetUnderlyingResolver ¶
func (s *InstancePerDependencyScope) GetUnderlyingResolver() *Resolver
GetUnderlyingResolver simply returns the underlying resolver for this scope.
type Lifetime ¶
type Lifetime int
Lifetime defines the lifetime of a resolved dependency.
const InstancePerContainer Lifetime = 1
InstancePerContainer behaves in much the same way as the Singleton lifetime, with the exception of forked containers. When a container is forked, registrations made with the Singleton lifetime will persist in the new container. Registrations made with InstancePerContainer will not. This means that each container will resolve InstancePerContainer registrations up to one time each.
const InstancePerDependency Lifetime = 2
InstancePerDependency is the smallest, and simplest, lifetime. Every call to Resolve will resolve a new instance from the container.
const Singleton Lifetime = 0
Singleton is the largest lifetime. Dependencies that are registered with Singleton will only ever be resolved once. After the initial resolution, the same instance will be returned in all further resolutions, even across containers.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver validates and embeds a registration function.
func NewResolver ¶
NewResolver creates a new resolver with the resolver function f. It is required that f looks like `func (c di.Container) (T, error)` where `T` is an interface type. An error will be returned if f is not a valid resolver function.
func (*Resolver) GetReturnedInterfaceType ¶
GetReturnedInterfaceType returns the type of the interface that is returned by the resolver function.
type Scope ¶
type Scope interface {
Resolve(c Container) (reflect.Value, error)
GetUnderlyingResolver() *Resolver
GetLifetime() Lifetime
}
Scope defines the methods that any lifetime scope must implement. While three Lifetime's exist, there are only two actual implementations of this interface - InstancePerContainer and InstancePerDependency. This is because the Singleton lifetime simply directs container Forks to copy over the existing InstancePerContainer scopes to the new container.