Di - A (very) WIP Go 1.18+ generic dependency injection package based on type reflection

di

A (very) WIP Go 1.18+ generic dependency injection package based on type reflection. Because this package is in a very early development state, you need to expect breaking API changes.

Usage

Below, you can see a very simplified demonstration on how this package works. If you want to see a more comprehensive example, please take a look at the examples listed.

type Service1 interface {}

type Service2 interface {}

type service1Impl struct {
    S2 Service2
}

type service2Impl struct {
    S1 Service1
}

func main() {
    c := di.NewContainer()

    di.MustRegister[Service1, service1Impl](c)
    di.MustRegister[Service2, service2Impl](c)

    s1 := di.MustGet[Service1](c)
}

Limitations

There are some severe limitations with this package.

  • To set fields automatically via the injection system, they must be exported. All unexported fields are ignored.
  • Currently, only interface fields can be automatically assigned to instances of registered services.
  • Currently, there are only singleton instances because of simplicity.

Ideas

Here, you can see some implementation ideas which will be implemented in the upcoming time into this package.

  • Register dependencies directly via instance
  • Register dependencies via builder functions
  • Add disposal functions which will be called when the container is getting disposed.
  • Add transistent service registration.

Goal

The goal of this package is to replace the current DI system in my project shinpuru which currently depends on suralabs/di.

Owner
Ringo Hoffmann
Full stack developer. SysAdmin / DevOps Enthusiast. I ♥ Go, React, C# and Docker. | he/him
Ringo Hoffmann
Similar Resources

Go library for decoding generic map values into native Go structures and vice versa.

mapstructure mapstructure is a Go library for decoding generic map values to structures and vice versa, while providing helpful error handling. This l

Dec 28, 2022

Helm plugin to reference value files packaged in dependency charts

Helm Octopus Plugin This Helm plugin allows to reference packaged value files (other than the default values.yaml). Install helm plugin install https:

Sep 23, 2021

MNA - stands for mobile number assignment - a small zero external dependency golang library that is used to identify mobile number assignment in tanzania

MNA - stands for mobile number assignment - a small zero external dependency golang library that is used to identify mobile number assignment in tanzania

Nov 29, 2021

conditiond is a generic constraint and policy evaluator.

conditiond conditiond is a generic constraint and policy evaluator. This tool lets you define constraints in data and evaluate them at run time. It's

Dec 5, 2022

A small & fast dependency-free library for parsing micro expressions.

MicroExpr A small & fast dependency-free library for parsing micro expressions. This library was originally built for use in templating languages (e.g

Nov 25, 2022

A fast unused and duplicate dependency checker

A fast unused and duplicate dependency checker

Depp - A fast unused and duplicate package checker Installation npm install -g depp-installer (will try to get npm install -g depp later) Usage Just

Oct 31, 2022

Some utility functions for generic types in Go.

GOUF - Utility Functions for generic types Go team released Go 1.18 beta recently with support for Generics(a.k.a type parameters). This package provi

Apr 13, 2022

Generic Free List implementation to reuse memory and avoid allocations

gofl GOFL provides a Generic Free List implementation for Go. Installation This

Oct 17, 2022

Slice - provides generic Map, Reduce and Filter functions for Go.

slice slice is a simple Go package to provide generic versions of Map, Reduce and Filter on slices. I mainly wrote it as an exercise to get more famil

Jan 1, 2023
Comments
  • Add transient services

    Add transient services

    As seen in Readme, those are created each time they are requested from the container.

    I have written isolated tests for them, which prove they should work just fine. However, unlike the previous Services (renamed here SingletonServices for brevity), a cyclic dependency of transient services will cause a stack overflow. I do not check the chain for loops here, as I have assumed it is out of scope for this PR.

Helpers for making the use of reflection easier

go-xray This is a Golang library with reflection related functions which I use in my different projects. KeyValue This type is used to construct a key

Oct 24, 2022
Faster golang reflection

In order to solve a problem that can work with any golang type, one has no choice but to use reflection. Native golang reflection comes with hefty performance price, on benchmarking simple getter/setter case to manipulate struct dynamically I've seen around 100 time worst performance comparing to statically typed code.

Dec 21, 2022
Create deep copies (clones) of your maps and slices without using reflection.

DeepCopy DeepCopy helps you create deep copies (clones) of your maps and slices. Create deep copies (clones) of your objects The package is based on t

Nov 20, 2022
tail a text file and trigger an action if a new line occurs [wip]

Tailpipe Synopsis Config Help Synopsis Tail a file and trigger an action if a new line occurs. Currently only mailing the new line somewhere is suppor

Dec 23, 2021
Package ethtool allows control of the Linux ethtool generic netlink interface.

ethtool Package ethtool allows control of the Linux ethtool generic netlink interface.

Dec 14, 2022
🚀 GoDI: Generic based DI in Go

?? GoDI: Generic based DI in Go Installation DI: go get -u github.com/MaximZayats/godi/ import "github.com/MaximZayats/godi/di" CMD: go get -u github.

Aug 14, 2022
Generic-based collection tools

go-collection go collection is a tool implemented using generic, it can help you process slice/map data quickly and easily convert between them. Note:

Dec 29, 2022
Customisable and automated HTTP header injection
Customisable  and automated HTTP header injection

headi Customisable and automated HTTP header injection. Example run from the HTB machine Control: InsecureSkipVerify is not currently configured, if y

Dec 31, 2022
A very simplified Mux focused for ease of use.

hyper-mux A very simplified Mux focused for ease of use. go get github.com/hyperjumptech/hyper-mux How to use the Mux The following is how you going t

Aug 23, 2022
A better Generic Pool (sync.Pool)
A better Generic Pool (sync.Pool)

This package is a thin wrapper over the Pool provided by the sync package. The Pool is an essential package to obtain maximum performance by reducing the number of memory allocations.

Dec 1, 2022