滥用 UserDetailServiceImpl 进行非用户相关数据库查找的推荐替代方案?

发布于 2024-09-15 23:47:30 字数 517 浏览 3 评论 0原文

在我正在使用的项目中,有一个UserDetailServiceImpl 类,执行所有与用户相关的数据库查找。

问题是,我也养成了在那里进行非用户相关的数据库查找并使用 UserDetailServiceImpl 类作为默认查找服务以避免直接在控制器操作方法中进行数据库查找的坏习惯。这样做的优点是我不必为每个模型创建单独的查找服务 - 这看起来相当愚蠢和多余。但是我应该考虑是否有一种更标准的数据库查找方法以避免激怒 Spring 之神?

In the project I am working with, there is a UserDetailServiceImpl class which carries out all of the user-related database lookups.

The problem is, I have gotten into the bad habit of doing non-user-related database lookups there too and using the UserDetailServiceImpl class as a default lookup service to avoid doing database lookups directly in the controller action methods. The advantage of this is that I don't have to create a separate lookup service for each of my models - which would seem rather silly and redundant. But is there a more standard approach to database lookups that I should consider to avoid angering the Spring gods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荭秂 2024-09-22 23:47:30

我不喜欢仅使用服务来包装数据库查找,最终您的控制器中会包含所有业务逻辑。我会组织我的服务,以便每种类型的用户都有一个服务。因此,如果我有客户、数据输入人员和管理员,我会创建一个 customerService、一个 dataEntryService 和一个 adminService。每个服务都会公开该类客户所需的方法。控制器中不应该存在任何业务逻辑,它严格关注从 url、请求和会话获取输入,调用某个服务上的方法(传入这些输入),然后获取服务调用的结果并将其放在何处页面可以显示它。

I don't like using services just to wrap database lookups, you end up with all of the business logic in your controller. I would organize my services so there would be one per type of user. So if I had customers and data-entry people and admins, I would make a customerService and a dataEntryService and an adminService. Each service would expose the methods that that kind of customer needs. No business logic should be in a controller, it is strictly concerned with getting inputs from the url and request and the session, calling a method on some service (passing in those inputs), then taking the result of the service call and putting that where the page can display it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文