DAO 设计模式和 Servlet

发布于 2024-12-21 16:12:06 字数 637 浏览 1 评论 0原文

我正在 Oracle 网站上阅读有关 DAO 设计模式的内容,并尝试在使用 JSP、Servlet、纯 java 对象和 MVC 模式的上下文中理解下图。在我的例子中,BusinessObject 是我的 servlet,而 TransferObject 是我的仅包含属性、修改器和访问器 (DTO) 的 java 类吗?

例如,如果我在 servlet(控制器)中有此代码

DTO.setFirstName(request.getParameter("firstName"));
DTO.setLastName(request.getParameter("lastName"));
DAO.save(DTO);


(来源:sun.com

I am reading about the DAO design pattern on Oracle's website and I'm trying to understand the below image in the context of using JSP's, Servlets, plain java objects, and the MVC pattern. In my case would the BusinessObject be my servlet and the TransferObject be my java class with only properties, mutators, and accessors (DTO)?

For example, if I had this code in a servlet (controller)

DTO.setFirstName(request.getParameter("firstName"));
DTO.setLastName(request.getParameter("lastName"));
DAO.save(DTO);


(source: sun.com)

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

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

发布评论

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

评论(3

鹿港巷口少年归 2024-12-28 16:12:06

几乎。在处理表示逻辑的控制器和处理数据访问逻辑的 DAO 之间,应该有一个包含业务对象的业务层。

这些业务对象的主要职责是

  • 向控制器提供业务服务。它们是一个外观
  • ,用于封装应用程序的业务逻辑
  • ,以划分事务
  • 以使用一个或多个 DAO 来获取、查找和持久化对象。

该层非常重要,因为您希望能够在单个事务中对数据库执行多个操作。并且处理这个问题不应该是网络控制器的责任。此外,相同的业务服务可以由 Web 控制器以外的其他客户端(Swing 客户端、批处理等)使用。

业务对象通常使用会话 EJB 或 Spring 服务来实现。

它们还可以

  • 通过模拟业务对象对控制器进行单元测试,
  • 通过模拟 DAO 对业务逻辑进行单元测试

Almost. Between the controller, which handles presentation logic, and the DAO, which handles Data access logic, there should be a business layer, containing the business objects.

The main responsibilities of these business objects are

  • to provide business services to the controllers. They are a facade
  • to encapsulate the business logic of the application
  • to demarcate transacations
  • to use one or several DAOs to get, find and persist objects.

This layer is very important because you want to be able to perform several operations on your database within a single transaction. And it should not be the responsibility of the web controller to handle this. Moreover, the same business services could be used by other clients than the web controllers (Swing client, batch, etc.)

Business objects are typically implemented using session EJBs, or Spring services.

They're also useful to be able to

  • unit test the controller by mocking the business objects
  • unit test the business logic by mocking the DAOs
堇色安年 2024-12-28 16:12:06

是的,BusinessObject看起来就像MVC的C(Controller)。

Yes, BusinessObject looks like a C (Controller) of the MVC.

好多鱼好多余 2024-12-28 16:12:06

整个DAO模式是MVC中模型层的一部分,其中BussinessObject提供模型接口以及模式实现的DAO和DTO对象部分。

您的 servlet 将位于控制器层(位于),而用于呈现要发送到客户端的 HTML(或其他格式)的类将位于视图层(位于)。

Web 应用程序的大小和复杂性决定了是否可以仅从一个类构建层。

为了回答您的评论,DTO(我们称之为数据持有者对象)仅由属性、getter/setter、清理和验证方法组成。它们的作用是存储/传输和业务逻辑的实现之间的关注点分离。

The whole DAO pattern is part of the Model layer in MVC, in which the BussinessObject offers the Model interface and the DAO and DTO objects part of the pattern implementation.

Your servlet would be (in) the Controller layer and the class that you use to render the HTML (or other format) to be sent to the client would be (in) the View layer.

The size and complexity of your web application determines wether the layers can be built from just one class or not.

In answer to your coment, the DTO (we call it data-holder objects) only consist of attributes, getters/setters, cleanup and validation methods. They function as a separation of concerns between storage / transfer and the implementation of bussiness logic.

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