di

package module
v0.0.0-...-718ab47 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: MIT Imports: 7 Imported by: 0

README

github.com/go-mike/di

Dependency Injection Abstractions and default implementation

Glossary

  • Service Interface: Represents a type that serves as a key to request a service to a service provider, and as a way to interact with the service.
  • Service Implementation: Represents a type that implements a service interface.
  • Dependency: Is a relationship between a service implementation and a service interface, where the former requires an instance of the latter to be able to function.
  • Dependency Injection: Is the process of providing a service implementation with the required dependencies.
  • Service Lifetime: Represents the lifetime of a service.
    • Singleton Lifetime: The service is instantiated once and shared across all requests.
    • Scoped Lifetime: The service is instantiated once per scope.
    • Transient Lifetime: The service is instantiated every time it is requested.
  • Disposable: Represents a type that can be disposed.
  • Service Factory: Is a function that can create an instance of a service implementation, and a way to dispose of it when not required anymore.
  • Service Descriptor: Represents a description of a service with a lifetime, a service interface, and a service factory.
  • Service Collection: Is a mutable collection of service descriptors, which can be used to configure the services available in an application.
  • Service Describer: Is an immutable collection of service descriptors, used internally by the service provider to resolve services.
  • Service Container: Is a container of services, which can be used to resolve services, and manage their lifetime.
  • Service Provider: Is a type to access the services available in an application.
  • Activator: Is a type to help create instances of services with dependencies from a service provider.

Where are services resolved from

When a service Is requested from a container Then
Singleton Root Container The service is resolved from current container
Singleton Scoped Container The service is resolved from the parent container
Scoped Root Container The request fails
Scoped Scoped Container The service is resolved from current container
Transient Root Container The service is resolved from current container
Transient Scoped Container The service is resolved from current container

Table of service possible dependencies

Can a service Ask for a service Answer
Singleton Singleton
Singleton Scoped
Singleton Transient
Scoped Singleton
Scoped Scoped
Scoped Transient
Transient Singleton
Transient Scoped if scoped
Transient Transient

Documentation

Overview

DI package propose a simple abstraction for Dependency Injection, and a simple implementation for such abstractions

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrServiceContainerDisposed = errors.New("service container has been disposed")
	ErrMissingServiceDescriber  = errors.New("missing service describer")
)
View Source
var ErrInvalidFuncResultType = errors.New("invalid function result type")
View Source
var ErrInvalidFuncResults = errors.New("invalid function results")
View Source
var ErrInvalidFuncType = errors.New("invalid function type")
View Source
var ErrInvalidInstance = errors.New("invalid instance")
View Source
var ErrInvalidStructType = errors.New("invalid struct type")
View Source
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 Lifetime

type Lifetime int
const (
	UnknownLifetime Lifetime = iota
	Singleton
	Scoped
	Transient
)

func (Lifetime) String

func (lifetime Lifetime) String() string

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

type ServiceInfo struct {
	ServiceType    reflect.Type
	Lifetime       Lifetime
	IsInstantiated bool
}

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 ServiceProvider interface {
	GetService(serviceType reflect.Type) (any, error)
	GetServiceInfo(serviceType reflect.Type) ServiceInfo
}

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)

Jump to

Keyboard shortcuts

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