-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring from higher layers based on Protocols #3183
Comments
Why this approach1. Why start with Upper Layers?
2. Why use
|
Example Code (It's pseudo code)@dataclass
class KeyPair:
access_key: str
secret_key: str
@dataclass
class User:
id: str
username: str
role: str
status: str
...
@dataclass
class AuthResult:
user: User
key_pair: KeyPair
...
class AuthServiceProtocol(Protocol):
async def authorize(self, domain: str, username: str, password: str) -> AuthResult:
...
async def get_role(self, access_key: str, group_id: str, user_id: str) -> str:
...
...
class Service:
AuthService: AuthServiceProtocol
@attrs.define(slots=True, auto_attribs=True, init=False)
class RootContext(BaseContext):
service: Service
# other dependency
...
####
@actxmgr
async def service_ctx(root_ctx: RootContext) -> AsyncIterator[None]:
root_ctx.service = Service.create(root_ctx)
yield
####
async def authorize(request: web.Request, params: Any) -> web.Response:
root_context: RootContext = request.app["_root.context"]
service = root_context.service.AuthService
# need to validate the parameters
domain = params["domain"]
username = params["username"]
password = params["password"]
# authorize the user
# if user is not authorized, it will raise an exception in the service
auth_result = await service.authorize(domain, username, password)
return web.json_response({
"data": {
"access_key": auth_result.keypair["access_key"],
"secret_key": auth_result.keypair["secret_key"],
"role": auth_result.user["role"],
"status": auth_result.user["status"],
},
}) |
LGTM! 💯 |
Motivation
Objective
Scope
sub_app
functionalities.Protocol
for lower layers.Protocol
, test code can be written starting from the upper layers.Solution
Handler
,Service
,Repository
to ensure each layer has a clear role.typing.Protocol
Protocol
, establish the structure of the upper layers and gradually separate the implementations of the lower layers.Roles and Responsibilities of Each Layer
To improve code maintainability and facilitate testing, the application will be structured into distinct layers, each with specific responsibilities:
1. Handler Layer
Role:
Responsibilities:
Notes:
Service
layer interfaces.2. Service Layer
Role:
Responsibilities:
Notes:
3. Repository Layer
Role:
Responsibilities:
Notes:
4. Client Layer
Role:
Responsibilities:
Notes:
I believe that starting from the upper layer by defining the interfaces for the lower layers using
Protocol
, and then progressively implementing the lower layers, would be an efficient approach for our refactoring process.The text was updated successfully, but these errors were encountered: