Dependency Injection
Explore how dependency injection enhances Go applications by separating dependency construction from usage. Learn to refactor tightly coupled code into loosely coupled components using interfaces and constructor functions. This lesson guides you through incremental improvements toward flexible, reusable, and easily testable code.
We'll cover the following...
Dependency injection (DI) is a software design pattern that aims to separate the building of a dependency from its usage. It’s based on the concept that we don’t have to instantiate a dependency in the same place where we’re going to use it. The burden of constructing is exported to another component of our software, which acts as a builder. If we initialize the dependency within our function, we introduce a rigidity that can prevent us from writing flexible and testable code
The previous code snippet shows us how to achieve dependency injection in our code. As a rule of thumb, if we create an instance of a struct within the same component that will use it (maybe with the := operator or the new keyword), we’re not using dependency injection.
If we rely on it, it’s easier to build loosely-coupled code that will result in more reusable, flexible, and testable code.
In this lesson, we’ll explore DI features by using an example application to learn its benefit in software development.
Build an online course platform
The application we’ll use is an online course platform like Educative. For the sake of the demo, there are ...