Dependency Injection Contraption #1544
Unanswered
lealemseyoum
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
While auditing the code, I came across a use of DI in the
AuthenticationBackendclass that I could not rationalize. In the examples the following is shown as an intended usage for this class.Then in FastAPIUsers, there is a method
get_auth_routerwhich in turn callsget_auth_routerfrom 'routers land'. Thisget_auth_routerfrom 'routers_land' needs anAuthenticationBackendobject let's say to make routes. Let's check the 'login' route function. I am showing code snippets only relevant for my rant 😄Ok. At this point it would be reasonable to assume that
backendneedsget_strategyor it does something with it like create aStrategyobject. But then I got curious as to why there is a strategy object being passed tobackend.login, after all one would assumeAuthenticationBackendhas its own internalStrategyobject provided to it via the DI i.e. get_strategy 😕 Then I went on to see howAuthenticationBackendmakes use of or NOT thisStrategyobject. Lo and behold ...So then, this means,
AuthenticationBackendowns a dependency callable, that callers need to provide so that they are able to pass it's result i.e. aStrategyobject back to it's methods 😖 .This being a dependency for a route function, it will repeatedly create the same
Strategyobject whenever the route is accessed. But why is it being created repeatedly - isn't it intended to be static - I mean one is not expected to infer what sort of strategy object to create based on the contents of a request sent to theloginroute right?In natural language the
AuthenticationBackendclass is saying, and I quote 😆😳
Am I missing something ? 😕 I could not understand the rationale for this pattern here. I would appreciate it if some one lets me know that this is deliberate and there is an intended use-case for it.
Beta Was this translation helpful? Give feedback.
All reactions