Documentation
¶
Index ¶
- Variables
- func Register[T any](value T)
- func RegisterNamed[T any](name string, value T)
- func RegisterNamedToContext[T any](ctx context.Context, name string, value T)
- func RegisterToContext[T any](ctx context.Context, value T)
- func Resolve[T any]() (value T, found bool)
- func ResolveAll[T any]() (values []T)
- func ResolveAllFromContext[T any](ctx context.Context) (values []T)
- func ResolveFromContext[T any](ctx context.Context) (value T, found bool)
- func ResolveNamed[T any](name string) (value T, found bool)
- func ResolveNamedFromContext[T any](ctx context.Context, name string) (value T, found bool)
- func WithContext(ctx context.Context, container *Container) context.Context
- type Container
- type GenericContainer
- func (c *GenericContainer[T]) Register(value T)
- func (c *GenericContainer[T]) RegisterNamed(name string, value T)
- func (c *GenericContainer[T]) Resolve() (value T, found bool)
- func (c *GenericContainer[T]) ResolveAll() (value []T)
- func (c *GenericContainer[T]) ResolveNamed(name string) (value T, found bool)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrContainerNotInContext is used when a container is not found in the context. ErrContainerNotInContext = errors.New("container not found in context") )
Functions ¶
func Register ¶
func Register[T any](value T)
Register the supplied value of type T as the default value when using Resolve().
Example ¶
package main import ( "fmt" "io" "os" "github.com/renevo/ioc" ) func main() { ioc.Register(5) ioc.Register[io.Writer](os.Stdout) registered, _ := ioc.Resolve[int]() writer, _ := ioc.Resolve[io.Writer]() fmt.Fprintln(writer, registered) }
Output: 5
func RegisterNamed ¶
RegisterNamed will register the supplied value of type T with the specified name.
func RegisterNamedToContext ¶
RegisterNamedToContext will register the supplied value of type T with the specified name.
This function will panic if the context does not contain a container.
func RegisterToContext ¶
RegisterToContext will register the supplied value of type T as the default value when using ResolveFromContext().
This function will panic if the context does not contain a container.
Example ¶
package main import ( "context" "fmt" "io" "os" "github.com/renevo/ioc" ) func main() { ctx := ioc.WithContext(context.Background(), &ioc.Container{}) ioc.RegisterToContext(ctx, 5) ioc.RegisterToContext[io.Writer](ctx, os.Stdout) registered, _ := ioc.ResolveFromContext[int](ctx) writer, _ := ioc.ResolveFromContext[io.Writer](ctx) fmt.Fprintln(writer, registered) }
Output: 5
func ResolveAll ¶
func ResolveAll[T any]() (values []T)
ResolveAll will lookup and return all values registered.
func ResolveAllFromContext ¶
ResolveAllFromContext will lookup and return all values registered.
This function will panic if the context does not contain a container.
func ResolveFromContext ¶
ResolveFromContext will lookup the default registered value.
This function will panic if the context does not contain a container.
func ResolveNamed ¶
ResolveNamed will lookup the value with the specified name.
func ResolveNamedFromContext ¶
ResolveNamedFromContext will lookup the value with the specified name.
This function will panic if the context does not contain a container.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container registers and resolves types to a map of named values.
func FromContext ¶
FromContext will return a container that is stored in the context. If a container is not found, the global default container will be returned.
type GenericContainer ¶
GenericContainer provices some helper functions to register default values as well as provide the reflection options to work with the underlying ioc.Container.
func (*GenericContainer[T]) Register ¶
func (c *GenericContainer[T]) Register(value T)
Register the supplied value of type T as the default value when using Resolve().
func (*GenericContainer[T]) RegisterNamed ¶
func (c *GenericContainer[T]) RegisterNamed(name string, value T)
RegisterNamed will register the supplied value of type T with the specified name.
func (*GenericContainer[T]) Resolve ¶
func (c *GenericContainer[T]) Resolve() (value T, found bool)
Resolve will lookup the default registered value.
func (*GenericContainer[T]) ResolveAll ¶
func (c *GenericContainer[T]) ResolveAll() (value []T)
ResolveAll will lookup and return all values registered.
func (*GenericContainer[T]) ResolveNamed ¶
func (c *GenericContainer[T]) ResolveNamed(name string) (value T, found bool)
ResolveNamed will lookup the value with the specified name.