Skip to content

Commit

Permalink
finished documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ompluscator committed Oct 16, 2022
1 parent 805f24e commit 6acf33e
Show file tree
Hide file tree
Showing 9 changed files with 461 additions and 361 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: go
go:
- 1.18
- stable
- master

script:
- go test -v ./...
Expand Down
137 changes: 135 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,135 @@
# genjector
Reflection-free Dependency Injection framework for Go 1.18+
# Package Genjector
Reflection-free Run-Time Dependency Injection framework for Go 1.18+

## The Goal
The purpose of The Genjector package is to provide a Dependency
Injection framework without relying on reflection and depending solely
on Go Generics (provided from Go version 1.18).

It supports many different features:
+ Binding concrete implementations to particular interfaces.
+ Binding implementations as references or values.
+ Binding implementations with Provider methods.
+ Binding implementations with concrete instances.
+ Define Binding as singletons.
+ Define annotations for Binding.
+ Define slices and maps of implementations.
+ ...

## Benchmark
While providing the most of known features of Dependency Injection
frameworks, The Genjector Package also delivers top performance
comparing to current widely used Run-Time DI frameworks, (so, solutions
based on code generators are excluded).

```shell
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 can be found inside inner "examples" package.

Some simple code blocks can be found bellow.

### Simple Reference
```go
package example

type ServiceInterface interface {
String() string
}

type Service struct {
value string
}

func (s *Service) Init() {
s.value = "value provided inside the Service"
}

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

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

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

value := instance.String()
if value != "value provided inside the Service" {
return err
}
```

### Complex Reference
```go
package example

type ServiceInterface interface {
String() string
}

type Service struct {
value string
}

func (s *Service) Init() {
s.value = "value provided inside the Service"
}

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

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

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

value := instance.String()
if value != "value provided inside the Service" {
return err
}
```
Loading

0 comments on commit 6acf33e

Please sign in to comment.