Skip to content

Class-based Router Encapsulation #8991

Answered by tiangolo
Kojiro20 asked this question in Questions
Jan 10, 2021 · 10 comments · 3 replies
Discussion options

You must be logged in to vote

Hey @Kojiro20, thanks for your interest.

I see that your approach is different from fastapi-utils's class-based views, in that you are actually creating the instances of the classes directly, not expecting FastAPI to do that for you. So I guess it's probably a different use case.

About your motivations:

modularization of independently testable router definitions using dependency-injection style testing

I think you can achieve more or less the same functionality you are showing here with a function, without needing any additional complexity or changes in FastAPI:

from fastapi import FastAPI, APIRouter
from fastapi.testclient import TestClient
from pydantic import BaseModel

app = FastAPI(…

Replies: 10 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@rlimberger
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by tiangolo
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@Kludex
Comment options

@luqasz
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
9 participants
Converted from issue

This discussion was converted from issue #2625 on February 28, 2023 17:16.