Categorygithub.com/Sanchous98/go-di
repository
2.1.2
Repository: https://github.com/sanchous98/go-di.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# 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.