Skip to content

Latest commit

 

History

History
59 lines (47 loc) · 1.84 KB

README.md

File metadata and controls

59 lines (47 loc) · 1.84 KB

technology Go goversion-image

di-injector

di-injector helps you to manage dependency injection. It works in a very simple way: You just create a DiContainer and add all the dependencies you want to it, finally when you are coding your business logic, just mark the struct's fields you want to be injected with the tag: inject:"auto" and pass this structs to the library to manage the dependencies. The library will look for the "appropriate" dependency in its bag of dependencies and will inject the value.

Import Library

package main 
import di_injector "github.com/sebastianMurdoch/di-injector"

Example: Very silly example used just to show you how to use the library

package main

import (
	"fmt"
	di_injector "github.com/sebastianMurdoch/di-injector"
)

func main() {
	/* Create a container */
	c := di_injector.NewDiContainer()
	/* Add your dependencies */
	c.AddToDependencies(" Of course")
	c.AddToDependencies(&ServiceImpl{})
	/* Inject your dependencies */
	bo := businessObject{}
	c.InjectWithDependencies(&bo)
	fmt.Println(bo.SomeService.doService() + bo.CommonString)
}

type businessObject struct {
	CommonString string  `inject:"auto"`
	SomeService  Service `inject:"auto"`
}

type Service interface {
	doService() string
}

type ServiceImpl struct {}

func (s *ServiceImpl) doService() string {
	return "Can this library be more useless?"
}

To see a more usefull example please check out this post

Comments and PRs of any kind are welcome!! Thanks

Questions?