di

package module
v0.0.3-0...-7b4eb0e Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2019 License: MIT Imports: 3 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

di

GoDoc

Package di implements a comprehensive dependency injection container.

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.

func (*InstancePerContainerScope) Resolve

Resolve calls the embedded resolver only once before it begins returning the same initially resolved value for each call.

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.

func (*InstancePerDependencyScope) Resolve

Resolve calls the embedded resolver exactly once each time it is called.

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

func NewResolver(f interface{}) (*Resolver, error)

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

func (r *Resolver) GetReturnedInterfaceType() reflect.Type

GetReturnedInterfaceType returns the type of the interface that is returned by the resolver function.

func (*Resolver) Resolve

func (r *Resolver) Resolve(c Container) (reflect.Value, error)

Resolve calls the resolver function, returning the resolved value as a reflect.Value, and the error returned from calling the resolver function. Exactly one of these return values will have a non-zero value.

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.

Jump to

Keyboard shortcuts

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