DAO 设计模式和 Servlet
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几乎。在处理表示逻辑的控制器和处理数据访问逻辑的 DAO 之间,应该有一个包含业务对象的业务层。
这些业务对象的主要职责是
该层非常重要,因为您希望能够在单个事务中对数据库执行多个操作。并且处理这个问题不应该是网络控制器的责任。此外,相同的业务服务可以由 Web 控制器以外的其他客户端(Swing 客户端、批处理等)使用。
业务对象通常使用会话 EJB 或 Spring 服务来实现。
它们还可以
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
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
是的,BusinessObject看起来就像MVC的C(Controller)。
Yes, BusinessObject looks like a C (Controller) of the MVC.
整个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.