-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
[Question] parameters validation best practice (best practice about parameter validation) #3430
Comments
Currently, PGV (proto-gen-validate) is quoted, which is very convenient for verifying some static types and data restrictions. However, when encountering some logic that requires querying the library for verification, which layer should it be placed at? For example: the caller passes a user ID, and I need to check the database first to determine whether the user exists. If the user exists, a
I would like to ask if you have any better implementation plan |
这个问题我以前问过,实际上就是service层的事情,不应该放在biz层去。以前问毛大这个问题,回复如下:
|
I have asked this question before. It is actually a matter of the service layer and should not be placed on the biz layer. I asked Mao Da this question before, and his reply was as follows:
|
目前引用了PGV(proto-gen-validate),对于一些静态类型和数据限制的校验很方便,但是遇到一些需要查询库才能做校验的逻辑,应该放到哪一层比较合适呢?
比如:调用方传了一个用户ID过来,我需要先查库确定这个用户是否存在,如果用户存在的话,会查询出一个
User DO
,User DO
再贯穿整个usecase
逻辑,但是现在遇到两个问题:如果把校验逻辑放在
usecase
层,那么有两个方案:a. 在
usecase
逻辑中做参数校验:这样会有一个问题,
userDO
是在Logic
中新创建的,会导致在单测时不得不mock掉userRepo
, 鉴于go的mock不是特别好用,我觉得有点麻烦b. 在
usecase
加一个RetrieveUser
方法,把检验逻辑前置到service
层:这样做倒是简化了
Logic
的单测逻辑, 只需要构造Params
即可,但是usecase
会变得很膨胀把检验放到
service
层:参考
1.b
,在service
层依赖userRepo
:我个人是比较倾向于这种方案,这样做保证了几点:
service
和usecase
层依赖的是同一个userRepo
,不会产生逻辑偏差;usecase.Logic
的单测变得简单想问下大家还有没有更好的实现方案
The text was updated successfully, but these errors were encountered: