我是否正确地使用 MVC 实现了 n 层应用程序? [英] Have I implemented a n-tier application with MVC correctly?

查看:32
本文介绍了我是否正确地使用 MVC 实现了 n 层应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于对设计模式和架构非常不熟悉,我无法向其他人准确解释我最新的应用程序是如何设计的.我在认为它是纯 n 层、纯 MVC 和 n 层与表示层中的 MVC 之间切换.目前我认为后者是正确的,但我希望有更多经验丰富的开发人员的想法.

Being pretty unfamiliar with design patterns and architecture, I'm having trouble explaining to others exactly how my latest application is designed. I've switched between thinking it's a pure n-tier, pure MVC and n-tier with MVC in the presentation layer. Currently I think the latter is correct, but I want thoughts from more experienced developers.

  1. 浏览器向 Tomcat 发送 HTTP 请求.通过 web.xml 将请求映射到 servlet(我称之为控制器)
  2. 控制器实例化一个或多个业务对象并调用这些对象的方法,即 customerBO.getById(12) 它将在调用一个或多个 DAO 方法之前再次执行业务逻辑/验证,即 customerDAO.getById(12).BO 将 CustomerVO 的列表返回给控制器
  3. 控制器为视图 (JSP) 准备属性 (request.setAttribute("customers",customers);) 并选择要使用的 .jsp 文件,该文件依次迭代列表并呈现XHTML 返回浏览器.
  1. Browser sends HTTP request to Tomcat. Maps the request via web.xml to a servlet (which I call controller)
  2. The controller instantiates one or more business object and calls methods on these, i.e. customerBO.getById(12) which again will perform business logic/validation before calling one or more DAO methods, i.e. customerDAO.getById(12). The BO returns a list of CustomerVO's to the controller
  3. The controller prepares attributes for the view (JSP) (request.setAttribute("customers", customers);) and chooses a .jsp file to use which in turn will iterate the list and render XHTML back to the browser.

结构(我的建议/理解)

展示层:目前使用的是我认为的 MVC Web 实现:servlet(控制器)、jsp(视图)和我自己的 OO XHTML 表单实现(即 CustomerForm)就在这里.应该可以通过切换此表示层来使用 Swing/JavaFX/Flex GUI,而无需更改下面层上的任何内容.

Structure (my proposal/understanding)

Presentation tier: currently using what I think is a MVC web-implementation: servlets (controllers), jsp (views) and my own implementation of OO XHTML forms (ie. CustomerForm) lies here. It should be possible to use a Swing/JavaFX/Flex GUI by switching out this presentation layer and without the need to change anything on the layers below.

逻辑层:分为两层,业务对象 (BO) 位于顶部.负责业务逻辑,但除了输入验证之外,我还没有找到太多可放在此处的内容,因为应用程序主要由简单的 CRUD 操作组成......在许多情况下,这些方法只是调用 DAO 层上的同名方法.

Logic tier: Divided into two layers, with Business Objects (BO) on top. Responsible for business logic, but I haven't found much to put in here besides input validation since the application mostly consists of simple CRUD actions... In many cases the methods just call a method with the same name on the DAO layer.

带有 CRUD 方法的 DAO 类,它再次联系下面的数据层.还有一个 convertToVO(ResultSet res) 方法,可以从数据库执行 ORM 并转换为(列表)值对象.所有方法都将值对象作为输入,即 customerDAO->save(voter) 并在成功时返回更新的投票者,失败时返回 null.

DAO classes with CRUD methods, which again contacts the data tier below. Also has a convertToVO(ResultSet res) methods which perform ORM from the database and to (lists of) value objects. All methods take value objects as input, i.e. customerDAO->save(voter) and return the updated voter on success and null on failure.

数据层:在底层,数据存储在数据库或 XML 文件中.我没有在这里编码"任何东西,除了一些 MySQL 存储过程和触发器.

Data tier: At the bottom data is stored in a database or as XML files. I have not "coded" anything here, except some MySQL stored procedures and triggers.

  1. MVC 中的 M.当模型是从逻辑层中的业务对象返回的列表/VO 时,我不确定是否可以调用此 n 层 MVC?当控制器/视图在这里时,模型是否需要驻留在表示层中?而表现层中的表单模板可以称为模型吗?如果是这样的话;来自 BO 的表单和列表都被视为 MVC 中的 M 吗?
  2. 根据我的理解,在 MVC 中,视图应该观察模型并在更改时更新,但这在视图是呈现的 XHTML 页面的 Web 应用程序中是不可能的?这反过来又让我想到了一个问题:对于 Web 应用程序和常规桌面应用程序,MVC 的实现方式是否不同?
  3. 当所有 HTTP 请求都显式映射到 web.xml 时,我没有使用前端控制器模式,对吗?要使用前端控制器,我需要将所有请求转发到一个标准的 servlet/控制器,该 servlet/控制器依次评估请求并调用另一个控制器?
  4. 业务层在我的应用程序中感觉有点无用".你通常在这个层/对象中放什么?应该总是有一个业务层吗?我知道它应该包含业务逻辑",但这究竟是什么?我只是执行输入验证并实例化一个或多个 DAO 并对其调用适当的方法......

我意识到有 MVC 框架,例如 Struts for Java,但自从这是我的第一个 Java Web 应用程序以来,我试图更深入地了解事物的工作原理.回想起来,我希望你能回答我偶然发现的一些问题.

I realize there is MVC frameworks such as Struts for Java, but since this my first Java web-application I tried to get a deeper understanding of how things work. Looking in retrospect I hope you can answer some of the questions I stumbled upon.

推荐答案

当模型是从逻辑层中的业务对象返回的列表/VO 时,我不确定是否可以调用此 n 层 MVC

I'm not sure if I can call this n-tier MVC when the models are lists/VO's returned from business objects in the logic tier

那些是非常好的模型.我还认为 Struts 中的 ActionForms 是模型.ActionForms 是 Struts 用来表示/建模 HTML 表单的东西.

Those are perfectly good models. I also consider the ActionForms in Struts to be models. ActionForms are what Struts uses to represent/model HTML forms.

在 MVC 中,视图应该观察模型并在更改时更新,但这在 Web 应用程序中是不可能的

in MVC the view is supposed to observe the model and update on change, but this isn't possible in a web-application

是的,关于您是否可以拥有带有 Web 应用程序的真正 MVC,这是一个有争议的问题.

Yep, and that is a matter of debate as to whether you can have true MVC with web-applications.

应该总是有一个业务层吗?

Should one always have a business layer?

这取决于应用程序的类型.一些应用程序是数据库驱动的,本质上是数据库的 UI.在这种情况下,只需要很少的业务逻辑.

It depends on the type of application. Some applications are database-driven, and are essentially a UI for the database. In that case, there's very little business logic required.

数据层:

存储过程实际上并不是数据层代码的一部分.您应该创建由业务对象调用的数据访问对象 (DAO).DAO 调用存储过程.此外,DAO 接口不应向业务对象提供有关数据存储位置的提示,无论是数据库、文件系统还是来自某些 Web 服务.

The stored procedures aren't really part of the data tier code. You should be creating data access objects (DAOs) which are called by the business objects. The DAOs call the stored procedures. Further, the DAO interfaces should give no hint to the business objects as to where the data is stored, whether that be a database or file system or from some web service.

这篇关于我是否正确地使用 MVC 实现了 n 层应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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