dependencies

package
v0.0.0-...-ed34e12 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 4 Imported by: 3

README

Dependency Injection

Our own take on DI is a lightweight "framework" of conventions, helpers, and constructs. It does not make any effort to provide any sort of master registry to make available everywhere (at least, not yet).

Ref: https://en.wikipedia.org/wiki/Dependency_injection Ref: https://faun.pub/dependency-injection-in-go-the-better-way-833e2cc65fab

Documentation

Overview

DigiStratum GoLib - Dependency Injection

Index

Constants

View Source
const DEP_VARIANT_DEFAULT = "default"

Variables

This section is empty.

Functions

func NewDependencies

func NewDependencies(deps ...DependencyIfc) *dependencies

Make a new one of these!

func NewDependency

func NewDependency(name string) *dependency

func NewDependencyInstance

func NewDependencyInstance(name string, instance interface{}) *dependencyInstance

Types

type DependenciesIfc

type DependenciesIfc interface {
	// Add one or more Dependencies to the set
	Add(deps ...DependencyIfc) *dependencies

	// Get a dependency by name/variant
	Get(name string) DependencyIfc
	GetVariant(name, variant string) DependencyIfc

	// Check whether a dependency is in the set by name/variant
	Has(name string) bool
	HasVariant(name, variant string) bool

	// Get the list of currently set dependencies
	GetVariants(name string) []string
	GetAllVariants() map[string][]string
}

type DependencyCaptureFunc

type DependencyCaptureFunc func(instance interface{}) bool

return true on success, false on error (i.e. interface assertion failure)

type DependencyIfc

type DependencyIfc interface {
	GetName() string

	SetVariant(variant string) *dependency
	GetVariant() string

	SetRequired() *dependency
	IsRequired() bool

	CanCapture() bool
	CaptureWith(captureFunc DependencyCaptureFunc) *dependency
	Capture(instance interface{}) error
}

type DependencyInjectable

type DependencyInjectable struct {
	*startable.Startable
	// contains filtered or unexported fields
}

Exported to support embedding

func NewDependencyInjectable

func NewDependencyInjectable(deps ...DependencyIfc) *DependencyInjectable

Make a new one of these

func (*DependencyInjectable) AddDeclaredDependencies

func (r *DependencyInjectable) AddDeclaredDependencies(deps ...DependencyIfc) *DependencyInjectable

Add more Dependency declarations

func (*DependencyInjectable) GetDeclaredDependencies

func (r *DependencyInjectable) GetDeclaredDependencies() DependenciesIfc

What are all the declared Dependecies?

func (*DependencyInjectable) GetInjectedDependencies

func (r *DependencyInjectable) GetInjectedDependencies() DependenciesIfc

What are the injected DependencyInstances?

func (*DependencyInjectable) GetInstance

func (r *DependencyInjectable) GetInstance(name string) interface{}

Get the injected dependency matching name (default or any variant)

func (*DependencyInjectable) GetInstanceVariant

func (r *DependencyInjectable) GetInstanceVariant(name, variant string) interface{}

Get the injected dependency matching name and specific variant

func (*DependencyInjectable) GetMissingDependencies

func (r *DependencyInjectable) GetMissingDependencies() DependenciesIfc

What Dependencies are Required that have not yet been injected?

func (*DependencyInjectable) GetOptionalDependencies

func (r *DependencyInjectable) GetOptionalDependencies() DependenciesIfc

What are just the optional Dependencies?

func (*DependencyInjectable) GetOptionalDependencyInstances

func (r *DependencyInjectable) GetOptionalDependencyInstances() []dependencyInstance

Get the injected, optional Dependency Instances

func (*DependencyInjectable) GetRequiredDependencies

func (r *DependencyInjectable) GetRequiredDependencies() DependenciesIfc

What are just the required Dependencies?

func (*DependencyInjectable) GetRequiredDependencyInstances

func (r *DependencyInjectable) GetRequiredDependencyInstances() []dependencyInstance

Get the injected, required Dependency Instances

func (*DependencyInjectable) GetUnknownDependencies

func (r *DependencyInjectable) GetUnknownDependencies() DependenciesIfc

What Dependencies are injected, but unknown (undeclared) to us?

func (*DependencyInjectable) GetUnknownDependencyInstances

func (r *DependencyInjectable) GetUnknownDependencyInstances() []dependencyInstance

Get the injected, unknown Dependency Instances

func (*DependencyInjectable) HasAllRequiredDependencies

func (r *DependencyInjectable) HasAllRequiredDependencies() bool

Have all required dependencies have been injected?

func (*DependencyInjectable) InjectDependencies

func (r *DependencyInjectable) InjectDependencies(depinst ...DependencyInstanceIfc) error

Receive dependency injection from external source

func (*DependencyInjectable) Start

func (r *DependencyInjectable) Start() error

Starter func; ensure that all required dependencies are injected

type DependencyInjectableIfc

type DependencyInjectableIfc interface {
	// Embedded interface(s)
	startable.StartableIfc

	// Our own interface
	AddDeclaredDependencies(deps ...DependencyIfc) *DependencyInjectable

	// Injection & Retrieval
	// Receive dependency injection from external source
	InjectDependencies(depinst ...DependencyInstanceIfc) error
	// Have all required dependencies have been injected?
	HasAllRequiredDependencies() bool

	// Discovery (Declared)
	// What are all the declared Dependecies?
	GetDeclaredDependencies() DependenciesIfc
	// What are just the required Dependencies?
	GetRequiredDependencies() DependenciesIfc
	// What are just the optional Dependencies?
	GetOptionalDependencies() DependenciesIfc

	// Discovery (Injected)
	// What are the injected DependencyInstances?
	GetInjectedDependencies() DependenciesIfc
	// What Dependencies are Required that have not yet been injected?
	GetMissingDependencies() DependenciesIfc
	// What Dependencies are injected, but unknown (undeclared) to us?
	GetUnknownDependencies() DependenciesIfc

	// Retrieval (Injected)
	// Get the injected dependency matching name (default or any variant)
	GetInstance(name string) interface{}
	// Get the injected dependency matching name and specific variant
	GetInstanceVariant(name, variant string) interface{}
	// Get the injected, required Dependency Instances
	GetRequiredDependencyInstances() []dependencyInstance
	// Get the injected, optional Dependency Instances
	GetOptionalDependencyInstances() []dependencyInstance
	// Get the injected, unknown Dependency Instances
	GetUnknownDependencyInstances() []dependencyInstance
}

Implementation can consume injected DependencyInstanceIfc's

type DependencyInstanceIfc

type DependencyInstanceIfc interface {
	GetName() string
	GetVariant() string
	GetInstance() interface{}
	SetVariant(variant string) *dependencyInstance
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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