Skip to content

Resource Factory for Short-Lived Session Objects #455

Open
@moon-bits

Description

@moon-bits

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 each
instance of get_user.

def create_db_session(db: Database):
  session = db.create_session()
    yield session
  db.close_session(session)

class APIContainer:
  database_session = providers.Resource(create_db_session)
  
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`
  ) 

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?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions