Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Default = New(newAdapter(container.Default))
Default is the default concrete of the service provider manager.
var ErrNilScopeValue = errors.New("provider: scope value must not be nil")
ErrNilScopeValue is returned when a nil value is passed to WithValue. The container binds values by their reflected type, which cannot be determined from an untyped nil.
Functions ¶
func DefaultOptions ¶
func DefaultOptions() *options
DefaultOptions returns a new instance of options with default values.
Types ¶
type Container ¶
type Container interface {
// Reset calls the same method of the default concrete.
Reset()
// Bind calls the same method of the default concrete.
Bind(receiver any, opts ...bind.BindOption) error
// Call calls the same method of the default concrete.
Call(receiver any, opts ...resolve.ResolveOption) error
// Resolve calls the same method of the default concrete.
Resolve(abstraction any, opts ...resolve.ResolveOption) error
// Fill calls the same method of the default concrete.
Fill(receiver any, opts ...resolve.ResolveOption) error
// Scope creates a new child container with the given name, which can be used to manage scoped dependencies.
Scope(name string) Container
// Derive creates a new child container that inherits the binding of the parent container.
Derive() Container
}
Container defines the interface for a dependency injection container.
type HasOrder ¶
type HasOrder interface {
// Order determines the execution order of the provider.
// Providers with lower order values are registered and booted before those with higher values.
// 1- first register from lower to higher.
// 2- then boot from lower to higher.
// 3- finally terminate from higher to lower. (reverse order for termination)
Order() int
}
HasOrder is an optional interface that providers can implement to specify their execution order.
type HasScope ¶ added in v1.1.0
type HasScope interface {
// Scoped reports whether the provider should be registered as a scoped provider.
Scoped() bool
}
HasScope is an optional interface that providers can implement to opt into scoped execution. When Register receives a provider whose Scoped method returns true, the provider is run per-scope (by Scope/Derive against a child container) instead of at global boot.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages the lifecycle of service providers, including their registration, booting, and termination.
func (*Manager) Register ¶
Register registers a service provider with the service provider manager. Providers that implement HasScope and return true are stored as scoped providers (run per-scope); all others run at global boot.
func (*Manager) Run ¶
Run executes the service provider manager, which involves booting all registered providers and handling their termination.
func (*Manager) Scope ¶ added in v1.1.0
Scope opens a scoped instance of the container and runs the manager's scoped providers against it. By default the scope is anonymous and ephemeral (container.Derive), becoming eligible for garbage collection once the caller drops the returned Scope; WithPersistent makes it a named, persistent child instead. Any WithValue options seed the child before the scoped providers' Register then Boot. The caller owns the returned Scope and must Terminate it when the scope ends, unless WithAutoTermination ties teardown to ctx.
On any error the scope is not returned; matching the manager's global Run, already-booted providers are not terminated here.
type Option ¶
type Option func(*options)
Option is a function that configures the service provider manager.
func WithCallback ¶
WithCallback sets the callback function that will be called with the context and container when the manager starts running.
func WithTerminationDeadline ¶
WithTerminationDeadline sets the duration to wait for providers to terminate before forcing termination.
func WithTerminationDelay ¶
WithTerminationDelay sets the duration to wait for providers to terminate gracefully before forcing termination.
type Provider ¶
type Provider interface {
// Register registers the provider's services into the container.
// This method is called during the application's initialization phase.
Register(ctx context.Context, container Container) error
// Boot boots the provider, which is called after all providers have been registered.
// This method is used to perform any initialization tasks that require access to other providers.
Boot(ctx context.Context, container Container) error
// Terminate terminates the provider, which is called before the application exits.
// This method is used to release resources or perform cleanup tasks.
Terminate(ctx context.Context) error
}
Provider defines the interface for a service provider.
type Scope ¶ added in v1.1.0
type Scope struct {
// contains filtered or unexported fields
}
Scope is a live scoped instance: a child container seeded with the WithValue values and with the manager's scoped providers already registered and booted. Call Terminate to tear the scoped providers down in reverse order (unless WithAutoTermination was set, which does this for you on context cancellation).
func (*Scope) Name ¶ added in v1.1.0
Name returns the scope name, or an empty string for an ephemeral scope.
type ScopeOption ¶ added in v1.1.0
type ScopeOption func(*scopeConfig)
ScopeOption configures a scoped instance of the container.
func WithAutoTermination ¶ added in v1.1.0
func WithAutoTermination() ScopeOption
WithAutoTermination makes the scope terminate itself once the context passed to Scope is cancelled, releasing the caller from calling Terminate. Teardown runs exactly once, whether triggered by the context or by an explicit Terminate, so combining the two is safe.
func WithPersistent ¶ added in v1.1.0
func WithPersistent(name string) ScopeOption
WithPersistent makes the scope a named, persistent child of the manager's container (container.Scope) rather than the default anonymous, ephemeral one (container.Derive). The named child is cached on its parent and reused by later calls with the same name.
func WithValue ¶ added in v1.1.0
func WithValue(name string, value any) ScopeOption
WithValue seeds the scoped container with value, resolvable by name. The value is bound as a named singleton, so scoped providers (and anything else resolving from the scope) can retrieve it via resolve.WithName(name).