Skip to content

Is that-depends possible to support inject almost everywhere without wiring? #39

Closed

Description

@rumbarum

I used to use dependency-injector(di) for inject config, complex obj or so on everywhere,
like described on their docs.

As I know, wiring is a key action which reads code and replace initial dummy obj (Provide(...)) to real one(injecting object) before run python process.
I also have experienced injecting from central Container instance made circular import error if i use container directly over related modules and that case only literal inject works supported by wiring.

Will that-depends provide similar usage covering this case without wiring?

Activity

  1. lesnik512 commented on Jun 21, 2024

    @lesnik512
    Member

    Hello, @rumbarum

    1. In case of that-depends there is no wiring, so such thing won't work and I'm not sure that it's needed:
    service: Service = Provide[Container.service]

    You can just build dependency anywhere in you code like this

    1. About circular import error: I personally think that circular import error should be resolved by refactoring code instead of hiding it by wiring of DI.
  2. rumbarum commented on Jun 21, 2024

    @rumbarum
    Author

    @lesnik512
    Thank you for your answer.

    This is my use case of inject something directly. I could wrap it by something callable and inject it as args. But this is much simpler. So I applied this pattern when necessary.

    session: async_scoped_session = Provide["session"]
    
    class SQLAlchemyMiddleware:
        def __init__(self, app: ASGIApp) -> None:
            self.app = app
    
        async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
            session_id = str(uuid4())
            context = set_session_context(session_id=session_id)
            try:
                await self.app(scope, receive, send)
            except Exception as e:
                raise e
            finally:
                await session.remove()
                reset_session_context(context=context)

    Anyway, I will try review your package.

  3. locked and limited conversation to collaborators on Jun 21, 2024
  4. converted this issue into a discussion #40 on Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions