Writing clean, maintainable code is a challenging task. Fortunately, there are many patterns, techniques, and reusable solutions available to us to make achieving that task much easier. Dependency Injection is one of those techniques, which is used to write loosely-coupled yet highly-cohesive code.
In this post, we’ll show you how to implement Dependency Injection as you develop an app for plotting historic weather data. After developing the initial app, using Test-Driven Development, you’ll refactor it using Dependency Injection to decouple pieces of the app to make it easier to test, extend, and maintain.
By the end of this post, you should be able to explain what Dependency Injection is and implement it in Python with Test-Driven Development (TDD).
In software engineering, Dependency Injection is a technique in which an object receives other objects that it depends on.
Benefits:
For more, refer to Martin Fowler’s Forms of Dependency Injection article.
To see it in action, let’s take a look at a few real-world examples.
Scenario:
#injection #dependency #python