Documentation
¶
Overview ¶
Package godi implements a small and lightweight container used for dependency injection without code generation.
Its main component is the Container interface, which can be instantiated with NewContainer. Now dependencies can be bound to the container either instantiating or as a singleton and queried form the Container's ResolverFunc.
It's recommended to pass the Container's ResolverFunc with the context of your application, allowing you to access the dependency injection container easily.
// create the container and bind a dependency container := godi.NewContainer() container.MustBind("time-service", func(resolver godi.ResolverFunc) any { return time.Now() }) // add resolver to a context ctx := context.WithValue(context.Background(), "container", container.Resolver()) // use the context to retrieve the resolver and execute it resolver := ctx.Value("container").(godi.ResolverFunc) currentTime := godi.MustResolve[time.Time]("time-service", resolver) fmt.Println(currentTime.Unix())
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustResolve ¶
func MustResolve[T any](name string, resolver ResolverFunc) T
MustResolve is a helper function to simplify interaction with a ResolverFunc. MustResolve tries to fetch a dependency by its name and panics, if the dependency can't be converted to the given type or can't be found by the provided ResolverFunc.
func Resolve ¶
func Resolve[T any](name string, resolver ResolverFunc) (T, error)
Resolve is a helper function to simplify interaction with a ResolverFunc. Resolve tries to fetch a dependency by its name and convert it to the given type. An error is returned if the conversion failed or the dependency could not be found.
Types ¶
type BinderFunc ¶
type BinderFunc = func(resolver ResolverFunc) any
BinderFunc is a generic function, used to bind dependencies to a Container. It's first argument is a ResolverFunc, which allows you to request additional dependencies as needed.
type Container ¶
type Container interface { Lock() Bind(name string, binder BinderFunc) error MustBind(name string, binder BinderFunc) BindSingleton(name string, binder BinderFunc) error MustBindSingleton(name string, binder BinderFunc) Resolver() ResolverFunc }
Container is the main interface for the dependency collection container. Through the Container, multiple dependencies can be prepared and stored by an identifying name and resolved on demand by this name.
The Container supports instanced binding, through its Bind method. Instanced dependencies are instanced on demand, if the dependency is requested. The Container also supports singleton binding, through its BindSingleton method. Singleton dependencies are instanced once lazily, when requested for the first time. All further dependency requests receive this first instance. Both binding methods offer a variant, which panics on a failed bind.
Once all Dependencies are bound to the container. You may call Lock to prevent any more modification of the allowed dependencies. To resolve a dependency by its name, get the ResolverFunc by calling Resolver. You may use the Resolve or MustResolve helper functions to handle the type conversion for you.
func NewContainer ¶
func NewContainer() Container
NewContainer instantiates a generic Container, which can be filled with instanced or singleton dependencies, locked and queried for dependencies.
type ResolverFunc ¶
ResolverFunc is a generic function, used to request a dependency from a Container by its name. As the returned value is of any value, you may use the Resolve or MustResolve helper functions to handle the type conversion for you.