APIController
This module serves as the main public interface for FastIoC's controller system.
It re-exports the core APIController
base class and all FastIoC-compatible
route decorators (get
, post
, put
, patch
, head
, options
, delete
, trace
, websocket
)
to provide a clean and unified import path for building dependency-injected FastAPI controllers.
By using these exports, developers can easily define fully type-hinted, dependency-injected controllers that integrate seamlessly with FastAPI's routing system while leveraging FastIoC's inversion of control (IoC) container.
Example:
from fastioc.controller import APIController, get
from my_app.interfaces import IUserService, ILoggingService
class UserController(APIController):
config = {
"prefix": "/users",
"container": app_container
}
logger: ILoggingService
@get("/")
def list_users(self, service: IUserService):
self.logger.log('get users')
return service.get_all()
router = UserController.router()
app.include_router(router)
This module is intended to be the primary import surface for most applications using FastIoC's controller architecture.
APIController
APIController provides an integration layer between FastIoC's dependency injection container and FastAPI's routing system.
This class acts as a declarative controller that automatically registers all endpoint methods decorated with FastIoC route decorators (e.g., @get, @post, etc.) into a FastIoC-powered APIRouter.
Each route and its dependencies are resolved through FastIoC's dependency injection mechanism, allowing constructor injection, type-hint-based injection, and lifecycle management (e.g., singleton, transient).
The controller can define a class-level config
attribute containing APIRouterParams, which
specifies routing configuration such as prefix, tags, responses, and more.
⚠️ Important:
You must include a valid container
instance in the config
parameter to enable dependency injection
and proper controller instantiation.
Usage example:
class UserController(APIController):
config = {
"prefix": "/users",
"container": app_container
}
@get("/")
def list_users(self, service: UserService):
return service.get_all()
router = UserController.router()
app.include_router(router)
router(config: Optional[APIRouterParams] = {}) -> APIRouter
classmethod
Create a new FastIoc APIRouter instance and populate it with APIRoutes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Optional[APIRouterParams]
|
Optional configuration parameters for the APIRouter. If not provided, uses the controller's default config. NOTE: If you provide this argument, it will completely override the controller config |
{}
|
Returns:
Name | Type | Description |
---|---|---|
APIRouter |
APIRouter
|
An instance of fastioc.integrations.APIRouter with routes registered. |