本文介绍了Ruby on Rails 中的 MVC 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用外行的语言向我解释 Ruby on Rails 中的 MVC.我对理解 MVC 中的模型特别感兴趣(无法理解模型).

Could someone please explain MVC to me in Ruby on Rails, in layman terms. I am especially interested in understanding the Model in MVC (can't get my head around the model).

推荐答案

一些背景知识,MVC 是一种(复合)设计模式,由 Trygve Reenskaug (Smalltalk) 于 1979 年开发.

Some background, MVC is a (compound) design pattern and was developed in 1979 by Trygve Reenskaug (Smalltalk).

真正的 MVC 主要计划用于 n 层(非 Web)系统,它将系统分为 3 个不同的部分,模型、视图和控制器

True MVC was primarily planned for use in n-tier (non web) systems and it splits a system into 3 distinct parts, a Model, View and Controller

模型

  • 包含应用程序的数据(通常链接到数据库)
  • 包含应用程序的状态(例如客户有什么订单)
  • 包含所有业务逻辑
  • 通知视图状态变化(** 不适用于 ROR,见下文)
  • 不了解用户界面,因此可以重复使用

风景

  • 生成向用户呈现数据的用户界面
  • 被动,即不做任何处理
  • 一旦数据显示给用户,视图工作就完成了.
  • 由于不同的原因,许多视图可以访问同一个模型

控制器

  • 从外部世界接收事件(通常通过视图)
  • 与模型互动
  • 向用户显示适当的视图

** 经典 MVC 不适合 Web 应用程序,因为模型不能以观察者的方式将所有更改发送到视图(视图是网页).上世纪 90 年代,JSP 团队引入 Model2 是为了克服不断变化的基础架构.MVC Web 框架实际上不是 MVC,而是 Model2(Ruby on Rails 也是如此).

** Classic MVC is not suited to web applications, as the model cannot send all changes to the view in an observer fashion (the view is a web page). The Model2 was introduced to overcome the changing infrastructure by JSP team in 90s . MVC Web frameworks are really not MVC, but Model2 (this is true of Ruby on Rails).

这是来自大师 Martin Fowler 的 GUI 模式的描述,包括 MVCGUI 架构

Here is a description of GUI patterns including MVC from the master, Martin FowlerGUI Architectures

到目前为止我找到的最好的书是Agile使用 Rails 进行 Web 开发.它从假设没有知识开始,并且非常全面.

The best book I have found so far is Agile Web Development with Rails. It begins by assuming no knowledge, and is quite comprehensive.

希望这对你有所启发!

这篇关于Ruby on Rails 中的 MVC 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:35