Documentation
¶
Overview ¶
Package di provides a dependency injection container for gokit applications.
It supports eager, lazy, and singleton registration modes with type-safe resolution using Go generics. The container enables decoupled architecture by managing service dependencies and their lifecycle.
Registration ¶
di.Register[MyService](container, func() (*MyService, error) {
return NewMyService(), nil
})
Resolution ¶
svc := di.MustResolve[MyService](container)
Index ¶
- Variables
- func GetTypedResolver[T any](container Container, name string) func() (T, error)
- func MustResolve[T any](c Container, key string) T
- func Resolve[T any](c Container, key string) (T, error)
- func ResolveTyped[T any](container Container, name string) (T, error)
- func TryResolve[T any](c Container, key string) (T, bool)
- type CircuitBreaker
- type CircuitBreakerConfig
- type CircuitState
- type ComponentRegistration
- type Container
- type LazyOption
- type PkgNames
- type RegistrationInfo
- type RegistrationMode
- type RetryPolicy
- type UnifiedContainer
- func (c *UnifiedContainer) Close() error
- func (c *UnifiedContainer) GetResolver(name string) func() (interface{}, error)
- func (c *UnifiedContainer) InvalidateCache(name string) error
- func (c *UnifiedContainer) MustResolve(name string) interface{}
- func (c *UnifiedContainer) Refresh(name string) (interface{}, error)
- func (c *UnifiedContainer) Register(key string, constructor interface{}) error
- func (c *UnifiedContainer) RegisterEager(key string, constructor interface{}) error
- func (c *UnifiedContainer) RegisterLazy(key string, constructor interface{}, options ...LazyOption) error
- func (c *UnifiedContainer) RegisterSingleton(key string, instance interface{}) error
- func (c *UnifiedContainer) Registrations() []RegistrationInfo
- func (c *UnifiedContainer) Resolve(key string) (interface{}, error)
Constants ¶
This section is empty.
Variables ¶
var Pkg = PkgNames{
Config: "config",
Logger: "logger",
Database: "database",
Redis: "redis",
Storage: "storage",
Workload: "workload",
ServiceRegistry: "service_registry",
ServiceDiscovery: "service_discovery",
HTTPServer: "http_server",
GRPCServer: "grpc_server",
UnifiedServer: "unified_server",
KafkaProducer: "kafka_producer",
KafkaConsumer: "kafka_consumer",
KafkaConsumerGroup: "kafka_consumer_group",
SSEHub: "sse_hub",
}
Pkg contains all component names for the pkg bootstrap layer.
Functions ¶
func GetTypedResolver ¶
GetTypedResolver returns a type-safe resolver function.
func MustResolve ¶
MustResolve resolves a component with type safety, panics on error. Use this in handlers when you need a dependency.
Example:
botRepo := di.MustResolve[contracts.BotRepository](h.container, shareddi.Shared.BotRepository)
func Resolve ¶
Resolve resolves a component with type safety, returns error on failure. Use this when you want to handle resolution errors gracefully.
Example:
botRepo, err := di.Resolve[contracts.BotRepository](c, shareddi.Shared.BotRepository)
if err != nil {
return fmt.Errorf("failed to get bot repository: %w", err)
}
func ResolveTyped ¶
ResolveTyped provides type-safe resolution with generics.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
func NewCircuitBreaker ¶
func NewCircuitBreaker(config *CircuitBreakerConfig) *CircuitBreaker
func (*CircuitBreaker) IsOpen ¶
func (cb *CircuitBreaker) IsOpen() bool
func (*CircuitBreaker) RecordFailure ¶
func (cb *CircuitBreaker) RecordFailure()
func (*CircuitBreaker) RecordSuccess ¶
func (cb *CircuitBreaker) RecordSuccess()
type CircuitBreakerConfig ¶
type CircuitState ¶
type CircuitState int
const ( CircuitClosed CircuitState = iota CircuitOpen CircuitHalfOpen )
type ComponentRegistration ¶
type ComponentRegistration struct {
// contains filtered or unexported fields
}
type Container ¶
type Container interface {
Register(key string, constructor interface{}) error
RegisterLazy(key string, constructor interface{}, options ...LazyOption) error
RegisterEager(key string, constructor interface{}) error
Resolve(key string) (interface{}, error)
RegisterSingleton(key string, instance interface{}) error
Close() error
// Introspection
Registrations() []RegistrationInfo
// Legacy methods for backward compatibility
InvalidateCache(name string) error
Refresh(name string) (interface{}, error)
GetResolver(name string) func() (interface{}, error)
MustResolve(name string) interface{}
}
Container defines the interface for a dependency injection container
func NewContainer ¶
func NewContainer() Container
func NewSimpleContainer ¶
func NewSimpleContainer() Container
NewSimpleContainer creates the new unified container (backward compatibility)
type LazyOption ¶
type LazyOption func(*ComponentRegistration)
func WithCircuitBreaker ¶
func WithCircuitBreaker(config *CircuitBreakerConfig) LazyOption
func WithRetryPolicy ¶
func WithRetryPolicy(policy *RetryPolicy) LazyOption
Helper functions and options
type PkgNames ¶
type PkgNames struct {
// Core infrastructure
Config string
Logger string
Database string
Redis string
Storage string
Workload string
ServiceRegistry string
ServiceDiscovery string
// HTTP/gRPC servers
HTTPServer string
GRPCServer string
UnifiedServer string
// Messaging
KafkaProducer string
KafkaConsumer string
KafkaConsumerGroup string
// Real-time
SSEHub string
}
PkgNames defines the base layer component names for the pkg bootstrap layer. Projects embed this struct in their own shared/service DI names.
type RegistrationInfo ¶
type RegistrationInfo struct {
Key string
Mode RegistrationMode // Eager, Lazy, or Singleton
Initialized bool
}
RegistrationInfo describes a registered component for introspection.
type RegistrationMode ¶
type RegistrationMode int
RegistrationMode determines how a component should be resolved
const ( Eager RegistrationMode = iota // Initialize immediately on registration Lazy // Initialize on first resolve Singleton // Pre-created instance )
type RetryPolicy ¶
type UnifiedContainer ¶
type UnifiedContainer struct {
// contains filtered or unexported fields
}
UnifiedContainer is our single, unified DI container
func (*UnifiedContainer) Close ¶
func (c *UnifiedContainer) Close() error
func (*UnifiedContainer) GetResolver ¶
func (c *UnifiedContainer) GetResolver(name string) func() (interface{}, error)
func (*UnifiedContainer) InvalidateCache ¶
func (c *UnifiedContainer) InvalidateCache(name string) error
Legacy methods for backward compatibility
func (*UnifiedContainer) MustResolve ¶
func (c *UnifiedContainer) MustResolve(name string) interface{}
func (*UnifiedContainer) Refresh ¶
func (c *UnifiedContainer) Refresh(name string) (interface{}, error)
func (*UnifiedContainer) Register ¶
func (c *UnifiedContainer) Register(key string, constructor interface{}) error
Register component with lazy loading by default (most common case)
func (*UnifiedContainer) RegisterEager ¶
func (c *UnifiedContainer) RegisterEager(key string, constructor interface{}) error
RegisterEager registers a component for immediate initialization
func (*UnifiedContainer) RegisterLazy ¶
func (c *UnifiedContainer) RegisterLazy(key string, constructor interface{}, options ...LazyOption) error
RegisterLazy registers a component for lazy initialization
func (*UnifiedContainer) RegisterSingleton ¶
func (c *UnifiedContainer) RegisterSingleton(key string, instance interface{}) error
RegisterSingleton registers a pre-created instance
func (*UnifiedContainer) Registrations ¶
func (c *UnifiedContainer) Registrations() []RegistrationInfo
Registrations returns info about all registered components for introspection.
func (*UnifiedContainer) Resolve ¶
func (c *UnifiedContainer) Resolve(key string) (interface{}, error)
Resolve gets a component instance