Skip to content

cosiner/go-di

Repository files navigation

go-di

GoDoc Build Status Coverage Status Go Report Card

go-di is a library for Go to do dependency injection.

Documentation

Documentation can be found at Godoc

Example

package di_test

import (
	"fmt"
	"log"
	"net/http"

	di "github.com/cosiner/go-di"
)

type DBConfig struct{}
type DB struct{}
type UserHandler struct{}
type TopicHandler struct{}
type Router struct{}

type Providers struct{}

func (Providers) ProvideDBConfig() DBConfig {
	return DBConfig{}
}
func (Providers) ProvideDB(config DBConfig) (DB, error) {
	return DB{}, nil
}
func (Providers) ProvideHandlers(db DB) (UserHandler, TopicHandler, error) {
	return UserHandler{}, TopicHandler{}, nil
}

func (Providers) ProvideHttpHandler(args struct {
	User  UserHandler
	Topic TopicHandler
}) (res struct {
	Router  Router
	Handler http.Handler
}, err error) {
	res.Router = Router{}
	return res, nil
}

func ExampleInjector() {
	inj := di.New()

	err := inj.Provide(di.OptMethods(Providers{}, ""))
	if err != nil {
		log.Fatal(err)
	}
	err = inj.Run()
	if err != nil {
		log.Fatal(err)
	}
	var args struct {
		Handler http.Handler
	}
	err = inj.Inject(&args)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(args.Handler == nil)
	// Output: true
}

LICENSE

MIT.

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages