Monday, November 28, 2022
HomeGolangA mirrored image-free Run-Time Dependency Injection framework for Golang – Golang Libraries,...

A mirrored image-free Run-Time Dependency Injection framework for Golang – Golang Libraries, Apps, Golang Jobs and Go Tutorials


The Purpose

The aim of The Genjector bundle is to supply a Dependency Injection framework with out counting on reflection and relying solely on Go Generics (supplied from Go model 1.18).

It helps many various options:

  • Binding concrete implementations to specific interfaces.
  • Binding implementations as pointers or values.
  • Binding implementations with Supplier strategies.
  • Binding implementations with concrete cases.
  • Outline Binding as singletons.
  • Outline annotations for Binding.
  • Outline slices and maps of implementations.

Golang Package deal Benchmark

Whereas offering probably the most of recognized options of Dependency Injection frameworks, The Genjector Package deal additionally delivers prime efficiency in comparison with present extensively used Run-Time DI frameworks (so, options primarily based on code mills are excluded).

goos: darwin
goarch: amd64
pkg: github.com/ompluscator/genjector/_benchmark
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Benchmark
Benchmark/github.com/golobby/container/v3
Benchmark/github.com/golobby/container/v3-8         	 2834061	       409.6 ns/op
Benchmark/github.com/goava/di
Benchmark/github.com/goava/di-8                     	 4568984	       261.9 ns/op
Benchmark/github.com/goioc/di
Benchmark/github.com/goioc/di-8                     	19844284	        60.66 ns/op
Benchmark/go.uber.org/dig
Benchmark/go.uber.org/dig-8                         	  755488	      1497 ns/op
Benchmark/flamingo.me/dingo
Benchmark/flamingo.me/dingo-8                       	 2373394	       503.7 ns/op
Benchmark/github.com/samber/do
Benchmark/github.com/samber/do-8                    	 3585386	       336.0 ns/op
Benchmark/github.com/ompluscator/genjector
Benchmark/github.com/ompluscator/genjector-8        	21460600	        55.71 ns/op
Benchmark/github.com/vardius/gocontainer
Benchmark/github.com/vardius/gocontainer-8          	60947049	        20.25 ns/op
Benchmark/github.com/go-kata/kinit
Benchmark/github.com/go-kata/kinit-8                	  733842	      1451 ns/op
Benchmark/github.com/Fs02/wire
Benchmark/github.com/Fs02/wire-8                    	25099182	        47.43 ns/op
PASS

Examples

Detailed examples may be discovered contained in the interior “examples” bundle.

Some easy code blocks may be discovered beneath.

Easy Pointer

bundle instance

sort ServiceInterface interface {
  String() string
}

sort Service struct {
  worth string
}

func (s *Service) Init() {
  s.worth = "worth supplied contained in the Service"
}

func (s *Service) String() string {
  return s.worth
}

err := genjector.Bind(genjector.AsPointer[ServiceInterface, *Service]())
if err != nil {
  return err
}

occasion, err := genjector.NewInstance[ServiceInterface]()
if err != nil {
  return err
}

worth := occasion.String()
if worth != "worth supplied contained in the Service" {
  return err
}

Complicated Pointer

bundle instance

sort ServiceInterface interface {
  String() string
}

sort Service struct {
  worth string
}

func (s *Service) Init() {
  s.worth = "worth supplied contained in the Service"
}

func (s *Service) String() string {
  return s.worth
}

err := genjector.Bind(
  genjector.AsPointer[ServiceInterface, *Service](),
  genjector.AsSingleton(),
  genjector.WithAnnotation("service")
)
if err != nil {
  return err
}

occasion, err := genjector.NewInstance[ServiceInterface](
  genjector.WithAnnotation("service"),
)
if err != nil {
  return err
}

worth := occasion.String()
if worth != "worth supplied contained in the Service" {
  return err
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments