Documentation ¶
Overview ¶
DI package propose a simple abstraction for Dependency Injection, and a simple implementation for such abstractions
Index ¶
- Variables
- func ActivateFunc[T any](function any, provider ServiceProvider) (T, error)
- func ActivateFuncSimple(function any, provider ServiceProvider) (any, error)
- func ActivateStruct[T any](provider ServiceProvider) (*T, error)
- func ActivateStructSimple(structType reflect.Type, provider ServiceProvider) (any, error)
- type Disposable
- type Lifetime
- type ServiceCollection
- type ServiceContainer
- type ServiceDependencyError
- type ServiceDescriber
- type ServiceDescriptor
- func NewDescriptor[T any](lifetime Lifetime, factory ServiceFactory) ServiceDescriptor
- func NewDescriptorForType(serviceType reflect.Type, lifetime Lifetime, factory ServiceFactory) ServiceDescriptor
- func NewInstance[T any](instance T) (ServiceDescriptor, error)
- func NewInstanceForType(serviceType reflect.Type, instance any) (ServiceDescriptor, error)
- func NewScopedFactory[T any](factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
- func NewScopedFactoryForType(serviceType reflect.Type, factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
- func NewScopedServiceFactory[T any](factory ServiceFactory) ServiceDescriptor
- func NewScopedServiceFactoryForType(serviceType reflect.Type, factory ServiceFactory) ServiceDescriptor
- func NewScopedStruct[T any, Impl any]() (ServiceDescriptor, error)
- func NewScopedStructForType(serviceType reflect.Type, structType reflect.Type) (ServiceDescriptor, error)
- func NewScopedStructPtr[Impl any]() (ServiceDescriptor, error)
- func NewSingletonFactory[T any](factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
- func NewSingletonFactoryForType(serviceType reflect.Type, factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
- func NewSingletonServiceFactory[T any](factory ServiceFactory) ServiceDescriptor
- func NewSingletonServiceFactoryForType(serviceType reflect.Type, factory ServiceFactory) ServiceDescriptor
- func NewSingletonStruct[T any, Impl any]() (ServiceDescriptor, error)
- func NewSingletonStructForType(serviceType reflect.Type, structType reflect.Type) (ServiceDescriptor, error)
- func NewSingletonStructPtr[Impl any]() (ServiceDescriptor, error)
- func NewTransientFactory[T any](factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
- func NewTransientFactoryForType(serviceType reflect.Type, factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
- func NewTransientServiceFactory[T any](factory ServiceFactory) ServiceDescriptor
- func NewTransientServiceFactoryForType(serviceType reflect.Type, factory ServiceFactory) ServiceDescriptor
- func NewTransientStruct[T any, Impl any]() (ServiceDescriptor, error)
- func NewTransientStructForType(serviceType reflect.Type, structType reflect.Type) (ServiceDescriptor, error)
- func NewTransientStructPtr[Impl any]() (ServiceDescriptor, error)
- type ServiceFactory
- func NewFactory(factoryFunc SimpleServiceFactoryFunc) ServiceFactory
- func NewFactoryWith(displayName string, requirements []reflect.Type, ...) ServiceFactory
- func NewFuncFactory(function any) (ServiceFactory, error)
- func NewServiceInstanceFactory(factoryFunc ServiceFactoryFunc) ServiceFactory
- func NewServiceInstanceFactoryWith(displayName string, requirements []reflect.Type, ...) ServiceFactory
- func NewStructFactory[T any]() (ServiceFactory, error)
- func NewStructFactoryForType(structType reflect.Type) (ServiceFactory, error)
- type ServiceFactoryFunc
- type ServiceInfo
- type ServiceInstance
- type ServiceProvider
- type SimpleServiceFactoryFunc
- type SimpleServiceFactoryFuncOf
- type SimpleServiceFactoryFuncOfPtr
Constants ¶
This section is empty.
Variables ¶
var ( ErrServiceContainerDisposed = errors.New("service container has been disposed") ErrMissingServiceDescriber = errors.New("missing service describer") )
var ErrInvalidFuncResultType = errors.New("invalid function result type")
var ErrInvalidFuncResults = errors.New("invalid function results")
var ErrInvalidFuncType = errors.New("invalid function type")
var ErrInvalidInstance = errors.New("invalid instance")
var ErrInvalidStructType = errors.New("invalid struct type")
var (
ErrServiceNotFound = errors.New("service not found")
)
Functions ¶
func ActivateFunc ¶
func ActivateFunc[T any](function any, provider ServiceProvider) (T, error)
func ActivateFuncSimple ¶
func ActivateFuncSimple(function any, provider ServiceProvider) (any, error)
func ActivateStruct ¶
func ActivateStruct[T any](provider ServiceProvider) (*T, error)
func ActivateStructSimple ¶
func ActivateStructSimple(structType reflect.Type, provider ServiceProvider) (any, error)
Types ¶
type Disposable ¶
type Disposable interface {
Dispose()
}
Disposable represents objects that can be disposed.
func NewDisposable ¶
func NewDisposable(dispose func()) Disposable
NewDisposable returns a new disposable that calls the given dispose function when disposed.
func NewNoopDisposable ¶
func NewNoopDisposable() Disposable
NewNoopDisposable returns a new disposable that does nothing when disposed.
type ServiceCollection ¶
type ServiceCollection interface { ListDescriptors() []ServiceDescriptor FindDescriptors(predicate func(ServiceDescriptor) bool) []ServiceDescriptor FindFirstDescriptor(predicate func(ServiceDescriptor) bool) ServiceDescriptor FindDescriptorsForType(serviceType reflect.Type) []ServiceDescriptor FindFirstDescriptorForType(serviceType reflect.Type) ServiceDescriptor Add(descriptor ServiceDescriptor) ServiceCollection AddRange(descriptors ...ServiceDescriptor) ServiceCollection UpdateDescriptors( predicate func(ServiceDescriptor) bool, replace func([]ServiceDescriptor) []ServiceDescriptor, ) ServiceCollection TryAdd(descriptor ServiceDescriptor) ServiceCollection TryAddRange(descriptors ...ServiceDescriptor) ServiceCollection Build() (ServiceContainer, error) }
ServiceCollection is a collection of services, describing a dependency graph of services.
func NewServiceCollection ¶
func NewServiceCollection() ServiceCollection
NewServiceCollection creates a new ServiceCollection.
type ServiceContainer ¶
type ServiceContainer interface { Provider() ServiceProvider IsScoped() bool Dispose() IsDisposed() bool }
type ServiceDependencyError ¶
type ServiceDependencyError struct {
Errors []error
}
func (*ServiceDependencyError) Error ¶
func (err *ServiceDependencyError) Error() string
type ServiceDescriber ¶
type ServiceDescriber interface { GetServiceDescriptor(serviceType reflect.Type) ServiceDescriptor GetServiceDescriptors(serviceType reflect.Type) []ServiceDescriptor }
type ServiceDescriptor ¶
type ServiceDescriptor interface { fmt.Stringer Lifetime() Lifetime ServiceType() reflect.Type Factory() ServiceFactory }
func NewDescriptor ¶
func NewDescriptor[T any](lifetime Lifetime, factory ServiceFactory) ServiceDescriptor
NewDescriptor creates a new service descriptor for the given service type.
func NewDescriptorForType ¶
func NewDescriptorForType(serviceType reflect.Type, lifetime Lifetime, factory ServiceFactory) ServiceDescriptor
NewDescriptorForType creates a new service descriptor. parameters:
serviceType - the service type lifetime - the service lifetime factory - the service factory
returns:
the new service descriptor
func NewInstance ¶
func NewInstance[T any](instance T) (ServiceDescriptor, error)
NewInstance creates a new singleton service descriptor for the given service instance.
func NewInstanceForType ¶
func NewInstanceForType(serviceType reflect.Type, instance any) (ServiceDescriptor, error)
NewInstanceForType creates a new singleton service descriptor for the given service instance.
func NewScopedFactory ¶
func NewScopedFactory[T any](factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
NewScopedFactory creates a new singleton service descriptor for the given service type.
func NewScopedFactoryForType ¶
func NewScopedFactoryForType( serviceType reflect.Type, factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
NewScopedFactoryForType creates a new singleton service descriptor for the given service type.
func NewScopedServiceFactory ¶
func NewScopedServiceFactory[T any](factory ServiceFactory) ServiceDescriptor
NewScopedServiceFactory creates a new singleton service descriptor for the given service type.
func NewScopedServiceFactoryForType ¶
func NewScopedServiceFactoryForType(serviceType reflect.Type, factory ServiceFactory) ServiceDescriptor
NewScopedServiceFactoryForType creates a new singleton service descriptor for the given service type.
func NewScopedStruct ¶
func NewScopedStruct[T any, Impl any]() (ServiceDescriptor, error)
NewScopedStruct creates a new singleton service descriptor for the given service type.
func NewScopedStructForType ¶
func NewScopedStructForType( serviceType reflect.Type, structType reflect.Type) (ServiceDescriptor, error)
NewScopedStructForType creates a new singleton service descriptor for the given service type.
func NewScopedStructPtr ¶
func NewScopedStructPtr[Impl any]() (ServiceDescriptor, error)
NewScopedStructPtr creates a new scoped service descriptor for the given service type.
func NewSingletonFactory ¶
func NewSingletonFactory[T any](factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
NewSingletonFactory creates a new singleton service descriptor for the given service type.
func NewSingletonFactoryForType ¶
func NewSingletonFactoryForType( serviceType reflect.Type, factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
NewSingletonFactoryForType creates a new singleton service descriptor for the given service type.
func NewSingletonServiceFactory ¶
func NewSingletonServiceFactory[T any](factory ServiceFactory) ServiceDescriptor
NewSingletonServiceFactory creates a new singleton service descriptor for the given service type.
func NewSingletonServiceFactoryForType ¶
func NewSingletonServiceFactoryForType(serviceType reflect.Type, factory ServiceFactory) ServiceDescriptor
NewSingletonServiceFactoryForType creates a new singleton service descriptor for the given service type.
func NewSingletonStruct ¶
func NewSingletonStruct[T any, Impl any]() (ServiceDescriptor, error)
NewSingletonStruct creates a new singleton service descriptor for the given service type.
func NewSingletonStructForType ¶
func NewSingletonStructForType( serviceType reflect.Type, structType reflect.Type) (ServiceDescriptor, error)
NewSingletonStructForType creates a new singleton service descriptor for the given service type.
func NewSingletonStructPtr ¶
func NewSingletonStructPtr[Impl any]() (ServiceDescriptor, error)
NewSingletonStructPtr creates a new singleton service descriptor for the given service type.
func NewTransientFactory ¶
func NewTransientFactory[T any](factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
NewTransientFactory creates a new singleton service descriptor for the given service type.
func NewTransientFactoryForType ¶
func NewTransientFactoryForType( serviceType reflect.Type, factoryFunc SimpleServiceFactoryFunc) ServiceDescriptor
NewTransientFactoryForType creates a new singleton service descriptor for the given service type.
func NewTransientServiceFactory ¶
func NewTransientServiceFactory[T any](factory ServiceFactory) ServiceDescriptor
NewTransientServiceFactory creates a new singleton service descriptor for the given service type.
func NewTransientServiceFactoryForType ¶
func NewTransientServiceFactoryForType(serviceType reflect.Type, factory ServiceFactory) ServiceDescriptor
NewTransientServiceFactoryForType creates a new singleton service descriptor for the given service type.
func NewTransientStruct ¶
func NewTransientStruct[T any, Impl any]() (ServiceDescriptor, error)
NewTransientStruct creates a new singleton service descriptor for the given service type.
func NewTransientStructForType ¶
func NewTransientStructForType( serviceType reflect.Type, structType reflect.Type) (ServiceDescriptor, error)
NewTransientStructForType creates a new singleton service descriptor for the given service type.
func NewTransientStructPtr ¶
func NewTransientStructPtr[Impl any]() (ServiceDescriptor, error)
NewTransientStructPtr creates a new transient service descriptor for the given service type.
type ServiceFactory ¶
type ServiceFactory interface { Factory() ServiceFactoryFunc Requirements() []reflect.Type DisplayName() string }
func NewFactory ¶
func NewFactory(factoryFunc SimpleServiceFactoryFunc) ServiceFactory
NewFactory creates a new service factory from the given factory function. parameters:
factoryFunc - the factory function to create the service
returns:
the new service factory
func NewFactoryWith ¶
func NewFactoryWith( displayName string, requirements []reflect.Type, factoryFunc SimpleServiceFactoryFunc) ServiceFactory
NewFactoryWith creates a new service factory from the given factory function. parameters:
factoryFunc - the factory function to create the service requirements - the service's requirements displayName - the service's display name
returns:
the new service factory
func NewFuncFactory ¶
func NewFuncFactory(function any) (ServiceFactory, error)
NewFuncFactory creates a new service factory from the given function. parameters:
function - the function to create the service from
returns:
the new service factory
func NewServiceInstanceFactory ¶
func NewServiceInstanceFactory(factoryFunc ServiceFactoryFunc) ServiceFactory
NewServiceInstanceFactory creates a new service factory from the given factory function. parameters:
factoryFunc - the factory function to create the service
returns:
the new service factory
func NewServiceInstanceFactoryWith ¶
func NewServiceInstanceFactoryWith( displayName string, requirements []reflect.Type, factoryFunc ServiceFactoryFunc) ServiceFactory
NewServiceInstanceFactoryWith creates a new service factory from the given factory function. parameters:
factoryFunc - the factory function to create the service requirements - the service's requirements displayName - the service's display name
returns:
the new service factory
func NewStructFactory ¶
func NewStructFactory[T any]() (ServiceFactory, error)
NewStructFactory creates a new service factory from the given struct type. parameters:
structType - the struct type to create the service from
returns:
the new service factory
func NewStructFactoryForType ¶
func NewStructFactoryForType(structType reflect.Type) (ServiceFactory, error)
NewStructFactoryForType creates a new service factory from the given struct type. parameters:
structType - the struct type to create the service from
returns:
the new service factory
type ServiceFactoryFunc ¶
type ServiceFactoryFunc func(provider ServiceProvider) (ServiceInstance, error)
func ActivateFuncFactoryForType ¶
func ActivateFuncFactoryForType(function any) (ServiceFactoryFunc, error)
func ActivateStructFactoryForType ¶
func ActivateStructFactoryForType(structType reflect.Type) (ServiceFactoryFunc, error)
type ServiceInfo ¶
func (ServiceInfo) IsNotFound ¶
func (info ServiceInfo) IsNotFound() bool
type ServiceInstance ¶
type ServiceInstance struct { Instance any Disposable Disposable }
func ActivateFuncForType ¶
func ActivateFuncForType(function any, provider ServiceProvider) (ServiceInstance, error)
func ActivateStructForType ¶
func ActivateStructForType(structType reflect.Type, provider ServiceProvider) (ServiceInstance, error)
type ServiceProvider ¶
type SimpleServiceFactoryFunc ¶
type SimpleServiceFactoryFunc func(provider ServiceProvider) (any, error)
func ActivateFuncSimpleFactoryForType ¶
func ActivateFuncSimpleFactoryForType(function any) (SimpleServiceFactoryFunc, error)
func ActivateStructSimpleFactoryForType ¶
func ActivateStructSimpleFactoryForType(structType reflect.Type) (SimpleServiceFactoryFunc, error)
type SimpleServiceFactoryFuncOf ¶
type SimpleServiceFactoryFuncOf[T any] func(provider ServiceProvider) (T, error)
func ActivateFuncFactory ¶
func ActivateFuncFactory[T any](function any) (SimpleServiceFactoryFuncOf[T], error)
type SimpleServiceFactoryFuncOfPtr ¶
type SimpleServiceFactoryFuncOfPtr[T any] func(provider ServiceProvider) (*T, error)
func ActivateStructFactory ¶
func ActivateStructFactory[T any]() (SimpleServiceFactoryFuncOfPtr[T], error)