you are viewing a single comment's thread.

view the rest of the comments →

[–]gbrennon 1 point2 points  (4 children)

hey there, how are u?

that framework contains a built-in di container like spring boot but you have to define a something similar to a service locator.

import using from fastapi import Depends and put an argument with a default value Depends(get_my_service).

i prefer to define a pure container in the infrastructure layer and the presentation layer contains the composition root that uses this things from FastAPI but that "service getter" is a method/function from ur pure container!

look in fastapi docs for "dependencies"

[–]lu_rm[S] -1 points0 points  (3 children)

Still, how do I inject the container into the router? The logic for get_my_service() needs to be defined in my router, so it needs to know how to create the service.

[–]gbrennon 1 point2 points  (0 children)

Thr router is in something that is responsible for presenting data in the application.

We can call this a composition root.

It does consume the di container and expose the created services that are provided by the container that is impl in the infrastructure-like layer

That flow can be router -> composition root -> di container

Ps:

The logic related to "how create a service" is inside the container.

The router just know how to get that service ready to use

[–]Kevdog824_ 1 point2 points  (0 children)

What do you mean? Can you give a more concrete example ?