container

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package container provides dependency injection for Typosentinel This package implements a service container for managing dependencies and their lifecycle

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container manages service dependencies and their lifecycle

func NewContainer

func NewContainer() *Container

NewContainer creates a new dependency injection container

func (*Container) AddFinalizer

func (c *Container) AddFinalizer(name string, finalizer func(interface{}) error) error

AddFinalizer adds a finalizer function for a service

func (*Container) AddInitializer

func (c *Container) AddInitializer(name string, initializer func(interface{}) error) error

AddInitializer adds an initializer function for a service

func (*Container) AddTag

func (c *Container) AddTag(name string, tag string) error

AddTag adds a tag to a service

func (*Container) ClearScope

func (c *Container) ClearScope(scope string) error

ClearScope clears all scoped instances for a specific scope

func (*Container) Get

func (c *Container) Get(name string) (interface{}, error)

Get retrieves a service instance

func (*Container) GetByTag

func (c *Container) GetByTag(tag string) ([]interface{}, error)

GetByTag retrieves all service instances with a specific tag

func (*Container) GetByType

func (c *Container) GetByType(serviceType reflect.Type) (interface{}, error)

GetByType retrieves a service instance by its type

func (*Container) GetScoped

func (c *Container) GetScoped(name string, scope string) (interface{}, error)

GetScoped retrieves a service instance within a specific scope

func (*Container) GetServiceInfo

func (c *Container) GetServiceInfo(name string) (*ServiceDefinition, error)

GetServiceInfo returns information about a service

func (*Container) Initialize

func (c *Container) Initialize(ctx context.Context) error

Initialize initializes the container and validates dependencies

func (*Container) IsShutdown

func (c *Container) IsShutdown() bool

IsShutdown returns true if the container has been shut down

func (*Container) ListServices

func (c *Container) ListServices() []string

ListServices returns a list of all registered services

func (*Container) Register

func (c *Container) Register(name string, factory interface{}, lifecycle ServiceLifecycle) error

Register registers a service with the container

func (*Container) RegisterInstance

func (c *Container) RegisterInstance(name string, instance interface{}) error

RegisterInstance registers a pre-created instance as a singleton

func (*Container) RegisterScoped

func (c *Container) RegisterScoped(name string, factory interface{}) error

RegisterScoped registers a scoped service

func (*Container) RegisterSingleton

func (c *Container) RegisterSingleton(name string, factory interface{}) error

RegisterSingleton registers a singleton service

func (*Container) RegisterTransient

func (c *Container) RegisterTransient(name string, factory interface{}) error

RegisterTransient registers a transient service

func (*Container) SetLogger

func (c *Container) SetLogger(logger interfaces.Logger)

SetLogger sets the logger for the container

func (*Container) SetMetrics

func (c *Container) SetMetrics(metrics interfaces.Metrics)

SetMetrics sets the metrics collector for the container

func (*Container) Shutdown

func (c *Container) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the container

type ServiceDefinition

type ServiceDefinition struct {
	Name         string
	Type         reflect.Type
	Factory      interface{}
	Lifecycle    ServiceLifecycle
	Dependencies []string
	Tags         []string
	Initializer  func(interface{}) error
	Finalizer    func(interface{}) error
}

ServiceDefinition defines how a service should be created

type ServiceLifecycle

type ServiceLifecycle string

ServiceLifecycle represents the lifecycle of a service

const (
	// Singleton services are created once and reused
	LifecycleSingleton ServiceLifecycle = "singleton"
	// Transient services are created on each request
	LifecycleTransient ServiceLifecycle = "transient"
	// Scoped services are created once per scope (e.g., per request)
	LifecycleScoped ServiceLifecycle = "scoped"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL