Dynamic, Lazy Dependency Injection in Python

Automatic Python dependency injection to make your code more testable, decoupled, uncomplicated and readable

Photo by Rapha Wilde / Unsplash

Dependency Injection (DI) solves many problems by improving testability, decoupling, maintainability and readability. However, managing dependencies can sometimes introduce new problems. When do we initialize them? How do we initialize? Can they be reused effectively?

In order to take DI to the next level I’ve created FastInject: a Python package that simplifies dependency management with just a few decorators. FastInject automatically handles dependency instantiation and injection so that you can focus on your project. Features:

  • improved performance: Only create dependencies when they are actually needed
  • simpler initialization: Dependencies are resolved dynamically
  • avoid circular imports: Dependency resolution is deferred until runtime
  • improved flexibility: Dependencies can be influenced by runtime information or configuration

Let’s code!

Contents

  1. DI refresher: A comparison of code that uses DI versus code that doesn’t
  2. Dependency management with FastInject…