Hi All 👋

I’m excited to share that the latest version of Robyn now includes dependency injections but with a unique syntax.

For those who might not be familiar, Robyn is a fast, asynchronous Python backend web framework that operates with a Rust runtime, combining the best of both worlds for efficient and robust web development.

Here’s a sample example to illustrate the new feature:

from robyn import Robyn, ALLOW\_CORS

app = Robyn(__file__)

GLOBAL_DEPENDENCY = "GLOBAL DEPENDENCY"
ROUTER_DEPENDENCY = "ROUTER DEPENDENCY"

app.inject(ROUTER_DEPENDENCY=ROUTER_DEPENDENCY)
app.inject_global(GLOBAL_DEPENDENCY=GLOBAL_DEPENDENCY)

@app.get("/sync/global_di")
def sync_global_di(request, router_dependencies, global_dependencies):
   return global_dependencies["GLOBAL_DEPENDENCY"]  

It was one of the most requested features, and I’m curious to hear what the community thinks about this new approach. Any feedback or thoughts are greatly appreciated!

And as usual, the project can be found at: https://github.com/sparckles/Robyn

  • DanCardin@fediverser.communick.devB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    I think the tact that fastapi takes, i.e. foo: Annotated[x, Depends(dependency)] , is honestly the best mechanism I’ve seen (so much so that I’ve utilized something similar, in my own cli library)

    It keeps your function arguments agnostic to the dependency, retain typing ability, “easily” composes into nested dependencies, various other things.

    By contrast, I’m not especially a fan of this because the function argument name is hardcoded, the object presumably has no ability to type the dependencies, it’s unclear how nested dependencies would work, and it’s also unclear if/whether dependencies are resolved eagerly or on access