GithubHelp home page GithubHelp logo

maximzayats / godi Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 0.0 34 KB

๐Ÿš€ GoDI: Generic based DI in Go

Go 100.00%
dependency-injection di go go-generics go-library injection-dependency go-dependency-injection go-di

godi's Introduction

๐Ÿš€ 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.com/MaximZayats/godi/cmd/godi
  • go run github.com/MaximZayats/godi/cmd/godi init ./storage/decorators
  • go run github.com/MaximZayats/godi/cmd/godi --help

Example

  • Simple: Getting from container

    package main
    
    import (
        "fmt"
        "github.com/MaximZayats/godi/di"
    )
    
    func main() {
        di.AddSingletonByFactory[float32](func(c *di.Container) float32 {
            return 11.22
        })
    
        di.AddInstance[int](123)
    
        di.AddByFactory[string](func(c *di.Container) string {
            return "aabbcc"
        })
    
        fmt.Println(di.Get[int]())     // 123
        fmt.Println(di.Get[string]())  // "aabbcc"
        fmt.Println(di.Get[float32]()) // 11.22
        fmt.Println(di.Get[float32]()) // 11.22
    }
  • Injection: Pass arguments to function (Simple code generation is required)

    Full code: godi-fiber-example

    Snippet:

    type H = func(*fiber.Ctx) error
    
    // `stringFromDI` will be injected into the handler
    func handler(c *fiber.Ctx, stringFromDI string) error {
        return c.SendString("Hello from di: " + stringFromDI)
    }
    
    func main() {
        di.AddInstance[string]("I'm string from DI!!!", c)
        ...
        app.Get("/", injection.Inject[H](handler))
    }

Other examples

Usage

  1. Getting from container:

    • See examples above
  2. Injection (decorating):

    • Generate package for storing decorators:
      • go run github.com/MaximZayats/godi/cmd/godi init ./storage/decorators
      • Configure godi.injection:
        • import (
              ".../storage/decorators"
              "github.com/MaximZayats/godi/injection"
          )
          
          injection.Configure(decorators.Config)
      • Use injection:
        • // `a` and `b` will be auto injected in the function
          func Handler(c context.Context, a int, b string) int {
              fmt.Println(c, a, b)
              return a
          }
          
          injection.Configure(decorators.Config)
          
          // H is the type alias for the function after injection
          type H = func(context.Context) int
          decoratedHandler := injection.Inject[H](Handler)
          
          // IMPORTANT! You need to verify injection
          injection.MustVerifyInjections()
          
          decoratedHandler(context.TODO())
          See full example

Benchmarks

Code

goos: windows
goarch: amd64
pkg: github.com/MaximZayats/godi/benchmark
cpu: AMD Ryzen 5 1600 Six-Core Processor
BenchmarkGetFromFactorySingleton
BenchmarkGetFromFactorySingleton-12     500488393                2.443 ns/op
BenchmarkGetInstance
BenchmarkGetInstance-12                 495795447                2.403 ns/op
BenchmarkGetFromFactory
BenchmarkGetFromFactory-12              361722957                3.273 ns/op
PASS

godi's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.