-
-
Notifications
You must be signed in to change notification settings - Fork 326
Resource Factory for Short-Lived Session Objects #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@rmk135 do you need more information? I'm currently stucked in my project due this obstacle 😿 |
Hey @moon-bits , You need to shutdown resources explicitly. In that case resource will be initialized again and you'll have a new database connection for each For instance, if you use You can make the same thing with a resource provider. Just call PS: Apologies for the delayed response. |
Hi @rmk135 , I see. I wanted something more out-of-the-box by not calling Do you think my use case can also be achieved by a Thanks again for your help and work! |
I understand. The problem is that it's unknown when you expect to call
You can change I would suggest you to try this approach:
class Container();
database = provider.Factory(db.create_db, ...)
get_user = providers.Factory(
use_cases.GetUser,
database_session_provider=api.database_session.provider,
)
class GetUser:
def __init__(self, database_session_provider):
self._database_session_provider = database_session_provider
def execute(self):
with self._database_session_provider() as session: # session is created here
...
# and closed after "with" block is over |
Thanks @rmk135 ! Apologies for asking you again, but now I remember why I wanted it to behave like a The thing is that So the requirement is to have a i.e. class RepositoryContainer:
user_repository = providers.Factory(
MyRepository,
database_session=api.database_session # must be the same database session instance as in `get_user`
)
class UseCaseContainer:
api = providers.DependenciesContainer()
get_user = providers.Factory(
use_cases.GetUser,
database_session=api.database_session # `database_session` must be created for every instance of `get_user`
user_repository=...
) The example linked in the documentation is nice, but unfortunately not practical as the database session is only used within one repository. |
Ok, I see what you're looking for. I don't think there is anything out-of-the box to make it work that way. This is an interesting problem. I would like to address it in the framework one day. |
Running into the same situation. Would love to have a database session that is essentially tied to a request/response cycle and all the dependancies that use it during that request/response cycle and closes up afterwards. It first sight the "Closing" marker does what we want but that only seems to work in combination with "Provide" when directly referencing the "Resource" not on dependancies that have the resource as a sub dependancy. You would also need an idempotent shutdown for that on the Resource whenever you Provide multiple dependancies wrapped in a Closing that all reference that single Resource. |
Having the same issue as well. |
Sadly no, I was using FastAPI as HTTP framework so opted to switch (back) to the builtin DI solution that FastAPI provides. |
Same situation, I'm trying to resolve this with "Closing" marker, but it's not working: # handlers
@inject
async def create_project(
request: web.Request,
project_repository: ProjectRepository = Closing[Provide[Container.project_repository]],
) -> web.Response:
....
# di
class Container(containers.DeclarativeContainer):
db_session = providers.Resource(get_session, database=db)
project_repository = providers.Factory(
ProjectRepository,
session=db_session
) But if I add |
Hi @rmk135
You did a great job with this library! Thank you very much for that!
Yet, I'm having a hard time with a very common approach when using
Resource
: A database session should be created for eachinstance of
get_user
.But when running the application, it only creates ONE database session for the whole lifetime of the application, not
N
(the number of API calls).How is it possible to create a database session for each
get_user
instance?The text was updated successfully, but these errors were encountered: