di

package module
v1.11.2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: GPL-3.0 Imports: 10 Imported by: 1

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"

func main() {
    container := di.NewContainer()
    container.Set(&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(func () *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(&ExampleService{})
container.Compile()
container.Get((*ExampleService)(nil)).(*ExampleService) // Returns *ExampleService
  1. Leave as it is. In this case container will also resolve your service, but only as a dependency for other services. Useful for libraries.

The default implementation of container, provided by library is a global state. It also handler environment variables. You can build your application, working in a sandbox.

di.Application().SetEntryPoints(func (container GlobalState) {
    ...
})
di.Application().Run()

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()
}

type Stoppable interface {
    Shutdown()
}

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EntryNotFound = errors.New("entry not found")

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(any, ...string)
	// Build sets new entry in container without adding it to container
	Build(any) any
	// AppendTypes adds referenced types to an existing entry. Returns error if
	AppendTypes(any, ...any) error
	// 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 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 added in v1.11.0

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

func NewApplication added in v1.10.0

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