title: Compile-time Dependency Injection With Go Cloud's Wire date: 2018-10-09 by:
The Go team recently announced the open source project Go Cloud, with portable Cloud APIs and tools for open cloud development. This post goes into more detail about Wire, a dependency injection tool used in Go Cloud.
Dependency injection is a standard technique for producing flexible and loosely coupled code, by explicitly providing components with all of the dependencies they need to work. In Go, this often takes the form of passing dependencies to constructors:
// NewUserStore returns a UserStore that uses cfg and db as dependencies. func NewUserStore(cfg *Config, db *mysql.DB) (*UserStore, error) {...}
This technique works great at small scale, but larger applications can have a complex graph of dependencies, resulting in a big block of initialization code that‘s order-dependent but otherwise not very interesting. It’s often hard to break up this code cleanly, especially because some dependencies are used multiple times. Replacing one implementation of a service with another can be painful because it involves modifying the dependency graph by adding a whole new set of dependencies (and their dependencies...), and removing unused old ones. In practice, making changes to initialization code in applications with large dependency graphs is tedious and slow.
Dependency injection tools like Wire aim to simplify the management of initialization code. You describe your services and their dependencies, either as code or as configuration, then Wire processes the resulting graph to figure out ordering and how to pass each service what it needs. Make changes to an application's dependencies by changing a function signature or adding or removing an initializer, and then let Wire do the tedious work of generating initialization code for the entire dependency graph.
Go Cloud‘s goal is to make it easier to write portable Cloud applications by providing idiomatic Go APIs for useful Cloud services. For example, blob.Bucket provides a storage API with implementations for Amazon’s S3 and Google Cloud Storage (GCS); applications written using blob.Bucket
can swap implementations without changing their application logic. However, the initialization code is inherently provider-specific, and each provider has a different set of dependencies.
For example, constructing a GCS blob.Bucket
requires a gcp.HTTPClient
, which eventually requires google.Credentials
, while constructing one for S3 requires an aws.Config
, which eventually requires AWS credentials. Thus, updating an application to use a different blob.Bucket
implementation involves exactly the kind of tedious update to the dependency graph that we described above. The driving use case for Wire is to make it easy to swap implementations of Go Cloud portable APIs, but it's also a general-purpose tool for dependency injection.
There are a number of dependency injection frameworks out there. For Go, Uber's dig and Facebook's inject both use reflection to do runtime dependency injection. Wire was primarily inspired by Java's Dagger 2, and uses code generation rather than reflection or service locators.
We think this approach has several advantages:
Wire has two basic concepts: providers and injectors.
Providers are ordinary Go functions that “provide” values given their dependencies, which are described simply as parameters to the function. Here's some sample code that defines three providers:
// NewUserStore is the same function we saw above; it is a provider for UserStore, // with dependencies on *Config and *mysql.DB. func NewUserStore(cfg *Config, db *mysql.DB) (*UserStore, error) {...} // NewDefaultConfig is a provider for *Config, with no dependencies. func NewDefaultConfig() *Config {...} // NewDB is a provider for *mysql.DB based on some connection info. func NewDB(info *ConnectionInfo) (*mysql.DB, error) {...}
Providers that are commonly used together can be grouped into ProviderSets
. For example, it's common to use a default *Config
when creating a *UserStore
, so we can group NewUserStore
and NewDefaultConfig
in a ProviderSet
:
var UserStoreSet = wire.ProviderSet(NewUserStore, NewDefaultConfig)
Injectors are generated functions that call providers in dependency order. You write the injector's signature, including any needed inputs as arguments, and insert a call to wire.Build
with the list of providers or provider sets that are needed to construct the end result:
func initUserStore() (*UserStore, error) { // We're going to get an error, because NewDB requires a *ConnectionInfo // and we didn't provide one. wire.Build(UserStoreSet, NewDB) return nil, nil // These return values are ignored. }
Now we run go generate to execute wire:
$ go generate wire.go:2:10: inject initUserStore: no provider found for ConnectionInfo (required by provider of *mysql.DB) wire: generate failed
Oops! We didn't include a ConnectionInfo
or tell Wire how to build one. Wire helpfully tells us the line number and types involved. We can either add a provider for it to wire.Build
, or add it as an argument:
func initUserStore(info ConnectionInfo) (*UserStore, error) { wire.Build(UserStoreSet, NewDB) return nil, nil // These return values are ignored. }
Now go generate
will create a new file with the generated code:
// File: wire_gen.go // Code generated by Wire. DO NOT EDIT. //go:generate wire //+build !wireinject func initUserStore(info ConnectionInfo) (*UserStore, error) { defaultConfig := NewDefaultConfig() db, err := NewDB(info) if err != nil { return nil, err } userStore, err := NewUserStore(defaultConfig, db) if err != nil { return nil, err } return userStore, nil }
Any non-injector declarations are copied into the generated file. There is no dependency on Wire at runtime: all of the written code is just normal Go code.
As you can see, the output is very close to what a developer would write themselves. This was a trivial example with just three components, so writing the initializer by hand wouldn't be too painful, but Wire saves a lot of manual toil for components and applications with more complex dependency graphs.
The Wire README goes into more detail about how to use Wire and its more advanced features. There's also a tutorial that walks through using Wire in a simple application.
We appreciate any input you have about your experience with Wire! Wire's development is conducted on GitHub, so you can file an issue to tell us what could be better. For updates and discussion about the project, join the Go Cloud mailing list.
Thank you for taking the time to learn about Go Cloud's Wire. We’re excited to work with you to make Go the language of choice for developers building portable cloud applications.