dishka icon indicating copy to clipboard operation
dishka copied to clipboard

Ping dependencies

Open Tishka17 opened this issue 1 year ago • 3 comments

Add separate decorator to provide ping functionality. It has no scope during declaration and can be used to run specific code declated in providers:

Declaration:

class Provider:
   @ping
   def foo(self) -> None:
         print("PING")
   @ping
   def bar(self, session: Session) -> None:
         session.execute(select(1))
         print("PING SESSION")

Usage:

container.ping()

That call will find all pings declared with current and parent scopes and request them

Discuss: should we unify this technic?

Tishka17 avatar Aug 04 '24 19:08 Tishka17

Discuss: auto calculation of scope based on dependencies vs explicit scope

Tishka17 avatar Aug 04 '24 19:08 Tishka17

Discuss: auto calculation of scope based on dependencies vs explicit scope

Good discussion point, there is a boundary between code readability and developer experience. Auto calculation can be good convenient option (NestJS DI System has it), but it can be difficult to read dependency source from a lot of nested providers (modules)

Forceres avatar Aug 17 '24 21:08 Forceres

The description proposes API where you explicitly declare each ping. That's the most compatible way, certainly.

It could be nice if there was some introspection in play, too. Like if a dependency defines ping (or alike) method with certain arguments/annotations then it would be automatically registered/called.

class SomeClass:
    def ping(self):
         ...

Then you wouldn't have to explicitly define the ping each time.

Or @provide(ping=True) or @provide(ping="custom_method_name") though that would be bad for type safety.

Though, I don't think many dependencies need to define pings. Most likely just IO (database, cache, connections to other HTTP servers, etc). And in those cases you normally need to manually define/customize how the ping is done, anyway.

So, the proposed API would most likely suffice.

tuukkamustonen avatar May 23 '25 06:05 tuukkamustonen