Documentation
¶
Index ¶
- Variables
- func Get[T any](ctx context.Context, container *Container) (T, error)
- func Inject[T any](ctx context.Context, container *Container, into *T) (err error)
- func IsDependencyAlreadyRegistered(err error) bool
- func IsDependencyCycle(err error) bool
- func IsDependencyNotRegistered(err error) bool
- func IsNilReceiver(err error) bool
- func MustRegister[T any](container *Container, provider Provider[T])
- func MustRegisterFactory[T any](container *Container, fn Constructor[T])
- func MustRegisterSingleton[T any](container *Container, fn Constructor[T])
- func MustRegisterValue[T any](container *Container, value T)
- func Register[T any](container *Container, provider Provider[T]) error
- func RegisterFactory[T any](container *Container, fn Constructor[T]) error
- func RegisterSingleton[T any](container *Container, fn Constructor[T]) error
- func RegisterValue[T any](container *Container, value T) error
- type Constructor
- type Container
- type ErrDependencyAlreadyRegistered
- type ErrDependencyCycle
- type ErrDependencyNotRegistered
- type ErrNilReceiver
- type Provider
Constants ¶
This section is empty.
Variables ¶
var Global = &Container{}
Global is the default global dependency container
Functions ¶
func IsDependencyAlreadyRegistered ¶
IsDependencyAlreadyRegistered returns true if the error is a ErrDependencyAlreadyRegistered
func IsDependencyCycle ¶
IsDependencyCycle returns true if the error is a ErrDependencyCycle
func IsDependencyNotRegistered ¶
IsDependencyNotRegistered returns true if the error is a ErrDependencyNotRegistered
func IsNilReceiver ¶
IsNilReceiver returns true if the error is a ErrNilReceiver
func MustRegister ¶
MustRegister registers a new value provider in the dependency injection container. The method panics on error.
func MustRegisterFactory ¶
func MustRegisterFactory[T any](container *Container, fn Constructor[T])
MustRegisterFactory adds a factory to the dependency injection container. The method panics on error.
func MustRegisterSingleton ¶
func MustRegisterSingleton[T any](container *Container, fn Constructor[T])
MustRegisterSingleton adds a singleton to the dependency injection container. The method panics on error.
func MustRegisterValue ¶
MustRegisterValue adds a value to the dependency injection container. The method panics on error.
func RegisterFactory ¶
func RegisterFactory[T any](container *Container, fn Constructor[T]) error
RegisterFactory adds a factory to the dependency injection container. It is equivalent to calling `Register(ctx, container, NewFactoryProvider(fn))`
func RegisterSingleton ¶
func RegisterSingleton[T any](container *Container, fn Constructor[T]) error
RegisterSingleton adds a singleton to the dependency injection container. It is equivalent to calling `Register(ctx, container, NewSingletonProvider(fn))`
func RegisterValue ¶
RegisterValue adds a value to the dependency injection container. It is equivalent to calling `Register(ctx, container, NewValueProvider(value))`
Types ¶
type Constructor ¶
Constructor creates a new instance of an object
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container holds all dependencies, resolving them as required
type ErrDependencyAlreadyRegistered ¶
type ErrDependencyAlreadyRegistered struct {
// contains filtered or unexported fields
}
ErrDependencyAlreadyRegistered is the type of error returned when type that is already registered with a container is re-registered.
func (ErrDependencyAlreadyRegistered) Error ¶
func (e ErrDependencyAlreadyRegistered) Error() string
type ErrDependencyCycle ¶
type ErrDependencyCycle struct {
// contains filtered or unexported fields
}
ErrDependencyAlreadyRegistered is the type of error returned when dependency cycle is detected when constructing dependencies
func (ErrDependencyCycle) Error ¶
func (e ErrDependencyCycle) Error() string
type ErrDependencyNotRegistered ¶
type ErrDependencyNotRegistered struct {
// contains filtered or unexported fields
}
ErrDependencyNotRegistered is the type of error returned when a requested type is not registered with the container
func (ErrDependencyNotRegistered) Error ¶
func (e ErrDependencyNotRegistered) Error() string
type ErrNilReceiver ¶
type ErrNilReceiver struct{}
ErrNilReceiver is the type of error returned when a nil receiver is used in the Inject method
func (ErrNilReceiver) Error ¶
func (e ErrNilReceiver) Error() string
type Provider ¶
Provider provides a value of a given type.
func NewFactoryProvider ¶
func NewFactoryProvider[T any](fn Constructor[T]) Provider[T]
NewFactoryProvider returns a provider that calls the provided constructor to generate the value every time is is required.
func NewSingletonProvider ¶
func NewSingletonProvider[T any](fn Constructor[T]) Provider[T]
NewSingletonProvider returns a provider that calls the provided constructor once to generate the value and provides the same value on subsequent resolves. The provider is thread safe.
func NewValueProvider ¶
NewValueProvider returns a provider that will always return the given value. This allows objects to be directly added to the Container without needing a constructor.