Documentation ¶
Overview ¶
This package wraps the golobby/container package to provide support for the following: 1. Easier usage of lazy type resolvers and ability to register specific type instances 2. Support for hierarchical/nested containers to resolve types from parent containers 3. Helper methods for easier/streamlined usage of of the IoC container
Index ¶
- Variables
- func RegisterInstance[F any](c *NestedContainer, instance F)
- func RegisterNamedInstance[F any](c *NestedContainer, name string, instance F)
- type NestedContainer
- func (c *NestedContainer) Fill(structure any) error
- func (c *NestedContainer) Invoke(resolver any) error
- func (c *NestedContainer) MustRegisterNamedScoped(name string, resolveFn any)
- func (c *NestedContainer) MustRegisterNamedSingleton(name string, resolveFn any)
- func (c *NestedContainer) MustRegisterNamedTransient(name string, resolveFn any)
- func (c *NestedContainer) MustRegisterScoped(resolveFn any)
- func (c *NestedContainer) MustRegisterSingleton(resolveFn any)
- func (c *NestedContainer) MustRegisterTransient(resolveFn any)
- func (c *NestedContainer) NewScope() (*NestedContainer, error)
- func (c *NestedContainer) NewScopeRegistrationsOnly() (*NestedContainer, error)
- func (c *NestedContainer) RegisterNamedScoped(name string, resolveFn any) error
- func (c *NestedContainer) RegisterNamedSingleton(name string, resolveFn any) error
- func (c *NestedContainer) RegisterNamedTransient(name string, resolveFn any) error
- func (c *NestedContainer) RegisterScoped(resolveFn any) error
- func (c *NestedContainer) RegisterSingleton(resolveFn any) error
- func (c *NestedContainer) RegisterSingletonAndInvoke(resolveFn any) error
- func (c *NestedContainer) RegisterTransient(resolveFn any) error
- func (c *NestedContainer) Resolve(instance any) error
- func (c *NestedContainer) ResolveNamed(name string, instance any) error
- type ServiceLocator
Constants ¶
This section is empty.
Variables ¶
var (
ErrResolveInstance error = errors.New("failed resolving instance from container")
)
Functions ¶
func RegisterInstance ¶
func RegisterInstance[F any](c *NestedContainer, instance F)
Registers a constructed instance of the specified type Panics if the registration fails
func RegisterNamedInstance ¶
func RegisterNamedInstance[F any](c *NestedContainer, name string, instance F)
Registers a named constructed instance of the specified type Panics if the registration fails
Types ¶
type NestedContainer ¶
type NestedContainer struct {
// contains filtered or unexported fields
}
NestedContainer is an IoC container that support nested containers Used for more complex registration scenarios such as scop based registration/resolution.
func NewNestedContainer ¶
func NewNestedContainer(parent *NestedContainer) *NestedContainer
Creates a new nested container from the specified parent container
func NewRegistrationsOnly ¶
func NewRegistrationsOnly(from *NestedContainer) *NestedContainer
Creates a new container with only registrations from the given container.
func (*NestedContainer) Fill ¶
func (c *NestedContainer) Fill(structure any) error
Fill takes a structure and resolves fields with the tag `container:"type" or `container:"name"`.
func (*NestedContainer) Invoke ¶
func (c *NestedContainer) Invoke(resolver any) error
Invokes the specified function and resolves any arguments specified from the container resolver registrations
func (*NestedContainer) MustRegisterNamedScoped ¶
func (c *NestedContainer) MustRegisterNamedScoped(name string, resolveFn any)
Registers a named resolver with a scoped lifetime (instance per scope) Ex: Each new cobra command will create a new scope Scoped registrations are added as singletons in the current container then are reset in any new child containers Panics if the resolver is not valid
func (*NestedContainer) MustRegisterNamedSingleton ¶
func (c *NestedContainer) MustRegisterNamedSingleton(name string, resolveFn any)
Registers a named resolver with a singleton lifetime Panics if the resolver is not valid
func (*NestedContainer) MustRegisterNamedTransient ¶
func (c *NestedContainer) MustRegisterNamedTransient(name string, resolveFn any)
Registers a named resolver with a transient lifetime (instance per resolution) Panics if the resolver is not valid
func (*NestedContainer) MustRegisterScoped ¶
func (c *NestedContainer) MustRegisterScoped(resolveFn any)
Registers a resolver with a scoped lifetime (instance per scope) Ex: Each new cobra command will create a new scope Scoped registrations are added as singletons in the current container then are reset in any new child containers Panics if the resolver is not valid
func (*NestedContainer) MustRegisterSingleton ¶
func (c *NestedContainer) MustRegisterSingleton(resolveFn any)
Registers a resolver with a singleton lifetime Panics if the resolver is not valid
func (*NestedContainer) MustRegisterTransient ¶
func (c *NestedContainer) MustRegisterTransient(resolveFn any)
Registers a named resolver with a singleton lifetime and instantiates the instance Panics if the resolver is not valid
func (*NestedContainer) NewScope ¶
func (c *NestedContainer) NewScope() (*NestedContainer, error)
NewScope creates a new nested container with a relationship back to the parent container Scoped registrations are converted to singleton registrations within the new nested container.
func (*NestedContainer) NewScopeRegistrationsOnly ¶
func (c *NestedContainer) NewScopeRegistrationsOnly() (*NestedContainer, error)
NewScopeRegistrationsOnly creates a new container with bindings deep copied from the container. Scoped registrations are then activated as singletons within the new nested container.
func (*NestedContainer) RegisterNamedScoped ¶
func (c *NestedContainer) RegisterNamedScoped(name string, resolveFn any) error
Registers a named resolver with a scoped lifetime (instance per scope) Ex: Each new cobra command will create a new scope Scoped registrations are added as singletons in the current container then are reset in any new child containers
func (*NestedContainer) RegisterNamedSingleton ¶
func (c *NestedContainer) RegisterNamedSingleton(name string, resolveFn any) error
Registers a named resolver with a singleton lifetime Returns an error if the resolver is not valid
func (*NestedContainer) RegisterNamedTransient ¶
func (c *NestedContainer) RegisterNamedTransient(name string, resolveFn any) error
Registers a named resolver with a transient lifetime (instance per resolution) Returns an error if the resolver is not valid
func (*NestedContainer) RegisterScoped ¶
func (c *NestedContainer) RegisterScoped(resolveFn any) error
Registers a resolver with a scoped lifetime (instance per scope) Ex: Each new cobra command will create a new scope Scoped registrations are added as singletons in the current container then are reset in any new child containers Returns an error if the resolver is not valid
func (*NestedContainer) RegisterSingleton ¶
func (c *NestedContainer) RegisterSingleton(resolveFn any) error
Registers a resolver with a singleton lifetime Returns an error if the resolver is not valid
func (*NestedContainer) RegisterSingletonAndInvoke ¶
func (c *NestedContainer) RegisterSingletonAndInvoke(resolveFn any) error
Registers a resolver with a singleton lifetime and instantiates the instance Instance is stored in container cache is used for future resolutions Returns an error if the resolver cannot instantiate the type
func (*NestedContainer) RegisterTransient ¶
func (c *NestedContainer) RegisterTransient(resolveFn any) error
Registers a resolver with a transient lifetime (instance per resolution) Returns an error if the resolver is not valid
func (*NestedContainer) Resolve ¶
func (c *NestedContainer) Resolve(instance any) error
Resolves an instance for the specified type Returns an error if the resolution fails
func (*NestedContainer) ResolveNamed ¶
func (c *NestedContainer) ResolveNamed(name string, instance any) error
Resolves a named instance for the specified type Returns an error if the resolution fails