Skip to content

di-injector helps you manage dependency injection in your project

License

Notifications You must be signed in to change notification settings

sebastianMurdoch/di-injector

Repository files navigation

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?

About

di-injector helps you manage dependency injection in your project

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages