Does app.use_controllers() support DI for controller constructors in v2.0.7? #589
Replies: 1 comment
-
|
Hi, But controllers inheritance is supported only since version 2.3.0, I recommend upgrading to this. :) Example: from blacksheep import Application
from blacksheep.server.controllers import Controller, get
class ExampleDependencyForConstructor:
pass
class ExampleDependencyForRequestHandler:
pass
class HomeController(Controller):
class_dep: ExampleDependencyForConstructor
@get("/")
def home(self, handler_dep: ExampleDependencyForRequestHandler):
assert isinstance(self.class_dep, ExampleDependencyForConstructor)
assert isinstance(handler_dep, ExampleDependencyForRequestHandler)
return self.text(f"Hello World! {id(self.class_dep)} {id(handler_dep)}")
app = Application()
# Register types
app.services.register(ExampleDependencyForConstructor)
app.services.register(ExampleDependencyForRequestHandler)curl http://127.0.0.1:44777/
Hello World! 140631871126144 140631871126448If something is not working for you, please provide an example of your code. |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
Hey all...just trying to confirm something about DI and controller registration in BlackSheep.
I'm on version 2.0.7 and running into issues with controllers that have constructor dependencies (e.g., services injected via init). I was expecting that calling app.use_controllers() would use the DI container (app.services) to resolve those dependencies, but what I’m seeing is that it tries to instantiate the controller with no args, leading to NoneType errors.
I tried passing services=app.services to app.use_controllers() and also to app.controllers_router.use_controllers(), but that throws a TypeError, so it seems unsupported in this version?
Is this expected behavior in 2.0.7? Was DI-based controller construction added in a later version? Just want to make sure I’m not missing something obvious before I either refactor the controllers or move to manual registration.
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions