Documentation
¶
Index ¶
- Variables
- func RegisterRequestDependencyType(valueOrType any)
- type Container
- func (c *Container) BindStructFields(val reflect.Value) error
- func (c *Container) Clone() *Container
- func (c *Container) Dependencies() []*Dependency
- func (c *Container) Invoke(value any) ([]any, error)
- func (c *Container) InvokeReturningFirst(value any) (any, error)
- func (c *Container) RegisterDependency(deps ...any) error
- func (c *Container) Resolve[T any]() (T, bool)
- func (c *Container) ResolveValue(typeOrValue any) (reflect.Value, bool)
- func (c *Container) VisitSingletons[T any](fn func(T))
- type Dependency
- type DependencyResolver
- type DependencyType
Constants ¶
This section is empty.
Variables ¶
var ErrDependencyNotFound = errors.New("dependency not found")
ErrDependencyNotFound is returned when a dependency is not found.
Functions ¶
func RegisterRequestDependencyType ¶
func RegisterRequestDependencyType(valueOrType any)
RegisterRequestDependencyType adds a new type to the request dependency types. It's useful when you want to add a custom type that can be resolved by the request's context. Call it before any dependency registration. It's not thread-safe.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is a dependency injection container. It is used to register and resolve dependencies. The dependencies are resolved in reverse order they are registered, starting from last to the first. See `RegisterDependency` and `Invoke` methods for more information.
func NewContainer ¶
func NewContainer() *Container
NewContainer creates a new DI Container. The container is used to register and resolve dependencies.
func (*Container) BindStructFields ¶
BindStructFields binds the fields of the given struct value. The struct value's fields are resolved by the registered dependencies. If a field is not found, an error is returned.
func (*Container) Dependencies ¶
func (c *Container) Dependencies() []*Dependency
Dependencies returns the dependencies of the container.
func (*Container) Invoke ¶
Invoke resolves and injects the given value. The value can be a function, a standard Go value or a pointer to a struct value. The value is resolved by the registered dependencies. If the value is a function, the input arguments are resolved by the registered dependencies. If the value is a standard Go value, it is returned as is. If the value is a pointer to a struct value, the struct value is resolved by the registered dependencies. If no matched dependencies are found, an error is returned.
func (*Container) InvokeReturningFirst ¶
InvokeReturningFirst same as `Invoke` but it returns only the first result.
func (*Container) RegisterDependency ¶
RegisterDependency registers a dependency with the container. The dependency can be a function, a standard Go value or a pointer to a struct value. The dependency can have any number of input and output arguments, but only one error output argument. The dependencies are resolved in reverse order they are registered, starting from last to the first. The resolved dependencies are passed as input arguments (or fields if struct) to the dep. The output arguments (or struct value) are stored and can be used by other dependencies. As an exception if dep is request dependency, then it's resolved only when it's called by the caller. A request dependency is when a function accepts at least one request type as input argument. The request types are: `context.Context`, `iris.Context`, `time.Time` and `*http.Request`, plus the routing and controller types registered by the core/router package. See `RegisterRequestDependencyType` to add more custom types.
If a dependency is not found, an error is returned. If a dependency is found, but the type does not match, an error is returned. If a dependency is found, but the value cannot be converted to the type, an error is returned.
func (*Container) Resolve ¶
Resolve resolves the dependency registered for the type T and returns it as T. It reports false when no registered dependency output matches T, when the resolved value cannot be converted to an interface or when it does not assert to T. Use `ResolveValue` for reflect-typed access.
func (*Container) ResolveValue ¶
ResolveValue resolves a dependency by type. If the type cannot be resolved by the registered dependencies, it returns false. If the dependency is found, it returns true and the value of the dependency.
Prefer the typed `Resolve` method when the type is known at compile-time; ResolveValue remains for callers that only hold a reflect.Type or an untyped value.
func (*Container) VisitSingletons ¶
VisitSingletons calls fn once for every resolved singleton output value of the registered dependencies, in registration order. Request-scoped dependencies are skipped: their values do not exist before a request. Comparable values (pointers included) are deduplicated, so a value that is registered directly and also returned by a constructor is visited once.
The type parameter T filters the visit: only values whose dynamic type asserts to T invoke fn. Pass a func(v any) to visit every singleton (T is inferred as any), or an interface-typed T to visit only the singletons implementing it.
type Dependency ¶
type Dependency struct {
// Source is the original value of the dependency.
Source reflect.Value
// DependencyResolver is the resolver of the dependency.
// It is used to resolve the dependency by given type(s).
DependencyResolver
}
Dependency represents a dependency. It holds the original value of the dependency and its resolver.
func NewDependency ¶
func NewDependency(dep any, dependencies []*Dependency) (*Dependency, error)
NewDependency creates a new dependency with the given value and dependencies. The value can be a function, a standard Go value or a pointer to a struct value. The resolved dependencies are passed as input arguments (or fields if struct) to the dep. The output arguments (or struct value) are stored and can be used by other dependencies.
type DependencyResolver ¶
type DependencyResolver interface {
// DependencyType returns the type of the dependency.
DependencyType() DependencyType
// Resolve resolves a dependency by type.
Resolve(toInput reflect.Type) (reflect.Value, bool)
}
DependencyResolver is an interface that resolves a dependency by type. It is used to resolve a dependency by given type(s).
type DependencyType ¶
type DependencyType uint8
DependencyType represents the type of a dependency. It is used to determine how a dependency is resolved. A dependency can be a singleton or a request dependency.
const ( // DependencyTypeSingleton is a dependency that is created once and shared // across all other dependencies and requests. DependencyTypeSingleton DependencyType = iota // DependencyTypeRequest is a dependency that is created once per request. // This is useful for creating a new instance of a dependency for each // request, but sharing that instance across all other dependencies. DependencyTypeRequest )