Spring的验证者应该访问数据库吗? [英] Should validators in spring access the database?

查看:92
本文介绍了Spring的验证者应该访问数据库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定使验证器根据数据库状态验证命令是否是一个好的设计决策.例如,如果除了检查电子邮件和用户名是否为空等之外,我还需要验证User Bean.我还需要拒绝已使用的值.这种逻辑应该进入验证器还是服务对象?

I'm not really sure if it's a good design decision to make the validators validate commands based on the state of the database. For example if I need to validate a User bean besides checking if the email and username are empty etc. I also need to reject values if they are already used. Should this kind of logic go in the validators or the service objects?

推荐答案

好吧,您的验证器只是spring bean,因此可以将它们与处理数据访问的服务对象一起注入.您可以让验证器从数据库中获取数据,而不会影响设计.

Well your validators are just spring beans, right, so they can be injected with the service objects that handle data access. You can have your validators get data from the database without compromising the design.

这篇关于Spring的验证者应该访问数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆