Summary
One aspect that makes a framework easy and practical to use is its support for the IoC principle. FastAPI has a built-in container that we can utilize to establish dependency among components. The use of a DI pattern to integrate all these components through wiring is a strong prerequisite in building microservice-driven applications. From simple injection using Depends()
, we can extend DI to build pluggable components for database integration, authentication, security, and unit testing.
This chapter also introduced some third-party modules such as Dependency Injector and Lagom that can design and customize containers. Because of the limitations of FastAPI on DI, there are external libraries that can help extend its responsibility to assemble, control, and manage object creation in a container. These third-party APIs can also create singleton objects, which can help decrease the heap size in the PVM.
Aside from performance tuning and memory management, DI can contribute...