In this post I want to show how to refactor with the Dependency Inversion Principle. I find it hard to create a mock example with sufficient complexity, so I will just take the cryptocurrency buy software that I have featured in this blog a few times. In each iteration I will show the full code, talk about the problems.
My current hobby Python project is the Vigilant Crypto Snatch. This has grown to a size which made me refactor it a few times by now. Recently I've refactored it towards the clean architecture. I have introduced a two-level module structure such that I have components like configuration, marketplace, telegram and so on.
I have recently finished Clean Architecture1. In the book Robert C. Martin presents a certain way to organize components of a program. It consists of a series of concentric rings, where dependencies are only inward. There is a core of central business rules in the middle, depending on nothing else. Then there are use-cases around that, depending only on the core business rules. And then even further out are adapters to other libraries and interfaces. Since there are no dependencies on concrete things, these can easily be exchanged for something else, be substituted for tests and so on.
I'm currently reading Clean Architecture: A Craftsman's Guide to Software Structure and Design by Robert C. Martin, which came out in 2017. It is a book about software architecture and design. And today I was able to put something that annoys me about social network and messaging services into new words.