Diy
I really like the Laravel Service Container.
diy
is my attempt at recreating something in Python.
After working with FastAPI, I notices some shortcomings in their DI system.
Other solutions in the Python ecosystem also did not satisfy me.
Most of the issues I had are described in the guiding principles section of diy
s documentation, though no libraries are explicitly mentioned.
Quickstart
There are two important parts in diy
.
Specification
s contain explicit instructions about how to resolve function parameters, construct types or map protocols/interfaces to a concrete implementation.
This is our example class, which only takes one parameter
This teaches our container how to build an ApiClient
The container retrieves a spec, and uses the contained instructions to construct types
This is how you get an instance
You can also call functions by letting their parameters be resolved from the container
Notes
- focus on good error messages
- dependency visualization
- autowiring
- validation