Documentation
¶
Overview ¶
Package di provides a lightweight dependency injection container for managing component lifecycle.
This package supports:
- Singleton and transient service lifecycles
- Interface-based registration and resolution
- Thread-safe concurrent access
- Factory functions and instance registration
- Automatic cleanup of closable services
Example usage:
container := di.New()
container.Register((*MyInterface)(nil), func(c *di.Container) (any, error) {
return NewMyService(), nil
}, true) // true = singleton
service, err := container.Resolve((*MyInterface)(nil))
Index ¶
- func IsDefaultRegisteredTyped[T any]() bool
- func IsRegistered(iface any) bool
- func IsRegisteredTyped[T any](c *Container) bool
- func MustResolve(iface any) any
- func MustResolveDefaultTyped[T any]() T
- func MustResolveTyped[T any](c *Container) T
- func ResetForTesting()
- func Resolve(iface any) (any, error)
- func ResolveDefaultTyped[T any]() (T, error)
- func ResolveTyped[T any](c *Container) (T, error)
- type Container
- func Clear() *Container
- func Default() *Container
- func New() *Container
- func NewWithOptions(opts ...Option) *Container
- func Register(iface any, factory func(*Container) (any, error), isSingleton bool) *Container
- func RegisterDefaultServices(services ...func(*Container) *Container) *Container
- func RegisterDefaultSingletonInstance[T any](instance T) *Container
- func RegisterInstance(iface any, instance any) *Container
- func RegisterSingleton(iface any, factory func(*Container) (any, error)) *Container
- func RegisterSingletonInstance[T any](c *Container, instance T) *Container
- func RegisterTransient(iface any, factory func(*Container) (any, error)) *Container
- func (c *Container) Clear() *Container
- func (c *Container) Close() error
- func (c *Container) IsRegistered(iface any) bool
- func (c *Container) MustResolve(iface any) any
- func (c *Container) Register(iface any, factory func(*Container) (any, error), isSingleton bool) *Container
- func (c *Container) RegisterInstance(iface any, instance any) *Container
- func (c *Container) RegisterServices(services ...func(*Container) *Container) *Container
- func (c *Container) RegisterSingleton(iface any, factory func(*Container) (any, error)) *Container
- func (c *Container) RegisterTransient(iface any, factory func(*Container) (any, error)) *Container
- func (c *Container) Resolve(iface any) (any, error)
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsDefaultRegisteredTyped ¶
IsDefaultRegisteredTyped checks if a service is registered with the default container with type safety
func IsRegistered ¶
IsRegistered checks if a service is registered with the default container
func IsRegisteredTyped ¶
IsRegisteredTyped checks if a service is registered with type safety
func MustResolve ¶
MustResolve resolves a service from the default container and panics if it fails
func MustResolveDefaultTyped ¶
func MustResolveDefaultTyped[T any]() T
MustResolveDefaultTyped resolves a service from the default container with type safety and panics if it fails
func MustResolveTyped ¶
MustResolveTyped resolves a service from the container with type safety and panics if it fails
func ResetForTesting ¶
func ResetForTesting()
ResetForTesting clears the global default container for testing. This must only be called from test code to ensure clean state between tests. Production code must never call this function.
func ResolveDefaultTyped ¶
ResolveDefaultTyped resolves a service from the default container with type safety
func ResolveTyped ¶
ResolveTyped resolves a service from the container with type safety
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container represents a dependency injection container. It manages service registration, lifecycle, and resolution with thread-safe access.
func Clear ¶
func Clear() *Container
Clear clears all registered services from the default container
func New ¶
func New() *Container
New creates a new dependency injection container. The container starts empty and requires explicit service registration before use.
func NewWithOptions ¶
NewWithOptions creates a new container with options
func RegisterDefaultServices ¶
RegisterDefaultServices registers multiple services at once with the default container
func RegisterDefaultSingletonInstance ¶
RegisterDefaultSingletonInstance registers a singleton instance with the default container
func RegisterInstance ¶
RegisterInstance registers an instance with the default container
func RegisterSingleton ¶
RegisterSingleton registers a singleton service with the default container
func RegisterSingletonInstance ¶
RegisterSingletonInstance registers a singleton instance
func RegisterTransient ¶
RegisterTransient registers a transient service with the default container
func (*Container) Close ¶ added in v1.1.3
Close closes all initialized singleton services that implement io.Closer. It returns a multi-error if any service fails to close.
func (*Container) IsRegistered ¶
IsRegistered checks if a service is registered
func (*Container) MustResolve ¶
MustResolve resolves a service from the container and panics if it fails
func (*Container) Register ¶
func (c *Container) Register(iface any, factory func(*Container) (any, error), isSingleton bool) *Container
Register registers a service with the container.
Parameters:
- iface: pointer to interface type (e.g., (*MyInterface)(nil))
- factory: function that creates the service instance
- isSingleton: if true, only one instance is created and reused; if false, new instance each time
Returns the container for method chaining.
func (*Container) RegisterInstance ¶
RegisterInstance registers an existing instance with the container. The instance is treated as a singleton and returned as-is on resolution.
Parameters:
- iface: pointer to interface type
- instance: the concrete instance to register
Returns the container for method chaining.
func (*Container) RegisterServices ¶
RegisterServices registers multiple services at once
func (*Container) RegisterSingleton ¶
RegisterSingleton registers a singleton service with the container. The service factory will be called only once, and the same instance is returned on every resolution.
func (*Container) RegisterTransient ¶
RegisterTransient registers a transient service with the container. A new instance is created every time the service is resolved.
type Option ¶
type Option func(*Container)
Option represents a container option
func WithInstance ¶
func WithInstance(iface interface{}, instance interface{}) Option
WithInstance registers an existing instance
func WithSingleton ¶
WithSingleton registers a service as a singleton
func WithTransient ¶
WithTransient registers a service as transient