/python-inject-practice

Practice with python dependency injection using python-inject @ https://github.com/ivankorobkov/python-inject

Primary LanguagePythonMIT LicenseMIT

python-inject-practice

Practice with python dependency injection using python-inject @ https://github.com/ivankorobkov/python-inject

This project was created to learn how to perform dependency injection, the pythonic way using python-inject. I'll be recording my thoughts and experiments in this repository.

Experiments

  • "instance" injection using python-inject. This injection strategy allows me to pull a binded dependency by object type from my injector given my configuration. See: instance_injection.py

  • "attribute" injection using python-inject. This injection strategy allows me to bind a dependency from my injector into class instances. See: attribute_injection.py

  • "param" injection using python-inject. This injection strategy allows me to inject dependencies into methods by param position or key. See: param_injection.py

What I've Learned

Preface: Until recently (3/1/2019) I've never knowingly used dependency injection. The design pattern was not something I was consciously aware of, however I leverage it almost daily in both personal projects and in production.

  • Dependency injection is more of a development pattern than anything else.
  • Contrary to my original confusion, there are multiple ways of tackling dependency injection.
  • Angular.js, which I've been actively using for the past couple of years, revolves around dependency injection:
    angular.module('myModule', [])
      .factory('serviceId', ['depService', depService => {
        // ...
      }])*
    
  • DI with python-inject can be very powerful with proper context control. IoC can also be incredibly helpful when using asynchronous code.

Resources


Copyright (c) 2019 John Nolette Licensed under the MIT License.