在spring mvc控制器中使用Services和DAO [英] Using Services and DAOs in spring mvc controller

查看:156
本文介绍了在spring mvc控制器中使用Services和DAO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个主要构成后端/数据库数据的CRUD操作的Web应用程序。有些情况下,我必须编写业务逻辑(我相信,随着我们深入开发,我们将拥有更多的业务逻辑。目前对于每个UI屏幕我正在创建我创建一个模型类,服务类,DAO类,控制器(它本质上是servlet)和一堆jsp页面。在大多数情况下,服务类只是调用DAO中的方法来传递模型对象。本质上我们使用模型类来从UI屏幕映射数据。因此,当提交表单时,控制器将填充模型对象。我已经开始使用服务类来保持从Web层到DAO层的分离层。但有时我觉得服务类只是添加了不必要的API调用级别,我会认为我可以将DAO注入到控制器中并更快地完成任务。我仅在需要执行其他业务逻辑时才使用服务类。如果您需要设计一个应用程序,您应该考虑使用controller-> DAO vs controller-> Service-> DAO控制流程。

解决方案

DAO更细化并处理一个特定的实体。服务提供宏观层面的功能,最终可以使用多个DAO。通常,服务用于定义事务边界以获得原子性。换句话说,如果最终使用多个DAO更新多个表,则在服务中定义事务边界将有助于提交或回滚对DB完成的所有更改。



您的设计,因为您主要为各种实体做CRUD,似乎服务并没有增加价值。然而,将网络前端看作是更新数据的一种方式。服务的使用将允许您将与网络服务相同的功能再次暴露给其他形式的客户端,如第三方集成商等。



总之,您的设计似乎符合常规做法。如果您觉得可以将多个服务组合成一个基于某些常见主题的服务,以减少代码开销,那么您应该继续执行。最后,最终的目标是创建可维护的代码,当需要时,没有人会害怕改变。


I'm building a web application that primarily constitutes of CRUD operations of data from back end/database. There are instances where in I have to write business logic(I'm sure we will have more business logic built as we go deeper in to development). Currently for each UI screen I'm creating I create a model class,Service class, DAO class, a controller(it's servlet essentially) and bunch of jsp pages. In most cases the service class just calls the methods from DAO to pass in model objects. Essentially we use model classes to map data from UI screens. Hence the controller will have the model objects populated when a form is submitted. I have started using service classes to keep a separation layer from web layer to DAO layer. But at times I feel that the service class is just adding unnecessary level of API calls, I would think that I could just inject the DAO in to Controller and complete the task faster. I want to use the service class only when there is additional business logic to be performed. If you have to design an application what factors do you consider using controller->DAO vs controller->Service->DAO control flow?

解决方案

DAOs are more granular and deal with one specific entity. Services provide macro level functionalities and can end up using more than one DAO. Typically, Services are used for defining transaction boundaries to gain atomicity. In other words, if you end up updating multiple tables using multiple DAOs, defining transaction boundary at service will help in either committing or rollbacking all the changes done to DB.

In your design, since you are primarily doing CRUD for various entities, it may seem that services are not adding much value. However, think of web-based front end as one way of updating data. Usage of services will allow you to expose same capabilities as a web-service later to other forms of client like third party integrators, etc.

So, in summary, your design seems to be in line with conventional practices. If you feel that you can combine multiple services into one based on some common theme such that it can reduce the overhead of code, then, you should go ahead and do it. At the end of day, ultimate goal is to create maintainable code which no one is afraid to change when need arises.

这篇关于在spring mvc控制器中使用Services和DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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