Skip to content

blbergwall/depend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

depend

A simple dependency injection tool for go.

GoDoc Go Report Card License

Install

go get github.com/blbergwall/depend

Overview

Simple because it only deals with interfaces.

All dependencies are first added, then they are all built and interdependences resolved and verified. The result can be used to provide dependencies to functions.

Usage

Import the depend package

import "github.com/blbergwall/depend"

Create a new dependency builder:

builder := depend.New()

Assume the following interfaces and implementations that will be depended on:

type testInterface1 interface { Method1() string }
type testInterface2 interface { Method2() string }

type testStruct1 struct { field string }
type testStruct2 struct { field string }

func new1() testInterface1 { return &testStruct1{field: "testInterface1"} }
func (ts1 *testStruct1) Method1() string { return ts1.field }

func new2Consume1(i1 testInterface1) testInterface1 {
	return &testStruct1{field: "testInterface2 from 1"}
}
func (ts2 *testStruct2) Method2() string { return ts2.field }

Then these can be added with:

builder.Add(new1)
builder.Add(new2Consume1)

Once all dependencies have been added:

provider, err := builder.Build()

This causes all added functions to be called once. If any functions return errors or any functions have dependencies that have not been added or there are any circular references an error will be returned.

Now given a function:

func toProvideFor(i2 testInterface2) (string, error) {
	return i2.Method1(), nil
}

provider can be used to provide for it:

result, err := provider.ProvideFor(toProvideFor)

In general result will be the first result from the function and err will be the last result from the function if it is an error.

If more than one of the same interface is added then only a slice of that interface will be provided.

For more examples see the tests

Also check out the documentation

Why?

Needed a dependency injection system for a project and existing systems seemed overly complicated.

One of the main points of dependency injection is to make code more testable. Only injecting interfaces makes code more testable and anything can be wrapped in an interface. So hoping the interface only constraint will be a net benefit.

License

MIT

About

A simple dependency injection tool for go

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages