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

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

问题描述

存在pretty不熟悉设计模式和架构,我无法解释给别人我最新的应用程序究竟是如何设计的。我想这是一个纯粹的n层,纯MVC和n层与MVC在presentation层之间切换。目前,我认为后者是正确的,但我想从有经验的开发人员的想法。

如何工作的:


  1. 浏览器发送HTTP请求到Tomcat。通过映射web.xml中一个servlet请求(我称之为控制器)

  2. 控制器实例化一个或多个业务对象,并调用这些方法,即 customerBO.getById(12)这将再次调用一个或多个前执行业务逻辑/验证DAO方法,即 customerDAO.getById(12)。本博返回CustomerVO的名单给控制器

  3. 控制器prepares为视图(JSP)属性(了request.setAttribute(顾客,顾客); ),并选择一个.jsp文件使用这反过来将遍历列表,并呈现XHTML返回给浏览器。

的结构(我的建议/理解)

presentation层:目前使用的是什么,我认为是一个MVC Web的实现:(即CustomerForm)的servlet(控制器),JSP(视图)和我自己的实施OO XHTML形式就出在这里。应该可以通过切换出这个presentation层且无需改变在下面的层的任何使用一个Swing / JavaFX的/ Flex的图形用户界面。

逻辑层:分为两层,顶部的Business Objects(BO)。负责业务逻辑,但我还没有发现太多摆在这里,除了输入验证因为应用程序主要是由简单的CRUD操作......在许多情况下,这些方法只是调用一个方法与DAO层上的名称相同。

DAO类与CRUD方法,这再次接触下面的数据层。也有一个convertToVO(ResultSet的RES),它从数据库和(名单)价值的物品进行ORM的方法。所有的方法都值对象作为输入,即customerDAO->保存(选民),并成功和零失败返回更新选民。

数据层:在底部的数据存储在一个数据库或XML文件。我没有codeD什么的,除了一些MySQL的存储过程和触发器。

问题(除了一个在标题):


  1. 在MVC在M。我不知道,如果当模特是我的列表可以调用这个n层的MVC / VO在逻辑层从业务对象返回?是必需的车型驻留在presentation层内当控制器/视图是在这里吗?并可以在presentation层的表单模板被称为模式?如果是这样;都形式和列表从BO到被认为是M在MVC?

  2. 从我的理解,在MVC视图应该观察变化的模型和更新,但是这是不可能在Web应用程序,其中的观点是呈现的XHTML页面?这又使我的问题:是MVC实现不同的Web应用程序与普通桌面应用程序

  3. 我不使用Front Controller模式时,所有的HTTP请求在web.xml中正确的是明确映射?要使用前端控制器我需要将所有请求转发到一个标准的servlet /控制器,反过来演算值为请求并调用另一个控制器?

  4. 业务层在我的应用程序感到有点无用。那你通常把这个层/对象?如果一个始终有一个业务层?我知道它应该包含业务逻辑,不过这是什么是什么呢?我只是执行输入验证和实例化一个或一个以上的DAO,并呼吁他们适当的方法...

我知道有MVC框架如Struts的Java,但因为这是我的第一个Java Web的应用程序,我试图让事情如何工作有了更深的了解。展望回想起来,我希望你能回答一些我偶然发现了问题。


解决方案

  

我不知道,如果当模特是我的列表可以调用这个n层的MVC / VO的从逻辑层业务对象返回


这些都是非常不错的机型。我也考虑在Struts中的ActionForm中是模型。的ActionForm是什么Struts使用重新present /型号HTML表单。


  

MVC中视图应该观察变化的模型和更新,但是,这是不可能的网络的应用程序


是的,这是一个有争议的问题,你是否能有真正的MVC与web的应用程序。


  

如果一个始终有一个业务层?


这取决于应用的类型。有些应用是数据库驱动的,本质上是对数据库中的用户界面。在这种情况下,有需要非常小的业务逻辑。


  

数据层:


该存储过程是不是真正的数据层code的一部分。你应该创建这是由业务对象称为数据访问对象(DAO)。在DAO中调用存储过程。另外,在DAO接口应该给没有暗示对业务对象,以在数据被存储,不管是从一些Web服务的数据库或文件系统或

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.

How it works:

  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.

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.

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.

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.

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.

Questions (besides the one in the title):

  1. The M in 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? Are the models required to reside within the presentation layer when the controller/view is here? And can the form templates in the presentation layer be called models? If so; are both the forms and lists from BO to be considered as the M in MVC?
  2. From my understanding, in MVC the view is supposed to observe the model and update on change, but this isn't possible in a web-application where the view is a rendered XHTML page? This in turn leads me to the question: is MVC implemented differently for web-applications vs. regular desktop applications?
  3. I'm not using a Front Controller pattern when all HTTP requests are explicitly mapped in web.xml right? To use Front Controller I need to forward all requests to a standard servlet/controller that in turn evalutes the request and calls another controller?
  4. The Business Layer felt a little "useless" in my application. What do you normally put in this layer/objects? Should one always have a business layer? I know it should contain "business logic", but what is this exactly? I just perform input validation and instantiate one or more DAOs and calls the appropriate methods on them...

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.

解决方案

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

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.

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

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?

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.

Data Tier:

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.

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

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