在Model-View-Controller原理中,什么是Frontend,什么是Backend? [英] In the Model-View-Controller principle, what is the Frontend and what is the Backend?

查看:14
本文介绍了在Model-View-Controller原理中,什么是Frontend,什么是Backend?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

维基百科说:

MVC 为数据库、用户和数据处理组件.软件系统的分离前后端简化开发,分离维护.

MVC provides front and back ends for the database, the user, and the data processing components. The separation of software systems into front and back ends simplifies development and separates maintenance.

我仍然没有看到模型-视图-控制器原则与前端和后端的想法之间的联系.可以将访问数据库的模型视为后端,将视图视为前端吗?

I still don't see the link between the model-view-controller principle and the idea of front- and backend. Can the Model with its access to the database be seen as the Backend and the View as the frontend?

推荐答案

好的.. 首先是条款:

OK.. first the terms:

  • 前端 - 是对用户可见的部分:HTML、CSS、客户端 Javascript.这一切基本上都是前端".在桌面应用程序前端将是 GUI.
  • 后端 - 是隐形部分.在 web 应用程序中,你的 java、ruby、php 或任何其他服务器端代码.它可以被解释或编译,因为它如何"工作对它是什么"没有影响.
  • Frontend - are the parts, which are visible to users: HTML, CSS, client-side Javascript. It all is basically "frontend". In a desktop application frontend would be the GUI.
  • Backend - is the invisible part. In web applications that is your java, ruby, php or any other serverside code. It can be either interpreted or compiled, because "how" it works has no impact on "what" it is.

如果您阅读了 GUI 架构 并研究了 MVC 模式一般来说,您会明白MVC 不是后端和前端的分离.尤其是当涉及到我们用于 Web 应用程序的 MVC 启发模式时.

If you read GUI Architectures and research the MVC pattern in general, you will understand that MVC is not about separation of backend and frontend. Especially when it comes to MVC-inspired patterns, that we use for web applications.

MVC 和相关模式的目标是将表示与域业务逻辑分开.

The goal of MVC and related patterns is to separate presentation from domain business logic.

以下是MVC部分的基本职责:

Here are the basic responsibilities of MVC parts:

  • 模型 - 业务逻辑
  • 视图 - 呈现逻辑
  • 控制器 - 改变模型和视图的状态(基于用户输入)
  • Model - business logic
  • View - presentation logic
  • Controller - changing state of model and view (based on user input)

举个例子:

  • Twitter 的替代客户端应用程序
  • 使用 OAuth 进行身份验证
  • 用户可以输入不同的搜索词
  • 通过 Twitter 的 REST API 获取信息
  • 验证数据
  • 解析 JSON 响应
  • 操纵 DOM 来呈现信息

这一切都可以通过客户端 JavaScript 来完成.你可以让MVC三元组运行前端"同时,提供REST API的后端"是一个类似MVC的结构.只是这次 View 生成的是 JSON 响应,而不是 HTML.

This all can be done with client-side JavaScript. You can have MVC triad running "frontend"! At the same time, the "backend" which provides REST API is an MVC-like structure. Only this time the View is generating JSON responses, instead of HTML.

*结论:您可以在后端和前端使用 MVC 模式.**

*Conclusion: You can use MVC pattern both on backend and frontend.**

后脚本

由于您一直在使用 Rails 构建一些应用程序,因此您对 MVC 的理解可能会被扭曲.我之所以这么说是因为,由于 RoR 最初是作为原型框架制作的(注意所有脚手架和其他用于生成一次性代码的功能),而且由于它的起源,Rails 实际上是在实现MVP 的一个非常贫乏的版本.

Post Scriptum

Since you have been building some applications with Rails, your understanding of MVC might be a but distorted. The reason I say this is because, since RoR was initially made as a prototyping framework (notice all the scaffolding and other features for generating throw-away code), and because of its origin, Rails is actually implementing a very anemic version of MVP.

我称之为贫血",因为他们削弱视图(它应该是MVP中的被动对象,而不是简单的模板)和模型层(是的,它应该是一个复杂的层,而不是 ORM 实例的集合).

I call it "anemic", because they nerfed both View (it should be a passive object in MVP, not a simple template) and Model Layer (yes, it is supposed to be a complicated layer, not a collection of ORM instances).

我建议您阅读两本出版物以更好地掌握该主题:

I would recommend for you to read two publications to get a much better grasp on the subject:

  • Patterns of Enterprise Application Architecture.. mandatory reading for serious developers
  • A Description of the Model-View-Controller User Interface Paradigm in the Smalltalk-80 System

第二个尽可能接近模式的初始定义.这与GUI 体系​​结构"文章一起,应该为您提供有关该主题的坚实基础.PoEAA 书(硬读,顺便说一句)将为您提供扩展它的上下文.

The second one is as close as you can get to initial definition of pattern. That, together with "GUI Architectures" article, should provide you a solid footing on the subject. And the PoEAA book (hard read, btw) would give you context in which to expand it.

这篇关于在Model-View-Controller原理中,什么是Frontend,什么是Backend?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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