Documentation
¶
Index ¶
- func AsType[T any](c *Container) []T
- func Get[T any](c *Container) (T, bool)
- func GetNamed[T any](c *Container, name string) (T, bool)
- func Has[T any](c *Container) bool
- func HasNamed[T any](c *Container, name string) bool
- func OfType[T any](c *Container) []T
- func Remove[T any](c *Container) bool
- func RemoveNamed[T any](c *Container, name string) bool
- func Set(c *Container, object any)
- func SetAs[T any](c *Container, object T)
- func SetNamed(c *Container, name string, object any)
- type Application
- func (a *Application[C]) Build() error
- func (a *Application[C]) Configure(path string, opts ...*dd.Options) error
- func (a *Application[C]) Initialize(configPaths ...string) error
- func (a *Application[C]) InitializeWithOptions(opts *dd.Options, configPaths ...string) error
- func (a *Application[C]) Link() error
- func (a *Application[C]) Start() error
- func (a *Application[C]) Stop() error
- type Container
- type Factory
- type FactoryFunc
- type InspectData
- type InspectFormat
- type InspectObject
- type InspectSummary
- type Linkable
- type Startable
- type Stoppable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsType ¶
AsType visits all objects in the container and returns any that can be cast to type T. This enables finding objects by interface or supertype regardless of their registration type.
func Get ¶
Get retrieves an object of type T from the container. Returns the object and true if found, or zero value and false if not found.
func GetNamed ¶
GetNamed retrieves a named object of type T from the container. Returns the object and true if found, or zero value and false if not found.
func HasNamed ¶
HasNamed checks if a named object of type T with the given name exists in the container.
func OfType ¶
OfType retrieves all objects of type T from the container (both singleton and named). Returns a slice containing the singleton (if exists) followed by all named instances.
func Remove ¶
Remove removes an object of type T from the container. Returns true if the object was found and removed, false if it didn't exist.
func RemoveNamed ¶
RemoveNamed removes a named object of type T with the given name from the container. Returns true if the object was found and removed, false if it didn't exist.
func Set ¶
Set registers a singleton object in the container by its type. If an object of the same type already exists, it will be replaced.
Types ¶
type Application ¶
type Application[C any] struct { Cfg C // configuration object C *Container // dependency injection container Factories []Factory[C] // factories for creating and registering objects }
Application orchestrates the lifecycle of a dependency injection container with configuration. It manages object creation through factories, dependency linking, startup, and shutdown phases.
func NewApplication ¶
func NewApplication[C any](cfg C) *Application[C]
NewApplication creates a new application with the given configuration. The configuration object is automatically registered in the container.
func WithFactory ¶
func WithFactory[C any](a *Application[C], f Factory[C]) *Application[C]
WithFactory adds a factory to the application for fluent configuration. Returns the application to enable method chaining.
func WithFactoryFunc ¶
func WithFactoryFunc[C any](a *Application[C], f func(a *Application[C]) error) *Application[C]
WithFactoryFunc adds a function factory to the application for fluent configuration. Returns the application to enable method chaining.
func (*Application[C]) Build ¶
func (a *Application[C]) Build() error
Build executes all registered factories to create and register objects in the container. Factories are responsible for calling SetAs[T]() to register their created objects.
func (*Application[C]) Configure ¶
func (a *Application[C]) Configure(path string, opts ...*dd.Options) error
Configure loads additional configuration from a file and merges it with the existing configuration. Supports JSON and YAML file formats based on file extension.
func (*Application[C]) Initialize ¶
func (a *Application[C]) Initialize(configPaths ...string) error
Initialize executes Configure, Build, and Link phases in sequence. Returns on first error without proceeding to subsequent phases.
func (*Application[C]) InitializeWithOptions ¶
func (a *Application[C]) InitializeWithOptions(opts *dd.Options, configPaths ...string) error
InitializeWithOptions executes Configure, Build, and Link phases in sequence with custom options. Returns on first error without proceeding to subsequent phases.
func (*Application[C]) Link ¶
func (a *Application[C]) Link() error
Link establishes dependencies between objects by calling Link() on all Linkable objects. This phase occurs after Build() to ensure all objects exist before dependency resolution. Returns the first error encountered, which stops the linking process.
func (*Application[C]) Start ¶
func (a *Application[C]) Start() error
Start initializes all Startable objects after linking is complete. Returns the first error encountered, which stops the startup process.
func (*Application[C]) Stop ¶
func (a *Application[C]) Stop() error
Stop shuts down all Stoppable objects for graceful cleanup. Returns the first error encountered, but continues attempting to stop remaining objects.
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is an application container that manages singletons and objects by type and (optionally) by name.
func NewContainer ¶
func NewContainer() *Container
NewContainer creates and returns a new empty container.
func (*Container) Inspect ¶
func (c *Container) Inspect(format InspectFormat) (string, error)
Inspect returns a formatted representation of the container contents. Supports table, JSON, and YAML formats for human and machine consumption.
type Factory ¶
type Factory[C any] interface { Build(a *Application[C]) error }
Factory creates and registers objects in the application container. Implementations should use SetAs[T]() to register created objects with the container.
type FactoryFunc ¶
type FactoryFunc[C any] func(a *Application[C]) error
FactoryFunc is a function type that implements Factory[C]. It allows using raw functions as factories without defining separate types.
func (FactoryFunc[C]) Build ¶
func (f FactoryFunc[C]) Build(a *Application[C]) error
Build implements the Factory interface for FactoryFunc.
type InspectData ¶
type InspectData struct {
Summary InspectSummary `json:"summary" yaml:"summary"`
Objects []InspectObject `json:"objects" yaml:"objects"`
}
InspectData represents the structured data for container inspection.
type InspectFormat ¶
type InspectFormat string
InspectFormat defines the output format for container inspection.
const ( InspectHuman InspectFormat = "human" InspectJSON InspectFormat = "json" InspectYAML InspectFormat = "yaml" )
type InspectObject ¶
type InspectObject struct {
Type string `json:"type" yaml:"type"`
Storage string `json:"storage" yaml:"storage"`
Name *string `json:"name" yaml:"name"`
Value string `json:"value" yaml:"value"`
}
InspectObject represents a single object in the container for inspection.
type InspectSummary ¶
type InspectSummary struct {
Total int `json:"total" yaml:"total"`
Singletons int `json:"singletons" yaml:"singletons"`
Named int `json:"named" yaml:"named"`
}
InspectSummary provides aggregate statistics about the container.
type Linkable ¶
Linkable defines objects that can establish connections to other container objects during the linking phase after all objects have been created.