di

package module
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

README

Go Dependency Injection Container

Build easily microservice applications

To start you have to create container instance, fill it with services and compile

package main

import "github.com/Sanchous98/go-di/v2"

func main() {
    container := di.NewContainer()
    container.Set(di.Service(&ExampleService{}))
    container.Compile()
    container.Get((*ExampleService)(nil)).(*ExampleService) // Returns filled ExampleService
}

There are some ways container can compile a service:

  1. Use resolver function. It's the most preferable method to integrate your service in container, because it has less performance overhead and permits to bind a service to an interface type. Resolver can have no parameters or receive the container. It's better to use interface, you need, because of LSP
container.Set(di.Resolver(func (di.Container) *ExampleInterfaceType {
    return &ExampleService{} 
}))
container.Get((*ExampleInterfaceType)(nil)).(*ExampleInterfaceType) // Returns *ExampleService
  1. Pass service to container. In this case fields tagged by "inject" tag will be filled from container. Important: precompile container to use it without any performance impact.
container.Set(di.Service(&ExampleService{}))
container.Compile()
container.Get((*ExampleService)(nil)).(*ExampleService) // Returns *ExampleService

Services can have default steps to self initialize and self destroy. To use this feature, implement Constructable and Destructible interfaces. It's useful for graceful shutdown feature.

type Constructable interface {
    Constructor()
}

type Destructible interface {
    Destructor()
}

Also services can be long-living. Implement Launchable and Stoppable interfaces to launch and stop your services gracefully.

type Launchable interface {
    Launch(context.Context)
}

type Stoppable interface {
    Shutdown(context.Context)
}

Constructor() method is called on service compiling. Destructor() method is called on application exiting when the container destroys. Launch(context.Context) is called on Run() method calling. Shutdown(context.Context) method is called on application exiting before the container destroys.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Constructable

type Constructable interface {
	Constructor()
}

Constructable is a service that has special method that initializes it

type Container

type Container interface {
	// Set defines new entry in container
	Set(Option, ...Option)
	// Build sets new entry in container without adding it to container
	Build(Option, ...Option) any
	// Has checks whether the service of passed type exists
	Has(any) bool
	// Get returns service from container
	Get(any) any
	// GetByTag returns tagged services from container
	GetByTag(string) []any
	// All return all services
	All() []any
}

Container handles services and fills them

type Destructible

type Destructible interface {
	Destructor()
}

Destructible is a service that has special method that destructs it

type Launchable

type Launchable interface {
	Launch(context.Context)
}

type Option

type Option = func(*entry)

func Annotate

func Annotate[T any]() Option

func Constructor added in v2.1.0

func Constructor[T any](constructor any) Option

func Default

func Default[T any](s T) Option

func DefaultResolver

func DefaultResolver() Option

func Resolver

func Resolver[T any, F ~func(Container) T](f F) Option

func Service

func Service[T any](s T) Option

func WithTags

func WithTags(tags ...string) Option

type PrecompiledContainer

type PrecompiledContainer interface {
	Compile()
	Destroy()
	Container
}

PrecompiledContainer is an extension of Container that can fill services before usage

func NewContainer

func NewContainer() PrecompiledContainer

type Runner

type Runner interface {
	PrecompiledContainer
	Name() string
	Run(context.Context)
}

func NewApplication

func NewApplication(name string) Runner

type Stoppable

type Stoppable interface {
	Shutdown(context.Context)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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