Documentation
¶
Overview ¶
Package ioc provides a simple generics based registry which allows registering instance providers for a name and then instantiating instances, returning instances of the requested type.
Package ioc provides a simple generics based registry which allows registering instance providers for a name and then instantiating instances, returning instances of the requested type.
Index ¶
- func Get[T any](r Registry, name string) (T, error)
- func GetTyped[T any](r *TypedRegistry) (T, error)
- func RegisterTyped[T any](r *TypedRegistry, provider TypedProvider[T])
- func RegisterTypedSingleton[T any](r *TypedRegistry, val T)
- type Provider
- type ProviderF
- type Registry
- type TypedProvider
- type TypedRegistry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get will return an instance provided by the Provider mapped to the given name If no provider for the given name exists, or if the instance is not of the type requested an error will be returned
func GetTyped ¶ added in v1.6.8
func GetTyped[T any](r *TypedRegistry) (T, error)
func RegisterTyped ¶ added in v1.6.8
func RegisterTyped[T any](r *TypedRegistry, provider TypedProvider[T])
func RegisterTypedSingleton ¶ added in v1.6.8
func RegisterTypedSingleton[T any](r *TypedRegistry, val T)
Types ¶
type Provider ¶
type Provider interface {
Get() any
}
Provider instances return instances of a given type. They may return singletons or a new instance each time
type ProviderF ¶
type ProviderF func() any
ProviderF is a function implementation of the Provider interface
type Registry ¶
type Registry interface { // Register adds a Provider for the given name Register(name string, provider Provider) // RegisterSingleton adds a Provider which always returns the given instance RegisterSingleton(name string, val interface{}) // GetProvider returns the Provider for the given name, or nil if no Provider has been // registered for the given name GetProvider(name string) Provider }
Registry instances store Providers keyed by name
type TypedProvider ¶ added in v1.6.8
TypedProvider instances return instances of a given type. They may return singletons or a new instance each time
func CachingProvider ¶ added in v1.6.8
func CachingProvider[T any](p TypedProvider[T]) TypedProvider[T]
func GetTypedProvider ¶ added in v1.6.8
func GetTypedProvider[T any](r *TypedRegistry) TypedProvider[T]
func InjectableProvider ¶ added in v1.6.8
func InjectableProvider[T any](r *TypedRegistry, provider any) TypedProvider[T]
func TypedProviderF ¶ added in v1.6.8
func TypedProviderF[T any](providerF func() (T, error)) TypedProvider[T]
type TypedRegistry ¶ added in v1.6.8
type TypedRegistry struct {
// contains filtered or unexported fields
}
func NewTypedRegistry ¶ added in v1.6.8
func NewTypedRegistry() *TypedRegistry
NewTypedRegistry returns a new TypedRegistry instance