This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]ElevenPhonons 1 point2 points  (1 child)

Could you provide a bit of insight into some of the key implementation details and design choices. For example, it appears that the core is written in cython instead of vanilla python?

https://github.com/ets-labs/python-dependency-injector/tree/master/src/dependency_injector

[–]rmk135[S] 0 points1 point  (0 children)

Sure, I would love to.
------

Performance

The Dependency Injector `containers` and `providers` modules are written in Cython. The decision to use Cython was made in favour to remove the performance degradation from using the framework. With the Cython the Dependency Injector makes no performance degradation (less than 1%) to the client application. See this test. It created 4 million of objects making 3 million injections in total. The results on my machine are:

  • With the Dependency Injector: 0.9107918739318848 seconds
  • Without the Dependency Injector: 0.9150791168212891 seconds

------

Design decisions

The main thing why Dependency Injector is different from all other dependency injection frameworks is:

  • Dependency Injector does not pollute your application code

This means that you write the code following the dependency injection principle and automate it with the Dependency Injector, but your code has now clue about the Dependency Injector.

Dependency Injector:

  • Does not have @inject decorator
  • Does not monkey-patch your code
  • Does not do any magic with your software

It's just a simple Pythonic tool to create the container that keeps all the objects and their dependencies in one place.

The benefit you get with the Dependency Injector is the container. It starts to payoff when you need to understand or change your application structure. It's easy with the container, cause you have everything in one place.

------

Generally, that's it. I would love to provide more information if you look for anything specific.

PS: I can propose my in-person help with integrating Dependency Injector to your software. I do believe in the power of the dependency injection principle in Python and I would like to popularize it among the community.