MVC 与 3 层架构?
MVC 和三层架构之间的基本区别是什么?
What is the basic difference between MVC and 3-tier architecture?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
MVC 和三层架构之间的基本区别是什么?
What is the basic difference between MVC and 3-tier architecture?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
在较大的应用程序中,MVC 只是 N 层架构的表示层。模型视图和控制器只关心表示,并利用中间层用来自数据层的数据填充模型。
MVC 还可以用作整个 3 层架构,其中视图是您的表示,控制器是您的业务逻辑,模型是您的数据层(通常由 DAL 生成,例如实体框架)。
理想情况下,尽管您希望控制器又瘦又笨,但将逻辑传递给“业务组件”,这实际上将成为您的中间层。
In larger applications MVC is the presentation tier only of an N-tier architecture. The models views and controllers are only concerned with the presentation, and make use of a middle tier to populate the models with data from the data tier.
MVC can also be used as the entire 3-tier architecture where Views are your presentation, Controllers are your business logic and Models are your data layer (usually generated by a DAL such as Entity Framework).
Ideally though you want your controllers to be skinny and dumb, passing off logic to a 'business component', which would essentially become your middle tier.
3层是一种架构风格,MVC是一种设计模式。
这点也是不同的。
但我们可以在 3 层架构风格中使用 MVC 模式。
so:
表示层:来自 MVC 模式的“控制器和视图”。
业务层:来自 MVC 模式的“模型(数据)”。
数据访问层:原始数据访问层。
3-tier is a Architecture Style and MVC is a Design Pattern.
so is Different in that.
but we could using mvc pattern in 3-tier architecture style.
so:
Presentation Tier: "Controllers and Views" from MVC Pattern.
Business Tier : "Model(Data)" from MVC Pattern.
Data Access Tier : Original Data Access Tier.
在 3 层架构中,层之间的通信是双向的。在MVC中,通信是单向的;我们可以说,每一“层”都由左边的一层更新,然后又更新右边的一层——其中“左”和“右”只是说明性的。
三层架构通常部署为 3 个独立的网络节点上的 3 个独立的进程。但 MVC 被设计为作为单个进程部署在单个网络节点中。 (如桌面应用程序)
三层中的业务层通常包含不同的层,实现著名的模式,如业务委托、业务外观、业务对象、服务定位器、数据传输对象等。但 MVC 本身是一种设计模式,用于表示层。
3层的目标是将业务逻辑与客户端和数据库分离,因此提供多种客户端协议、高可扩展性、异构数据访问等。但MVC的主要目标是某一部分的实现更改不需要更改另一部分。
In the 3-tier architecture, communication between tiers is bi-directional. In the MVC 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.
3-Tier architecture usually deploy as 3 separate processes on 3 separate network nodes. But MVC is designed to deploy as a single process in a single network node. (like a desktop application)
Business tier in 3-tier usually contains different layers implementing famous patterns like business delegate, business façade, business object, service locator, data transfer object, etc. But MVC is a design pattern itself that is used in presentation tier.
The goal of 3-tier is separation of business logic from client and database, so provide multiple client protocols, high scalability, heterogeneous data access, etc. But the main goal of MVC is that implementation changes in one part do not require changes to another.
两者之间没有关系。 MVC 是一种表示层模式。整个模型-视图-控制器存在于表示层中。
模型是保存数据的对象(通常只是VO),由视图表示或从视图填充。
控制器负责获取请求(并可能填充模型)并调用服务层。然后获取另一个(或相同的)模型并将其发送回视图。
视图是显示模型的部分,并提供捕获用户输入的组件。 (它通常是Web应用程序中的模板引擎,或者桌面应用程序中的UI组件)。
当谈论 3 层(或 n 层)应用程序时,我们谈论的是整个应用程序的架构,它由表示层(整个 MVC)、服务层(业务类)和数据访问层组成。
服务层(及其背后的所有层)隐藏在 MVC 控制器后面。
There is no relationship between the two. MVC is a presentation layer pattern. The whole Model-View-Controller exists in presentation layer.
Model is object holding data (usually just VOs) which are represented by View or, populated from View.
Controller is what gets the request (and may populate the model) and calls the service layer. Then gets another (or same) model and sends it back to View.
View is what displays model, and provides components to capture user input. (It is usually a template engine in Web Applications, or UI components in a desktop application).
When talking about 3-tier (or n-tier) application we are talking about architecture of the whole application, which consists of Presentation Layer (the whole MVC), the Service Layer (Business classes), and Data Access Layer.
The Service Layer (and all behind that) are hidden behind the Controllers of MVC.
与迈克尔在回应中所说的相比,我采取了不同的方法。
控制器绝不意味着成为您的业务逻辑。对我来说,业务逻辑属于模型层。尽管视图(以及某种程度上的控制器)和表示层的一部分,模型在 MVC 应用程序中从来不是它的一部分。模型应该是 MVC 应用程序的核心和灵魂,这就是领域驱动设计的全部内容,可以在 MVC 应用程序中轻松实现。
请记住,您不必在同一项目中拥有该模型(就 ASP.NET MVC 而言)。它可以驻留在完全不同的项目中,并且仍然可以充当应用程序的模型。
充当表示层的 MVC 应用程序只能在具有多个层的大型项目中工作,但它永远不能充当 3 层中的仅表示层。分层架构,这就是提问者所问的。
因此,我们可以说 MVC 在 3 层架构的三层中创建了两层(第三层可以是数据层,它本身并不是 MVC 架构的一部分)。
谢谢。
I take a different approach compared to what Michael said in his response.
Controllers are never meant to be your business logic. For me, business logic belongs to the model layer. And though, views (and to some extent controllers) and part of the presentation layer, model is never a part of it in an MVC application. Model should be the heart and soul of an MVC application and that is what Domain Driven Design is all about which can be easily implemented in an MVC application.
Please remember that you don't have to have the model inside the same project (speaking of ASP.NET MVC). It could reside in an entirely different project and it can still act as a model to the application
An MVC application acting as a presentation layer only can work in a huge project with many tiers but it can never act as a presentation only layer in a 3 tier architecture which is what the questioner asked.
So we can say that MVC makes two (third can be the data layer which isn't really part of MVC architecture per se) out of three layers of a 3-tier architecture.
Thanks.
在我看来,3 层架构和 MVC 之间没有直接比较。两者结合使用,因此我们倾向于通过同一镜头来看待它们。从概念上讲,它们不需要一起使用。我可以拥有不使用 MVC 提供的 3 层架构。
我不会详细阐述定义部分,但简而言之:
3 层是一种软件架构方法,其中用户界面、业务流程是逻辑,数据层独立开发,通常在单独的平台上。
MVC经过一段时间,它已经从软件模式演变为架构模式,并且在当今所有主要框架中都可以看到。
IMO there is no direct comparison between 3-Tier architecture and MVC. Both are used in conjuction and hence we tend to see them through the same lense. Conceptually they need not to be used together. I could have 3-Tier architecture that does not use what MVC has to offer.
I am not elaborating the definitions part, but in nutshell:
3-Tier is a software architecture approach, in which the user interface, business process are logic, data tier developed independently, most often on separate platforms.
MVC has evolved from software pattern to architectural pattern over a period of time and is seen in all major frameworks nowadays.
什么是三层架构?
三层(层)是一种客户端-服务器架构 其中用户界面、业务流程(业务规则)以及数据存储和数据访问作为独立模块或通常在单独的平台上开发和维护。基本上有 3 层:第 1 层(表示层、GUI 层)、第 2 层(业务对象、业务逻辑层)和第 3 层(数据访问层)。这些层可以单独开发和测试。
DAL - 数据访问层(它具有连接字符串以及数据读取和执行过程)
BOL - 业务对象层(它具有查询)
UI - 用户界面(表单和代码隐藏)
更多详细信息:3 层架构
What is a 3-tier architecture?
Three-tier (layer) is a client-server architecture in which the user interface, business process (business rules) and data storage and data access are developed and maintained as independent modules or most often on separate platforms. Basically, there are 3 layers, tier 1 (presentation tier, GUI tier), tier 2 (business objects, business logic tier) and tier 3 (data access tier). These tiers can be developed and tested separately.
DAL - Data Access Layer ( it has Connectionstring and Data read & execute process)
BOL - Bussiness Object Layer ( it has Queries )
UI - User Interface ( Forms & Code Behind )
More Details : 3 Tier Archtecture
三层架构是线性的,其中客户端层实际上从未与数据层通信——所有通信都通过中间层。另一方面,MVC 更呈三角形,其中视图将更新发送到控制器并从模型接收更新,然后控制器更新模型。
(请参阅 http://en.wikipedia.org/wiki/3 上的“与 MVC 架构的比较” -tier_architecture)
A 3-tier architecture is linear where the client tier never actually communicates with the data tier--all communication passes through the middle tier. MVC on the other hand is more triangular where the view sends updates to the controller and receives updates from the model and the controller updates the model.
(See "Comparison with the MVC architecture" on http://en.wikipedia.org/wiki/3-tier_architecture)
我的经验是,MVC 只是编写糟糕的 3 层的另一个“时尚”术语,其中一些通信在业务层周围跳跃,因此客户端和/或数据层也混合了业务规则。
我讨厌像这样编写的代码 - MVC 一词的设计目的一定是为了迷惑 HR 招聘人员,让他们认为老程序员(他们将其称为“3 层”)不适合这份工作。
My experience is that MVC is a just another "fad" term for badly-written 3-tier, where some of the communication jumps around the business layers, and thus the client and/or data layer also has business rules mixed in.
I hate code written like that - The term MVC must have been designed to confuse HR recruiters into thinking older programmers (who know it as "3-tier") are not matched for the job.
数据层然后数据层->逻辑层->表示层)但是 MVC
是三角形的建筑。 (控制更新View和Model。模型
更新视图。)
Data tier then Data tier -> Logic tier -> Presentation tier) But MVC
is triangular architecture. (Control update View and Model. Model
update View.)
我不认为 MVC 会改变任何事情或帮助您构建更好或健壮的系统。三层架构是成功且足够的系统。我/你可以在其中构建非常全面和强大的系统。我们都知道一个复杂或现实生活中的网站需要在所有层之间进行大量交互。我个人认为 php 因此比 .net 更有优势。如果你要求一个书呆子、傲慢的程序员在 .net 中构建一个简单的论坛系统,他会绞尽脑汁地思考使用哪个控件来呈现它。然后他会将数据网格与一些中继器结合起来......但稍后如果你只是要求添加评论部分或图像,他会说我到底是怎么做到的?另一方面,在 php 中...您可以将 html 与服务器代码混合以轻松实现任何表示层...因此不要吹嘘架构,因为它们具有相同的优点和缺点。但问你建造了什么?
I dont think MVC will change anything or help you build better or robust system. 3 tier architecture is successful and sufficient system. I/you can build very comprehensive and robust system in it. We all know a complex or real life website takes a lot of interaction between all the layers. I personally believe php for that reason has advatage over .net. If u ask a nerdy ass arrogant programmer to build a simple forum system in .net he will scratch his head over which control to use to render it. Then he will combine data grid with some repeater... But later on if u simply ask to add comment section or image, he will be like how the heck i do it? On the other hand in php... U can mix in html with server code to achieve any presentation layer easily... So dont brag about architecture as they have equal advatages and disadvantages. But ask what have you built?