-
I have several controllers with some common features so I want them to convert them into subclasses and move the common functionality to a parent class. A child controller would look like this:
so the parent class would look like this:
using dependencies = {"_db_handler": Provide(db_handler)} does not work. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Controllers and dependencies don't work like that. Dependencies are only injected into other dependencies or route handlers. Controller instances are also created once, not for each request. Is there a reason you can't / don't want to use normal dependency injection here? class CategoryController(BaseEndpoint):
_path = f"{BASE_PATH}category"
@get(path=_path
+ "/category/{category:str}"
)
async def get_dut(
self, category: int, db_handler
) -> list:
return await db_handler.get_categories(category)
) |
Beta Was this translation helpful? Give feedback.
@l3t2st1r I'm sorry if I didn't understand your request well (I'm also not a Litestar expert and I don't know if this is a good approach), but you can use
super()
to use the argument of the parent controller in the child controllers. Something like thisExample code