MVC - 它只是一个3层模型? [英] MVC - is it just a 3 tier model?

查看:181
本文介绍了MVC - 它只是一个3层模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始研究MVC和我不知道我抓住它。从我收集什么似乎是一个3层解决方案,即模式的实现对应DAL,控制器到业务逻辑层,并查看presentation层。

Just began researching mvc, and am not sure I grasp it yet. From what I gather it seems like an implementation of a 3 tier solution ie Model corresponds to DAL, Controller to business logic layer, and View as Presentation layer.

我是这里的方式关闭基地?

Am I way off base here?

推荐答案

我告诫治疗模式作为一个简单的数据访问层。这是简单化,并且它导致你把太多code到控制器层。这是更好,如果你把更多的code的模型,并进行数据库持久只有型号的内部code的一部分。我喜欢把MVC的是这样的:

I caution against treating the Model as simply a data access layer. That's oversimplifying, and it results in you putting too much code into the Controller Layer. It's better if you put more of that code in the Model, and make database persistence only a part of the Model's internal code. I like to think of MVC like this:


  • 控制器:处理输入,确定哪些Model和View实例化哪一个

  • 查看:应用程序数据的presentation

  • 型号:所有其他逻辑的应用,包括但不限于DAL

这基本上是页面控制器模式。

要想想另一种方法是这样的:假设你有到端口的Web应用程序到另一个平台,比如一个命令行应用程序或桌面GUI应用程序。你应该重用哪些部分应用程序逻辑?该控制器和视图会为您端口您的应用程序切换到另一个平台,因为投入和产出的实施将需要改变。在code,这并不需要改变应该在你的模型来实现。

Another way to think about it is this: suppose you had to port your web app to another platform, such as a command-line app or a desktop GUI app. What parts of the application logic should you reuse? The Controller and View would change as you port your app to another platform, because the implementation of both input and output would need to change. The code that doesn't need to change should be implemented in your Model.

如果你做的正确的关注点分离,然后将模型,视图和控制器将最小耦合,你可以改变一个实施而不影响其他人太多了。如果你改变了模式,你会发现自己改写控制器或查看了很多code,你可能还没有充分地分开这些层。反之亦然。

If you've done the separation of concerns right, then the Model, View, and Controller would be minimally coupled, and you could change the implementation of one without affecting the others too much. If you change the Model and you find yourself rewriting a lot of code in the Controller or the View, you probably haven't separated these layers adequately. And vice versa.

阅读Martin Fowler的贫血域模型反模式领域驱动设计快速来得到一些其他的角度。

Read about Martin Fowler's Anemic Domain Model antipattern or Domain Driven Design Quickly to get some other perspectives.

另请参见我的博客从2008年我响应的人责难活动记录模式中写道。它得到了一些很好的意见和讨论。

Also see my blog from 2008 that I wrote in response to people decrying the Active Record pattern. It got some good comments and discussion.

这篇关于MVC - 它只是一个3层模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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