Skip to content
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

“规约1.4.9定义DO/DTO/VO等POJO类时不要设定任何属性默认值”不合理 #360

Closed
hellozjf opened this issue Jul 18, 2018 · 4 comments

Comments

@hellozjf
Copy link

假设我有一个VO对象,它有一个属性为private List<OrderDetail> orderDetailList;,如果我不对它赋初值,那么返回给前端,前端会收到orderDetailList: null。但是我又和前端约定好,即使orderDetailList里面没有数据也至少要返回一个空数组。要满足这个需求,我就不得不为这个属性赋一个初值private List<OrderDetail> orderDetailList = new ArrayList<>();。因此我觉得规约1.4.9不太合理,应去掉。

@kerie
Copy link
Contributor

kerie commented Jul 24, 2018

@yangguanbao 帮忙看一下

@l-i-h-e
Copy link

l-i-h-e commented Sep 27, 2018

我觉得你提的这个是不合理的,无逻辑的模型自带了初始化逻辑,本身就说不通的,这和在模型中声明int ..等基数据类型并无本质的区别。这个问题只是只要配置一下json的序列化就好了。

@gujin520
Copy link
Contributor

。但是我又和前端约定好,即使orderDetailList里面没有数据也至少要返回一个空数组。

—— 约好,可以重新约定一下。

另外,默认值不予设定,是因为默认值往往在数据库更新,或者数据传输时没有赋值,导致保存到数据库或者数据接收方取到默认值。

@BestRoach
Copy link

我觉得你提的这个是不合理的,无逻辑的模型自带了初始化逻辑,本身就说不通的,这和在模型中声明int ..等基数据类型并无本质的区别。这个问题只是只要配置一下json的序列化就好了。

同意你的说法,既然是和前端约定好的,如果是JSON序列化的,就可以在JSON序列化方案中去配置。如果是其他方案的话,就应该在返回前set一个你和前端约定好的值进去。这样直接写的初始值会给排错造成难度。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants