Exploring ways of injecting dependencies
From the previous discussions, we know that FastAPI has a built-in container through which some objects are injected and instantiated. Likewise, we have learned that the only FastAPI components that are injectables are those so-called dependables, injectables, or dependencies. Now, let us enumerate different ways to pursue the DI pattern in our application.
Dependency injection on services
The most common area where DI occurs is in the parameter list of a service method. Any discussions regarding this strategy have been tackled already in the previous examples, so we only need to present additional points concerning this strategy:
- First, the number of custom injectables a service method should take is also part of the concern. When it comes to complex query parameters or request bodies, API services can take more than one injectable as long as there are no similar instance variable names among these dependables. This variable...