Loosely coupled Python code with Dependency Injection
Tags: python, programming, architecture, tutorial
Published: 2022-11-09 23:55:09


## Loosely coupled Python code with Dependency Injection

Software has to be flexible in order to respond to change. Dependency injection is a technique for managing the dependencies between software components. It is a design pattern that uses [inversion of control](https://en.wikipedia.org/wiki/Inversion_of_control) to provide the ability to swap out components for testing or other purposes without changing any other part of the system.

![Dependency Injection in Python](https://cdn-images-1.medium.com/max/2000/1*558ZzT23dBI2a7W9GDR08A.png)

The main idea behind dependency injection is to pass the responsibility for providing services from constructor functions or direct instantiation methods onto other objects. In other words, an object is given its dependencies instead of creating them. This way, if we want to add new features, we don’t have to change the existing code but instead, pass new dependencies into it.

## Creating Abstractions with Interfaces

There are three steps in writing loosely coupled Python code with dependency injection. The first step is to identify what dependencies the code needs, the second step is to create interfaces for each dependency, and the third step is to pass them into the dependent object through its constructor or method parameters.

Recently, I implemented functionality that required a byte encoder and decoder to manipulate business data (*I’ve simplified its implementation for readability purposes*). I abstracted out their responsibilities to interfaces. Python doesn’t have an interface keyword like Golang, but you can replicate interface functionality with the [ABC](https://docs.python.org/3/library/abc.html) library.

![Interfaces for encoder and decoder.](https://cdn-images-1.medium.com/max/2000/1*pE7ljP47xVPUWQF8ZHQY9A.png)

Focusing on the IPacketEncoder interface, a concrete encoder class must implement it. The initial business requirements required all types to be encoded and strings to be padded with their length. Luckily, Python provides the [struct](https://docs.python.org/3/library/struct.html) library to do most of the hard work.

![PaddedPacketEncoder concrete class.](https://cdn-images-1.medium.com/max/2000/1*6npvkUPlfUWH_aQaeK4w4Q.png)

## Implementing Dependency Injection

The encode_name function is a simple implementation for creating an encoded payload with the specified name. It takes an IPacketEncoder object and a string as parameters.

![PaddedPacketEncoder instance passed into encode_name function.](https://cdn-images-1.medium.com/max/2000/1*ubtImmLLVUTSG7RdU2JXpQ.png)

The two key implementation details are using the IPacketEncoder interface instead of the PaddedPacketEncoder concrete class and encoder is an argument rather than being initialized inside the function. **This is dependency injection.** Because IPacketEncoder is passed in, it can be easily extended, changed or provided with a [stub](https://en.wikipedia.org/wiki/Method_stub), thus keeping the system loosely coupled.

A few weeks later, there was a new requirement for an encoder to handle null terminated encodings. Luckily, due to the existing implementation, it can be easily implemented.

![NullTerminatedPacketEncoder concrete class.](https://cdn-images-1.medium.com/max/2000/1*jN4Y6XkS0YblS4PzPlFWCA.png)

NullTerminatedPacketEncoder essentially adds NULL_BYTE after each encoded string. Going back to the encode_name function, an instance of NullTerminatedPacketEncoder is a valid encoder argument since NullTerminatedPacketEncoder implements the IPacketEncoder interface similarly to PaddedPacketEncoder.

![NullTerminatedPacketEncoder passed into encode_name function.](https://cdn-images-1.medium.com/max/2000/1*9r1M-qsPxNlV9zdiEA-AVg.png)

If dependency injection wasn’t used here, it could result in code that is tightly coupled and difficult to change, for example:

![Coupled encoding Python code.](https://cdn-images-1.medium.com/max/2000/1*dqHXMd9IQDXIQ6zk9qFJDA.png)

If a new encoder is required, the encode_name function will have to be changed almost entirely along with its tests, extending delivery times. What’s worse, it could result in hidden side effects, especially if the original developer is no longer available.

To conclude, I’ll provide a less simplistic example using this technique. SocketService is responsible for decoding an operation code, calling its relevant handler and returning its result. All of its argument types are interface abstractions, including its constructor. The CLI is responsible for determining which encoder to use, i.e. NullTerminated or Padded, and passing its instance down to the SocketService class.

![SocketService utilising dependency injection.](https://cdn-images-1.medium.com/max/2000/1*zlPn_b4v3cB-wjlhtbLIVQ.png)

As projects continue to grow, its recommended to utilise a dependency injection framework to “inject” these dependencies automatically, such as [Dependency Injector](https://github.com/ets-labs/python-dependency-injector).

Hopefully, this demonstrates how to write loosely coupled Python code with dependency injection. Many Python developers dislike dependency injection because they feel it's not “pythonic”, but I would argue that its benefits can’t be discredited, especially when it's present in so many other programming languages.

Thanks for reading 😊. If you would like to connect with me, you can find me on [Linkedin](https://www.linkedin.com/in/shanenullain/) or [Github](https://github.com/ShaneNolan).