ioc

package
v0.22.72 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any](r Registry, name string) (T, error)

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

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 implemenation of the Provider interface

func (ProviderF) Get

func (f ProviderF) Get() any

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

func NewRegistry

func NewRegistry() Registry

NewRegistry returns a new Registry instance

Jump to

Keyboard shortcuts

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