godi

package module
v0.0.0-...-d336bbc Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2022 License: MIT Imports: 3 Imported by: 0

README

Go-DI

Go-DI 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 godi.NewContainer(). Now dependencies can be bound to the container either instantiating or as a singleton and queried from 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())

Types of bound dependencies

Go-DI differentiates two types of dependencies: instantiating and singleton dependencies.

Instantiating Dependencies

Instantiating Dependencies are instantiated every time, they are requested from the dependency container. Every request form the dependency container will therefore yield a new instance.

container.MustBind("rng", func(resolver godi.ResolverFunc) any {
    return rand.Int63()
})
resolver := container.Resolver()

// Will yield different results for all DI requests
fmt.Println(godi.MustResolve[int64]("rng", resolver))
fmt.Println(godi.MustResolve[int64]("rng", resolver))
fmt.Println(godi.MustResolve[int64]("rng", resolver))
Singleton Dependencies

Singleton Dependencies are instantiated only once. All subsequent requests to the dependency container will yield the first and only instantiated instance.

container.MustBindSingleton("rng-once", func(resolver godi.ResolverFunc) any {
    return rand.Int63()
})
resolver := container.Resolver()

// Will yield the same result for all DI requests
fmt.Println(godi.MustResolve[int64]("rng-once", resolver))
fmt.Println(godi.MustResolve[int64]("rng-once", resolver))
fmt.Println(godi.MustResolve[int64]("rng-once", resolver))

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

type ResolverFunc = func(string) (any, error)

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.

Jump to

Keyboard shortcuts

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