MVC 与 n 层架构

发布于 2024-07-15 19:51:55 字数 99 浏览 4 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

最佳男配角 2024-07-22 19:51:55

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.

御弟哥哥 2024-07-22 19:51:55

如果 3 层设计是这样的:

Client <-> Middle <-> Data

MVC 模式将是:

     Middle
     ^    |
     |    v
Client <- Data

意思是:

  • 在 3 层等效中,层之间的通信是双向并且总是通过中间层
  • 在 MVC 等价物中,通信是单向的; 我们可以说每个“层”都由左边的层更新,然后又更新右边的层 - 其中“左”和“右”仅仅是说明性

客户端将是视图中间控制器

If a 3-tier design were like this:

Client <-> Middle <-> Data

the MVC patter would be:

     Middle
     ^    |
     |    v
Client <- Data

Meaning that:

  • in the 3-tier equivalent, communication between layers is bi-directional and always passes through the Middle tier
  • in the MVC equivalent the communication is in unidirectional; we could say that each "layer" is updated by the one at the left and, in turn, updates the one at the right –where "left" and "right" are merely illustrative

P.S. Client would be the View and Middle the Controller

情场扛把子 2024-07-22 19:51:55

这就是 说一下n 层架构

乍一看,这三个层次可能
看起来和MVC(模型视图
控制器)概念; 然而,
从拓扑上看,它们是不同的。 A
三层结构的基本规则
架构永远不是客户端层
直接与数据通信
等级; 在三层模型中所有
通讯必须通过
中间件层。 从概念上讲
三层架构是线性的。
然而,MVC架构是
三角形:视图将更新发送到
控制器,控制器更新
模型和视图都会更新
直接来自模型。

This is what say about n-tier architecture

At first glance, the three tiers may
seem similar to the MVC (Model View
Controller) concept; however,
topologically they are different. A
fundamental rule in a 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. Conceptually the
three-tier architecture is linear.
However, the MVC architecture is
triangular: the View sends updates to
the Controller, the Controller updates
the Model, and the View gets updated
directly from the Model.

梦醒时光 2024-07-22 19:51:55

唯一的相似之处是这两种模式在图表中都有三个框。 从根本上讲,它们的用途完全不同。 事实上,通常不是选择使用哪种模式,而是两种模式可以和谐地一起使用。 这是两者的一个很好的比较: http:// /allthingscs.blogspot.com/2011/03/mvc-vs-3-tier-pattern.html

此图显示了如何一起使用这两种模式,其中 MVC 仅在表示/UI 层中使用:

mvc3tiercombo< /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:

mvc3tiercombo

伏妖词 2024-07-22 19:51:55

让自己休息一下。 在解决现实问题时,不要将自己限制在某些模式中。 只需记住一些一般原则,其中之一就是关注点分离

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.

霓裳挽歌倾城醉 2024-07-22 19:51:55

三层架构中的一个基本规则是客户端层永远不会直接与数据层通信; 在三层模型中,所有通信都必须通过中间件层。

这是班轮架构。 这解决了如何在用户和数据库之间传递信息的问题。 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.

↘紸啶 2024-07-22 19:51:55

@樱桃
中间件的工作方式更像是 MVC 模式中的请求处理程序或重定向器。

我想解释一下 MVC,根据我的说法,模型视图控制器是这样工作的。

  1. 客户端通过请求任何服务来发起会话。
  2. 该请求由控制器(请求处理程序、重定向器等)接收和处理。
  3. 控制器处理请求的基本信息并将其重定向到可以填充数据请求的相关模型。
  4. Model根据Controller传递的参数填写请求,并将结果返回给Controller。 (注意:在这里我想澄清一下,在真正的 MVC 架构中,数据不会直接返回到客户端,而是填充并返回到控制器。)
  5. 控制器而不是将该数据发送到视图(客户端)。
  6. 客户面前有所请求的服务。

这就是我所知道的关于 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.

  1. Client initiates the session by requesting for any service.
  2. This request is received and handled by Controller (Request handler, redirector etc)
  3. Controller process a basic info on the request and redirect it to the relevant Model which can fill up the data request.
  4. Model fill up the request according to the parameters passed by Controller and send back the results to Controller. (Note: Here i like to clear that data is not directly returned to client in true MVC architecture, rather it fills up and returned to controller.)
  5. Controller than send that data to View(Client).
  6. Client has the requested service in front of him.

That's all about MVC that i know.

殊姿 2024-07-22 19:51:55

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.

小…红帽 2024-07-22 19:51:55

除了线性之外,这里没有充分强调的另一个主要区别是,在 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.

穿透光 2024-07-22 19:51:55

结论:N层是一种架构,MVC是一种设计模式。 它们是应用于两个不同领域的相同隐喻。

Conclusion : N-tier is an architecture, MVC a design pattern. They are the same metaphore applied in two different fields.

醉殇 2024-07-22 19:51:55

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.

一瞬间的火花 2024-07-22 19:51:55

N 层架构从不直接与数据访问层通信。 在三层架构中:

  • 表示层呈现相关的 UI,
  • 业务层包含相关逻辑,
  • 然后是数据访问层。

所有数据都通过中间层进行通信。 演示<-> 商业<-> 数据。

MVC(模型-视图-控制器)架构是三角形的。

  • 视图将更新发送到控制器,
  • 控制器更新模型,
  • 然后视图直接从模型获取更新。

模型(数据)、视图(UI)、控制器(逻辑)。

N-tier architecture never communicates directly to the data access layer. In a 3-tier architecture:

  • Presentation layer presents the related UI,
  • Business layer contains related logic and
  • then the data access layer.

All the data communicates through the middle tier. Presentation <-> Business <-> Data.

MVC (Model-View-Controller) architecture is triangular.

  • The View sends updates to the controller,
  • The Controller updates the Model and
  • then the View directly gets updates from the model.

Model(Data), View(UI), Controller(Logic).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文