Write Your Own!#

Once you’ve understood how registries and containers interact (see Core Concepts if not), you already know how to write your own integrations!

All you need to do is:

  • Give the user an API to create a Registrys instance and attach it to the application so that the instance lives as long as the application does.

  • Ensure that the registry is closed when the application shuts down. Either by using the registry as a context manager, or by calling close() or aclose() on your registry instance. Or by telling the user to do it themselves – some web frameworks don’t have a way to hook into the application shutdown process.

  • Give the user an API to access the registry instance such they can register factories on startup.

  • On each request, create a Container instance and attach it to the request, so it lives as long as the request.

  • Ensure that the container instance is closed when the request is done.

  • Give the user an API to access the container instance in views, so they can get() their services.

    Tip

    If you need to call a function to extract the container from a request object or similar, consider calling it svcs_from like we do in our integrations.

That’s it!

If you need inspiration, look at our integrations, like the one for Flask that is based on thread locals, the one for Pyramid that extracts the container from a request object, or the one for AIOHTTP that is async.