dihedral

command module
v0.0.0-...-6d43c30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 14 Imported by: 0

README

Dihedral

Build Status

Dihedral is a compile-time injection framework for Go.

Getting started

> go get -u github.com/dimes/dihedral

Create a type you want injected:

type ServiceEndpoint string  // Name this string "ServiceEndpoint"
type Service struct {
    inject  embeds.Inject    // Auto-inject this struct 
    Endpoint ServiceEndpoint // Inject a string with name "ServiceEndpoint"
}

Create a module to provide non-injected dependencies:

// Each public method on this struct provides a type
type ServiceModule struct {}
func (s *ServiceModule) ProvidesServiceEndpoint() ServiceEndpoint {
    return ServiceEndpoint("http://hello.world")
}

Create a component as the root of the dependency injection:

type ServiceComponent interface {
    InjectService() *Service
}

Create a definition with the target component and the module configuration:

type ServiceDefinition interface {
    Modules() (*ServiceModule, dbstore.DBBindingModule)
    Target() ServiceComponent
}

Generate the bindings

> dihedral -definition ServiceDefinition

Use the bindings

func main() {
    // dihedral generates the digen package
    component := digen.NewDihedralServiceComponent()
    service := component.InjectService()
    fmt.Println(string(injected.Endpoint)) # Prints "http://hello.world"
}

Further Reading

Differences from Wire

Wire, Google's injection framework, is another compile time framework for Go. Both frameworks are inspired by Dagger. Dihedral differs from Wire in that Dihedral focuses on auto-injected components and self-contained modules, whereas Wire focuses more on type registration via provider functions. Dihedral also leverages struct receivers for better organization of runtime provided types. These features make Dihedral nicer to work with.

Dihedral's component structure also enables one to have multiple injected components that share modules. The type annotation system allows for auto-injected components, provided modules, and, in the future, sub-components that have a different scope than the parent component.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package gen contains the logic for generating the source code
Package gen contains the logic for generating the source code
internal
example/bindings
Package bindings setups the the dependency bindings for a component
Package bindings setups the the dependency bindings for a component
example/bindings/digen
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
Package resolver handles resolving dependencies
Package resolver handles resolving dependencies
Package structs contains common type definitions
Package structs contains common type definitions
Package typeutil contains helepr methods for interaction with ast types
Package typeutil contains helepr methods for interaction with ast types

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL