MVC 与 n 层架构
我想知道 MVC(一种架构模式)和应用程序的 n 层架构之间到底有什么区别。 我搜索过但找不到简单的解释。 可能我对 MVC 概念有点天真,所以如果有人能解释其中的区别那就太好了。
I was wondering what exactly is the difference between MVC(which is an architectural pattern) and an n-tier architecture for an application. I searched for it but couldn't find a simple explanation. May be I am a bit naive on MVC concepts, so if anyone can explain the difference then it would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
N 层架构通常由网络分隔各层。 IE 表示层位于某些 Web 服务器上,然后通过网络与后端应用程序服务器进行业务逻辑对话,然后再次通过网络与数据库服务器进行对话,也许应用程序服务器还调用一些远程服务(例如用于付款处理的 Authorize.net)。
MVC 是一种编程设计模式,其中不同部分的代码负责表示某些应用程序中的模型、视图和控制器。 这两件事是相关的,因为例如模型层可能有一个调用数据库来存储和检索数据的内部实现。 控制器可以驻留在网络服务器上,并远程调用应用程序服务器来检索数据。 MVC 抽象了应用程序架构如何实现的细节。
N 层仅指实现的物理结构。 这两者有时会混淆,因为 MVC 设计通常使用 N 层架构来实现。
N-tier architecture usually has each layer separated by the network. I.E. the presentation layer is on some web servers, then that talks to backend app servers over the network for business logic, then that talks to a database server, again over the network, and maybe the app server also calls out to some remote services (say Authorize.net for payment processing).
MVC is a programming design pattern where different portions of code are responsible for representing the Model, View, and controller in some application. These two things are related because, for instance the Model layer may have an internal implementation that calls a database for storing and retrieving data. The controller may reside on the webserver, and remotely call appservers to retrieve data. MVC abstracts away the details of how the architecture of an app is implemented.
N-tier just refers to the physical structure of an implementation. These two are sometimes confused because an MVC design is often implemented using an N-tier architecture.
如果 3 层设计是这样的:
MVC 模式将是:
意思是:
的 客户端将是视图,中间是控制器
If a 3-tier design were like this:
the MVC patter would be:
Meaning that:
P.S. Client would be the View and Middle the Controller
这就是 说一下n 层架构
This is what say about n-tier architecture
唯一的相似之处是这两种模式在图表中都有三个框。 从根本上讲,它们的用途完全不同。 事实上,通常不是选择使用哪种模式,而是两种模式可以和谐地一起使用。 这是两者的一个很好的比较: http:// /allthingscs.blogspot.com/2011/03/mvc-vs-3-tier-pattern.html
此图显示了如何一起使用这两种模式,其中 MVC 仅在表示/UI 层中使用:
< /a>
The only similarity is that the two patterns have three boxes in their diagrams. Fundamentally they are completely different in their uses. If fact, it is not usually a choice between which pattern to use, but both patterns can be use together harmoniously. Here is a good comparison of the two: http://allthingscs.blogspot.com/2011/03/mvc-vs-3-tier-pattern.html
This diagram shows how both patterns can be used together, with MVC used solely within the Presentation/UI Tier:
让自己休息一下。 在解决现实问题时,不要将自己限制在某些模式中。 只需记住一些一般原则,其中之一就是关注点分离。
Give yourself a break. And don't restrict yourself to certain patterns when solving real-world problems. Just remember some general principles, one of which is SEPARATION OF CONCERNS.
三层架构中的一个基本规则是客户端层永远不会直接与数据层通信; 在三层模型中,所有通信都必须通过中间件层。
这是班轮架构。 这解决了如何在用户和数据库之间传递信息的问题。 MVC 是一个三角架构:视图将更新发送到控制器,控制器更新模型,视图直接从模型更新。 这解决了用户界面如何管理屏幕上的组件的问题。
A fundamental rule in three-tier architecture is the client tier never communicates directly with the data tier; in a three-tier model all communication must pass through the middleware tier.
It’s liner architecture. This addresses the question of how to pass information between a user and a database. Where as MVC is a triangular architecture: the View sends updates to the Controller, the Controller updates the Model, and the View gets updated directly from the Model. This addresses questions of how a user interface manages the components on the screen.
@樱桃
中间件的工作方式更像是 MVC 模式中的请求处理程序或重定向器。
我想解释一下 MVC,根据我的说法,模型视图控制器是这样工作的。
这就是我所知道的关于 MVC 的全部内容。
@Cherry
Middle ware works more like a request handler or redirector in MVC Pattern.
I would like to explain a bit about MVC, According to me Model View Controller works like this.
That's all about MVC that i know.
N 层架构最好使用部署图来定义。
MVC 架构最好使用序列图来定义。
两者不同且不相关,您可以将两种架构组合在一起。 许多公司已经采取措施创建 N 层架构,不仅是为了部署和可扩展性,也是为了代码重用。
例如,您的业务实体对象可能需要由桌面应用程序、为客户端公开的 Web 服务、Web 应用程序或移动应用程序使用。 仅仅使用 MVC 方法根本无法帮助您重用任何东西。
An N-Tier architecture is best defined using a Deployment Diagram.
An MVC architecture is best defined using a Sequence Diagram.
The 2 are not the same and are not related and you can combine the two architectures together. A lot of companies have taken the steps to create N Tier'd architecture for not only deployment and scalability, but for code reuse as well.
For example, your Business Entity objects may need to be consumed by a desktop app, a web service exposed for a client, a web app, or a mobile app. Simply using an MVC approach will not help you reuse anything at all.
除了线性之外,这里没有充分强调的另一个主要区别是,在 N 层模型中,N 不一定是 3 层! 它通常被实现为三层(表示、应用程序、数据),中间层有两个子层(业务逻辑和数据访问)。 此外,MVC 中的模型可以包含用于数据操作的数据和业务逻辑,而这些将位于 n 层中的单独层中。
Besides being linear, another major difference that was not emphasized enough here is that in the N-tier model, N is not necessarily 3-tiers! It is most often implemented as three tiers (presentation, app, data) with the middle layer having two sub-tiers (business logic and data access). Also, the model in MVC can contain both data and business logic for data manipulation, whereas these would be in separate tiers in n-tier.
结论:N层是一种架构,MVC是一种设计模式。 它们是应用于两个不同领域的相同隐喻。
Conclusion : N-tier is an architecture, MVC a design pattern. They are the same metaphore applied in two different fields.
Jerry:这是一个简单的例子来说明两者的关系:
第 1 层 - 由通过某种网络服务或类似服务与第 2 层通信的模型组成,控制器用于处理输入验证、计算和其他事情与观点相关。 当然,它包含视图本身 - 可以是桌面应用程序中的 GUI,也可以是 Web 应用程序中的 Web 界面。
第 2 层 - 包含某种服务或其他从第 1 层接收消息的方式。不/不应该了解第 1 层,因此只能应答来自上面的呼叫 - 切勿自行询问。 还包含所有业务逻辑。
第 3 层 - 包含域模型、数据库的对象表示以及用于通信和更新数据库条目的所有逻辑。
Jerry: Here's a simple example of how the two are related:
Tier 1 - Consists of Models that communicate with Tier 2 through some sort of network service or similar, controllers to handle input validation, calculations and other things relevant for the views. And it contains the views themselves, ofcourse - which can be the GUI in a desktop-app, or the web-interface in a web-app.
Tier 2 - Contains some sort of service or other way of recieving messages from Tier 1. Does not/should not know about Tier 1, so can only answer to calls from above - never ask for things by itself. Also contains all business-logic.
Tier 3 - Contains the domain model, object representation of the database and all logic to communicate and update database-entries.
N 层架构从不直接与数据访问层通信。 在三层架构中:
所有数据都通过中间层进行通信。 演示<-> 商业<-> 数据。
MVC(模型-视图-控制器)架构是三角形的。
模型(数据)、视图(UI)、控制器(逻辑)。
N-tier architecture never communicates directly to the data access layer. In a 3-tier architecture:
All the data communicates through the middle tier. Presentation <-> Business <-> Data.
MVC (Model-View-Controller) architecture is triangular.
Model(Data), View(UI), Controller(Logic).