• Stars
    star
    28
  • Rank 882,216 (Top 18 %)
  • Language
    Go
  • License
    MIT License
  • Created about 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Golang type-safe dependency injection

nject - dependency injection

Stability: Active GoDoc unit tests report card codecov

Install:

go get github.com/muir/nject

This package provides type-safe dependency injection without requiring users to do type assertions.

Dependencies are injected via a call chain: list functions to be called that take and return various parameters. The functions will be called in order using the return values from earlier functions as parameters for later functions. Only the functions that are needed to be called in order to be able to call the last function in the chain are invoked. Then the last function in the chain is called.

Parameters are identified by their types. To have two different int parameters, define custom types.

Type safety is checked before any functions are called.

Functions whose outputs are not used are not called. Functions may "wrap" the rest of the list so that they can choose to invoke the remaining list zero or more times.

Chains may be pre-compiled into closures so that they have very little runtime penealty.

example

func example() {
	// Sequences can be reused.
	providerChain := Sequence("example sequence",
		// Constants can be injected.
		"a literal string value",
		// This function will be run if something downstream needs an int
		func(s string) int {
			return len(s)
		})
	Run("example",
		providerChain,
		// The last function in the list is always run.  This one needs
		// and int and a string.  The string can come from the constant
		// and the int from the function in the provider chain.
		func(i int, s string) {
			fmt.Println(i, len(s))
		})
}

Main APIs

Nject provides two main APIs: Bind() and Run().

Bind() is used when performance matters: given a chain of providers, it will write two functions: one to initialize the chain and another to invoke it. As much as possible, all dependency injection work is done at the time of binding and initialization so that the invoke function operates with very little overhead. The chain is initialized when the initialize function is called. The chain is run when the invoke function is called. Bind() does not run the chain.

Run() is used when ad-hoc injection is desired and performance is not critical. Run is appropriate when starting servers and running tests. It is not recommended for HTTP endpoint handlers. Run exectes the chain immediately.

Identified by type

Rather than naming values, inputs and outputs are identified by their types.
Since Go makes it easy to create new types, this turns out to be quite easy to use.

Types of providers

Multiple types of providers are supported.

Literal values

You can provide a constant if you have one.

Injectors

Regular functions can provide values. Injectors will be called at initialization time when they're marked as cacheable or at invocation time if they're not.

Injectors can be memoized.

Injectors can return a special error type that stops the chain.

Injectors can use data produced by earlier injectors simply by having a function parameter that matches the type of a return value of an earlier injector.

Injector chains can be collapsed to become a single injector -- this allows the chain to branch out in a tree structure so that it can generate multiple copies of the same type.

Injectors can be automatically reordered so that outputs are generated before they're required as inputs.

Wrappers (aka Middleware)

Wrappers are special functions that are responsible for invoking the part of the injection chain that comes after themselves. They do this by calling an inner() function that the nject framework defines for them.

Any arguments to the inner() function are injected as values available further down the chain. Any return values from inner() must be returned by the final function in the chain or from another wrapper further down the chain.

Composition

Collections of injectors may be composed by including them in other collections.

Debugging injector chains

If you chain successfully binds but does not do what you expect, add something into your chain that receives the nject.Debugging type:

func(d *nject.Debugging) {
	fmt.Println("Injectors included\n", d.Included)
}

If you chain does not bind, then Debugging won't help. Injection chain errors attempt to be self-explanatory, but sometimes that's not enough.

If you're building your injection sequence dynamically, it may be useful to print the injection chain. It has a String() method.

If you think that the issue is with nject, then open an issue with the reproduce information that is available as part of the debugging (Debugging.Reproduce) or available by calling nject.DetailedError(err) on the error returned from Bind() or Run().

Uses

Nject has been successfully used for:

  • Test frameworks so that test authors specify the test dependencies with the types injected into a function that runs a test. Using wrappers or t.Clanup() in the injection chain allows test writers to skip all or most test cleanup.
  • Endpoint handlers. Nject makes middleware easier to write.
  • Server startup.

Related packages

The following use nject to provide nicer APIs:

  • nape: dependency injection wrappers for binding http endpoint handlers using gorilla/mux
  • nchi: http router on top of httprouter with a go-chi-like API
  • nfigure: configuration and flag processing
  • npoint: dependency injection wrappers for binding http endpoint handlers
  • nserve: injection chains for for starting and stopping servers
  • ntest: support functions and hints for using nject to write tests
  • nvalid: enforce that http endpoints conform to Swagger definitions
  • nvelope: injection chains for building endpoints

Development status

The core of nject is stable and reliable. New features have tests, but may not have enough production use to be reliable.

This repo represents continued development of Blue Owl's nject base.

Nject will follow strict semver behavior except (1) things noted as experimental; and (2) releases that are less than 10 days old may have corrections applied that are not strictly backwards compatible with the recent release (for example, 1.0.0 accidentally exported a type that should have remained private).

Prior to release 0.2.0, nject was bundled with other packages. Those other packages are now in their own repos: npoint, nserve, and nvelope. Additionally, npoint was split apart so that the gorilla dependency is separate and is in nape.

Please create issues for any suggested features, improvements, or problems you run into.

More Repositories

1

libschema

database schema migrations on a per-library basis [Go]
Go
15
star
2

nchi

golang http router with elegance, speed, and flexibility
Go
13
star
3

Net-Netmask

Net::Netmask perl module
Perl
6
star
4

reflectutils

Golang utility functions for working with reflection
Go
6
star
5

nfigure

Golang struct-tag based configfile and flag parsing
Go
6
star
6

Time-modules

CPAN modules: TIme::ParseDate, Time::CTime, Time::JulianDay etc
Perl
5
star
7

sqltoken

High performance SQL tokenizer - Golang
Go
4
star
8

Cisco-Reconfig

The Cisco::Reconfig perl module
Perl
4
star
9

drbd-lxd

Instructions/tools for shared nothing DRBD LXC HA setup
Shell
4
star
10

nvelope

injection chains for building endpoints (golang)
Go
3
star
11

xop-go

Golang structured logging/tracing framework
Go
3
star
12

nserve

injection chains for for starting and stopping servers
Go
3
star
13

Daemon-Generic

The Daemon::Generic CPAN perl module
Perl
3
star
14

IO-Event

the CPAN IO::Event module
Perl
2
star
15

Stream-Aggregate

The Stream::Aggregate perl module
Perl
2
star
16

npoint

dependency injection wrappers for binding http endpoint handlers
Go
2
star
17

OOPS

OOPS cpan module --- object persistant store
Perl
2
star
18

nape

dependency injection of endpoint handlers using gorilla/mux
Go
2
star
19

Eval-LineNumbers

Eval::LineNumbers perl module on CPAN
Perl
1
star
20

Mail-SendVarious

Mail::SendVarious perl module
Perl
1
star
21

Proc-JobQueue

Proc::JobQueue perl modules
Perl
1
star
22

HTML-Transmorgify

HTML::Transmorgify perl module
Perl
1
star
23

Net-SMTP-Receive

Net::SMTP::Receive perl module
Perl
1
star
24

Text-Tabs-Wrap

The Text::Tabs & Text::Wrap CPAN Modules
1
star
25

rinetd.pl

rinetd.pl - tcp redirection server
Perl
1
star
26

genpxelinux.pl

genpxelinux.pl command
Perl
1
star
27

Proc-Parallel

The Proc::Parallel and RCP::ToWorker CPAN modules
Perl
1
star
28

Config-Checker

Config::Checker perl module
Perl
1
star
29

Object-Dependency

Object::Dependency perl module
Perl
1
star
30

File-Slurp-Remote

The File::Slurp::Remote cpan module
Perl
1
star
31

gwrap

Golang generic wrappers for core library functions
Go
1
star