When it comes to maintaining a large application codebase, sometimes you are confused with dependencies. Moreover, when you create a new struct that depends on a struct with many attributes, you need to provide those attributes manually. What if those attributes need other structs? It will take forever.
Dependency injection is a technique you need to learn to maintain a large application codebase. It helps you to provide the necessary attribute to a struct automatically. Using dependency injection from a small codebase is a good investment. Although a dependency injection on a small code base seems like an over-engineered solution, it will help you whenever your codebase grows big or when the team members are increasing.
In Go, there are many dependency injection tools out there. You can choose whichever you want. Some dependency injection tools work at runtime levels, which use reflection to provide the dependencies but are hard to debug. Google Wire provides a dependency injection tool that works at compile-time. Google Wire utilises code generation to help developers achieve a dependency injection pattern at compile-time. So, in this article, you will learn how to implement a dependency injection pattern using Google Wire.
Let’s say you have this
example module structure.
domain/user.go is where you put your structs and interfaces, for example:
user/ directory is where you put your implementation for those interfaces, for example:
As you can see from the code above:
It is pretty much straightforward. So If you want to create the
handler, first you need to create
a database connection,
repository, and finally the
service. That’s all for one domain. If you have multiple domains, and each struct has many attributes, it will be more bulky, right? Before it gets more complicated, let’s try to use
Using Google Wire
After installing Google Wire, let’s talk about
Provider is an initializer function where you create a single struct. For example:
Pretty much it. Other than that, you can also create a singleton provider using
sync.Once to keep the creation only running once. For example, here is what your
user/provider.go file contents look like:
As it sounds, a
provider set is a set of providers. It groups providers into one. If you are going to use those providers are frequently together, a provider set will be helpful. You can add a provider set to your
So, in case you need to import your providers one by one, you can easily import your provider set.
Interface binding is needed to bind an abstract interface to its concrete implementation. Your
UserService interface depends on a
UserRepository interface, but your
repository provider returns
a pointer of the repository struct, not a
UserRepository interface. There are two ways to fix this problem:
ProvideRepository’s return type to
But since there’s a Go proverb that says
accept interfaces, return structs. It would be wise if you chose the second option. Now you can bind your interfaces inside the provider set.
Now, after all the dependencies have a provider, let’s move on to the
You need to define the
+build wireinject tag to make Google Wire generate the complete code from your current file.
wire.Build is flexible. You can refer to the documentation to use all the possibilities. In this case, since you have defined all the providers inside the provider set, you only need to put the provider set as an argument.
Generate the Code
Now, you can use wire CLI tools to generate the complete code by executing
wire ./... command in your project root directory.
Here is the content of
Wire function automatically generated by wire. You can directly use the
Wire function inside your main function.
Using Google Wire on a small codebase seems like a waste, but it helps You create a cleaner constructor function. Instead of initializing all the structs at the main function, you can depend on Wire to inject all the dependencies.
Thank you for reading!