injector

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 8 Imported by: 0

README

Injector

A lightweight, high-performance dependency injection library for Go.

Overview

injector is a compile-time-safe dependency injection container designed for Go applications that need reliable, fast dependency resolution at startup. It provides multiple caching strategies to optimize for different access patterns and supports flexible lifecycle management for registered types.

Features

  • Type-Safe API: Generic functions for compile-time type safety
  • Multiple Lifecycle Options: Singleton, Scope, and Transient lifecycles
  • Error Handling: Constructors can optionally return an error value
  • Flexible Registration: Register by type with custom constructors
  • Named Lookups: Retrieve dependencies by type name
  • Caching Strategies: Choose between Map, BubbleList, and PriorityList caches
  • Dependency Verification: Validate your dependency graph before runtime
  • Scoped Instances: Create isolated scopes for request-specific dependencies
  • Function Injection: Automatically inject dependencies into functions

Installation

go get github.com/smarty/injector

Quick Start

Basic Usage
package main

import (
	"github.com/smarty/injector"
)

type Database interface {
	Query(sql string) ([]string, error)
}

type MySQLDB struct{}

func (d *MySQLDB) Query(sql string) ([]string, error) {
	// Implementation
	return []string{}, nil
}

func main() {
	di := injector.New()

	// Register a singleton instance
	injector.RegisterSingleton[Database](di, func() Database {
		return &MySQLDB{}
	})

	// Verify the dependency graph
	if err := injector.Verify(di); err != nil {
		panic(err)
	}

	// Retrieve the database
	db, err := injector.Get[Database](di)
	if err != nil {
		panic(err)
	}

	// Use the database
	_ = db
}
Lifecycle Options
Singleton

Instantiated once and reused across the entire application lifetime.

injector.RegisterSingleton[MyType](di, constructor)
Scope

Instantiated once per Get() call, allowing for request-scoped or operation-scoped instances.

injector.RegisterScope[MyType](di, constructor)
Transient

Instantiated every time it's requested as a dependency.

injector.RegisterTransient[MyType](di, constructor)
Error Handling in Constructors

Constructors can return an error in addition to the instance:

injector.RegisterSingletonError[MyType](di, func() (MyType, error) {
	// Return instance and error
	return MyType{}, nil
})
Function Injection

Automatically inject dependencies into functions:

func setupDatabase(db Database) error {
	// Setup logic
	return nil
}

// Call the function with injected dependencies
err := injector.Call(di, setupDatabase)
Named Lookups

Register types that can be retrieved by name:

db, err := injector.GetByName(di, "Database")
Caching Strategies

Choose the caching strategy that best fits your access patterns:

// Map: Good for random access patterns (default)
di := injector.New(injector.Map)

// BubbleList: Good for stable access patterns
di := injector.New(injector.BubbleList)

// PriorityList: Good for stable but changing access patterns
di := injector.New(injector.PriorityList)

API Reference

Core Methods
  • New(cacheStrategy ...CacheStrategy) *Injector: Create a new injector instance
  • Get[T](di *Injector) (T, error): Retrieve a dependency by type
  • GetByName(di *Injector, name string) (any, error): Retrieve a dependency by name
  • Call(di *Injector, function any) error: Call a function with injected dependencies
  • Call1 through Call4: Call functions with specific return value counts
  • CallN(di *Injector, function any) ([]any, error): Call a function with any number of returns
  • Verify(di *Injector) error: Validate the dependency graph
Registration Methods
  • RegisterSingleton[T](di *Injector, constructor any) error: Register a singleton
  • RegisterSingletonError[T](di *Injector, constructor any) error: Register a singleton with error handling
  • RegisterScope[T](di *Injector, constructor any) error: Register a scoped instance
  • RegisterScopeError[T](di *Injector, constructor any) error: Register a scoped instance with error handling
  • RegisterTransient[T](di *Injector, constructor any) error: Register a transient instance
  • RegisterTransientError[T](di *Injector, constructor any) error: Register a transient with error handling

Error Handling

The injector provides specific error types for different failure scenarios:

  • ErrorAlreadyRegistered: A type has already been registered
  • ErrorBadState: Injector is in an invalid state for the requested operation
  • ErrorDependencyLoop: A circular dependency has been detected
  • ErrorNotRegistered: A required dependency has not been registered
  • ErrorNotStructOrInterface: A type is not suitable for registration
  • ErrorVariadicArguments: A function has a variadic signature

Performance Considerations

  • The injector is optimized for startup-time usage. Generating dependencies takes a few microseconds per call.
  • Choose a caching strategy based on your access patterns:
    • Map: Random access, no reordering overhead
    • BubbleList: Stable patterns, benefits from reordering on stable workloads
    • PriorityList: Changing patterns that settle over time

Testing

The library includes comprehensive tests covering:

  • Type registration and validation
  • Dependency resolution
  • Circular dependency detection
  • Error handling
  • Performance benchmarks

Run tests with:

go test ./...

Run benchmarks with:

go test -bench=. ./...

Roadmap

Planned Features
  • Struct Tag Support for Automatic Field Injection - Allow dependency injection directly into struct fields via tags (e.g., di:"inject").

  • Factory Pattern Support - Register factory functions that can take parameters to create multiple instances with different configurations. Useful for creating variants of the same type based on input parameters.

License

MIT License - See LICENSE for details

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// InjectorError is a generic injector error, usable for detecting if an
	// error was injector related.
	InjectorError = errors.New("injector error")

	// ErrorAlreadyRegistered is returned when a type has already been
	// registered.
	ErrorAlreadyRegistered = fmt.Errorf("%w, already registered", InjectorError)

	// ErrorBadState is a panicking error when an access attempt is made on an
	// injector that is in a bad state.
	ErrorBadState = fmt.Errorf("%w, bad injector state", InjectorError)

	// ErrorDependencyLoop indicates that an unsolvable dependency injection
	// loop.
	ErrorDependencyLoop = fmt.Errorf("%w, dependency loop detected", InjectorError)

	// ErrorNoReturns is returned when a constructor has no return value.
	ErrorNoReturns = fmt.Errorf("%w, no return values, must be exactly 1 return value", InjectorError)

	// ErrorNotAFunction is returned when a non-function is passed as a function.
	ErrorNotAFunction = fmt.Errorf("%w, value is not a function", InjectorError)

	// ErrorNotAssignable is returned when a constructor returns a type that
	// cannot be assigned to the key type.
	ErrorNotAssignable = fmt.Errorf("%w, value is not assignable", InjectorError)

	// ErrorNotRegistered indicates that a required dependency does not appear
	// in the registered list.
	ErrorNotRegistered = fmt.Errorf("%w, not registered", InjectorError)

	// ErrorNotStructOrInterface is returned when a type is not registerable.
	ErrorNotStructOrInterface = fmt.Errorf("%w, key type is not a struct or interface", InjectorError)

	// ErrorTooManyReturns is returned when a constructor has more than 1 return
	// value.
	ErrorTooManyReturns = fmt.Errorf("%w, too many return values, must be exactly 1 return value", InjectorError)

	// ErrorVariadicArguments is returned when a function has a variadic
	// signature.
	ErrorVariadicArguments = fmt.Errorf("%w, function has a variadic signature", InjectorError)

	// ErrorWrongNumberOfReturns is returned when a function has more or less
	// than the expected number of return values.
	ErrorWrongNumberOfReturns = fmt.Errorf("%w, wrong number of return values", InjectorError)
)

Functions

func Call

func Call(injector *Injector, function any) (err error)

Call checks a function's signature then calls the function by injecting all the arguments. Call is used for any function that has no return values.

Parameters:

  • injector is the dependency injector to use when making the function call.
  • function is the function to be called with injected arguments.

Returns:

  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func Call1

func Call1[T1 any](injector *Injector, function any) (r1 T1, err error)

Call1 checks a function's signature then calls the function by injecting all the arguments. Call1 is used for any function that has exactly one return value.

Parameters:

  • injector is the dependency injector to use when making the function call.
  • function is the function to be called with injected arguments.

Returns:

  • r1 is return value 1.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func Call2

func Call2[T1, T2 any](injector *Injector, function any) (r1 T1, r2 T2, err error)

Call2 checks a function's signature then calls the function by injecting all the arguments. Call2 is used for any function that has exactly two return values.

Parameters:

  • injector is the dependency injector to use when making the function call.
  • function is the function to be called with injected arguments.

Returns:

  • r1 is return value 1.
  • r2 is return value 2.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func Call3

func Call3[T1, T2, T3 any](injector *Injector, function any) (r1 T1, r2 T2, r3 T3, err error)

Call3 checks a function's signature then calls the function by injecting all the arguments. Call3 is used for any function that has exactly three return values.

Parameters:

  • injector is the dependency injector to use when making the function call.
  • function is the function to be called with injected arguments.

Returns:

  • r1 is return value 1.
  • r2 is return value 2.
  • r3 is return value 3.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func Call4

func Call4[T1, T2, T3, T4 any](injector *Injector, function any) (r1 T1, r2 T2, r3 T3, r4 T4, err error)

Call4 checks a function's signature then calls the function by injecting all the arguments. Call4 is used for any function that has exactly four return values.

Parameters:

  • injector is the dependency injector to use when making the function call.
  • function is the function to be called with injected arguments.

Returns:

  • r1 is return value 1.
  • r2 is return value 2.
  • r3 is return value 3.
  • r4 is return value 4.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func CallN

func CallN(injector *Injector, function any) (returns []any, err error)

CallN checks a function's signature then calls the function by injecting all the arguments. CallN is used for any function with any number of return values.

Parameters:

  • injector is the dependency injector to use when making the function call.
  • function is the function to be called with injected arguments.

Returns:

  • returns contains all return values.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.

func Get

func Get[Tkey any](injector *Injector) (value Tkey, err error)

Get retrieves the given type using the registered constructor or instance.

Parameters:

  • injector is the dependency injector to get the instance from.

Returns:

  • value is the registered instance or the result of the registered constructor.
  • err is nil unless an error occurred during retrieval.

Errors:

  • if Verify() has not been called.
  • if Verify() returned an error.

func GetByName

func GetByName(injector *Injector, name string) (value any, err error)

GetByName retrieves the named type using the registered constructor or instance. The name is expected to be truncated down to type name only, package should not be included in the name. Pointer "*" symbols are also assumed to be stripped from the name and should not be included.

Parameters:

  • injector is the dependency injector to get the instance from.
  • name is the name of the type that should have been registered. Expected to be truncated down to type name only, package should not be included. Pointer "*" symbols are also assumed to be stripped from the name and should not be included.

Returns:

  • value is the registered instance or the result of the registered constructor.
  • err is nil unless the named type cannot be found.

Errors:

  • ErrorNotRegistered is returned if the named type cannot be found.

Errors:

  • if Verify() has not been called.
  • if Verify() returned an error.

func RegisterScope

func RegisterScope[Tkey any](target *Injector, constructor any) error

RegisterScope adds a constructor for the given type. Every time the type is requested in a unique Get() call, the same instance is always returned. If it's requested again in a new Get() call, the constructor is called and a new instance is returned.

Notes:

  • Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use RegisterScopeError instead.

Parameters:

  • target is the Injector to register the type in.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func RegisterScopeError

func RegisterScopeError[Tkey any](target *Injector, constructor any) error

RegisterScopeError adds a constructor for the given type. Every time the type is requested in a unique Get() call, the same instance is always returned. If it's requested again in a new Get() call, the constructor is called and a new instance is returned.

Notes:

  • Constructor is expected to return (Tkey, error). If your constructor does not return an error, use RegisterScope instead.

Parameters:

  • target is the Injector to register the type in.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func RegisterSingleton

func RegisterSingleton[Tkey any](target *Injector, constructor any) error

RegisterSingleton adds a constructor for the given type. Every time the type is requested, the same instance is always returned.

Notes:

  • Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use RegisterSingletonError

Parameters:

  • target is the Injector to register the type in.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func RegisterSingletonError

func RegisterSingletonError[Tkey any](target *Injector, constructor any) error

RegisterSingletonError adds a constructor for the given type. Every time the type is requested, the same instance is always returned.

Notes:

  • Constructor is expected to return (Tkey, error). If your constructor does not return an error, use RegisterSingleton instead.

Parameters:

  • target is the Injector to register the type in.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func RegisterTransient

func RegisterTransient[Tkey any](target *Injector, constructor any) error

RegisterTransient adds a constructor for the given type. Every time the type is requested, the constructor is always called and a new instance is returned.

Notes:

  • Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use RegisterTransientError

Parameters:

  • target is the Injector to register the type in.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func RegisterTransientError

func RegisterTransientError[Tkey any](target *Injector, constructor any) error

RegisterTransientError adds a constructor for the given type. Every time the type is requested, the constructor is always called and a new instance is returned.

Notes:

  • Constructor is expected to return (Tkey, error). If your constructor does not return an error, use RegisterTransient instead.

Parameters:

  • target is the Injector to register the type in.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func Verify

func Verify(injector *Injector) error

Verify examines all registered types and their corresponding constructors and validates them, otherwise an error is returned.

Parameters:

  • target is the Injector to explore and verify all the registered types in.

Errors:

  • ErrorDependencyLoop indicates that an unsolvable dependency injection loop.
  • ErrorNotRegistered indicates that a required dependency does not appear in the registered list.

Types

type CacheStrategy

type CacheStrategy int

CachingStrategy is any one of a few predefined type-caching backends for the injector. Each caching strategy has its strengths and weaknesses.

const (
	// Map uses a Go map for the cache. Good for truly random access.
	Map CacheStrategy = iota

	// BubbleList uses a slice that reorders based on the number of times
	// each element is accessed. Good for highly stable access patterns that
	// don't change during runtime.
	BubbleList

	// PriorityList uses a linked-list that promotes an item all the way to
	// the front whenever it is accessed. Good for fairly stable access
	// patterns that can change over time.
	PriorityList
)

type Injector

type Injector struct {
	// contains filtered or unexported fields
}

Injector is a dependency-injector meant to be single-use at startup and used in applications where taking a few microseconds generating a dependency is acceptable.

func New

func New(cacheStrategy ...CacheStrategy) *Injector

New creates a new injector, preloaded with itself.

Parameters:

  • cacheStrategy defines an optional internal caching strategy. If no caching strategy is chosen, the injector defaults to using Map. If more than one strategy is selected, the first strategy will be used.

Returns:

  • Injector with self already registered as a singleton.

func (*Injector) Call

func (this *Injector) Call(function any) (err error)

Call checks a function's signature then calls the function by injecting all the arguments. Call is used for any function that has no return values.

Parameters:

  • function is the function to be called with injected arguments.

Returns:

  • err returns any error encountered during the call.

Error:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func (*Injector) Call1

func (this *Injector) Call1(function any) (r1 any, err error)

Call1 checks a function's signature then calls the function by injecting all the arguments. Call1 is used for any function that has exactly one return value.

Parameters:

  • function is the function to be called with injected arguments.

Returns:

  • r1 is return value 1.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func (*Injector) Call2

func (this *Injector) Call2(function any) (r1, r2 any, err error)

Call2 checks a function's signature then calls the function by injecting all the arguments. Call2 is used for any function that has exactly two return values.

Parameters:

  • function is the function to be called with injected arguments.

Returns:

  • r1 is return value 1.
  • r2 is return value 2.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func (*Injector) Call3

func (this *Injector) Call3(function any) (r1, r2, r3 any, err error)

Call3 checks a function's signature then calls the function by injecting all the arguments. Call3 is used for any function that has exactly three return values.

Parameters:

  • function is the function to be called with injected arguments.

Returns:

  • r1 is return value 1.
  • r2 is return value 2.
  • r3 is return value 3.
  • err returns any error encountered during the call.

Returns:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func (*Injector) Call4

func (this *Injector) Call4(function any) (r1, r2, r3, r4 any, err error)

Call4 checks a function's signature then calls the function by injecting all the arguments. Call4 is used for any function that has exactly four return values.

Parameters:

  • function is the function to be called with injected arguments.

Returns:

  • r1 is return value 1.
  • r2 is return value 2.
  • r3 is return value 3.
  • r4 is return value 4.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.
  • if the function provided has an incongruent number of return values.

func (*Injector) CallN

func (this *Injector) CallN(function any) (returns []any, err error)

CallN checks a function's signature then calls the function by injecting all the arguments. CallN is used for any function with any number of return values.

Parameters:

  • function is the function to be called with injected arguments.

Returns:

  • returns contains all return values.
  • err returns any error encountered during the call.

Errors:

  • if calling Get on any of the argument types would error.
  • if the function provided is not a function.
  • if the function provided is variadic.

func (*Injector) Get

func (this *Injector) Get(key reflect.Type) (value any, err error)

Get retrieves the given type using the registered constructor or instance.

Parameters:

  • key is the type to look for a registered instance or constructor for.

Returns:

  • The registered instance or the result of the registered constructor.
  • err is nil unless an error occurred during retrieval.

Errors:

  • if Verify() has not been called.
  • if Verify() returned an error.

func (*Injector) GetByName

func (this *Injector) GetByName(name string) (value any, err error)

GetByName retrieves the named type using the registered constructor or instance. The name is expected to be truncated down to type name only, package should not be included in the name. Pointer "*" symbols are also assumed to be stripped from the name and should not be included.

Parameters:

  • name is the name of the type that should have been registered. Expected to be truncated down to type name only, package should not be included. Pointer "*" symbols are also assumed to be stripped from the name and should not be included.

Returns:

  • value is the registered instance or the result of the registered constructor.
  • err is nil unless the named type cannot be found.

Errors:

  • ErrorNotRegistered is returned if the named type cannot be found.

Errors:

  • if Verify() has not been called.
  • if Verify() returned an error.

func (*Injector) RegisterScope

func (this *Injector) RegisterScope(key reflect.Type, constructor any) error

RegisterScope adds a constructor for the given type. Every time the type is requested in a unique Get() call, the same instance is always returned. If it's requested again in a new Get() call, the constructor is called and a new instance is returned.

Notes:

  • Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use Injector.RegisterScopeError.

Parameters:

  • key is the registered type that the constructor will be registered with.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func (*Injector) RegisterScopeError

func (this *Injector) RegisterScopeError(key reflect.Type, constructor any) error

RegisterScopeError adds a constructor for the given type. Every time the type is requested in a unique Get() call, the same instance is always returned. If it's requested again in a new Get() call, the constructor is called and a new instance is returned.

Notes:

  • Constructor is expected to return (Tkey, error). If your constructor does not return an error, use Injector.RegisterScope instead.

Parameters:

  • key is the registered type that the constructor will be registered with.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func (*Injector) RegisterSingleton

func (this *Injector) RegisterSingleton(key reflect.Type, constructor any) error

RegisterSingleton adds a constructor for the given type. Every time the type is requested, the same instance is always returned.

Notes:

Parameters:

  • key is the registered type that the constructor will be registered with.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func (*Injector) RegisterSingletonError

func (this *Injector) RegisterSingletonError(key reflect.Type, constructor any) error

RegisterSingletonError adds a constructor for the given type. Every time the type is requested, the same instance is always returned.

Notes:

  • Constructor is expected to return (Tkey, error). If your constructor does not return an error, use Injector.RegisterSingleton instead.

Parameters:

  • key is the registered type that the constructor will be registered with.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func (*Injector) RegisterTransient

func (this *Injector) RegisterTransient(key reflect.Type, constructor any) error

RegisterTransient adds a constructor for the given type. Every time the type is requested, the constructor is always called and a new instance is returned.

Notes:

Parameters:

  • key is the registered type that the constructor will be registered with.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

func (*Injector) RegisterTransientError

func (this *Injector) RegisterTransientError(key reflect.Type, constructor any) error

RegisterTransientError adds a constructor for the given type. Every time the type is requested, the constructor is always called and a new instance is returned.

Notes:

  • Constructor is expected to return (Tkey, error). If your constructor does not return an error, use Injector.RegisterTransient instead.

Parameters:

  • key is the registered type that the constructor will be registered with.
  • constructor is the requisite function to generate the type.

Errors:

  • ErrorAlreadyRegistered is returned when a type has already been registered.
  • ErrorNoReturns is returned when a constructor has no return value.
  • ErrorNotAFunction is returned when a non-function is passed as a constructor.
  • ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
  • ErrorNotStructOrInterface is returned when a type is not registerable.
  • ErrorTooManyReturns is returned when a constructor has more than 1 return value.
  • ErrorVariadicArguments is returned when a constructor has a variadic signature.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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