问题描述
有人可以用外行术语在Ruby上向我解释MVC.我对了解MVC中的模型特别感兴趣(无法围绕模型).
推荐答案
一些背景,MVC是一种(化合物)设计模式,是由Trygve Reenskaug(SmallTalk)于1979年开发的.
True MVC主要计划在N层(非Web)系统中使用,并将系统分为3个不同的部分,一个模型,视图和控制器
模型
- 包含应用程序的数据(通常链接到数据库)
- 包含应用程序的状态(例如,客户的订单)
- 包含所有业务逻辑
- 通知状态变化的观点(**不正确,请参见下文)
- 对用户界面不了解,因此可以重复使用
视图
- 生成用户界面,该界面向用户显示数据
- 被动,即不进行任何处理
- 一旦将数据显示给用户. 进行工作.
- 许多视图由于不同的原因可以访问相同的模型
控制器
- 从外界接收事件(通常是通过视图)
- 与模型互动
- 向用户显示适当的视图
**经典MVC不适合Web应用程序,因为该模型无法以观察者的方式向视图发送所有更改(视图是网页).引入了Model2,以克服90年代JSP团队不断变化的基础架构. MVC Web框架实际上不是MVC,而是Model2(Ruby在Rails上是正确的).
这是对GUI模式的描述,包括Martin Fowler的MVC gui Architectures
到目前为止,我发现的最好的书是 agile铁轨开发.它首先是没有知识,并且是非常全面的.
希望这有助于您为您提供一些灯光!
其他推荐答案
MVC基本上指示模型视图控制器.和MVC由PHP,Perl,Python等许多语言使用.通常MVC的工作原理:
请求首先出现在控制器上,控制器查找和适当的视图并与模型进行交互,模型与您的数据库进行交互,并根据响应将响应发送给控制器,然后将响应发送给Controller,将输出参数提供给视图.
.其他推荐答案
您的模型是您程序使用的数据结构.
视图是与屏幕或下一个级别交互的部分.
控制器通常在模型和查看
之间处理数据MVC结构通常是嵌套的,因此"模型"或"视图"可能包含其自己的MVC(考虑屏幕上的组件.绘制自己的小视图,具有自己的小型模型(您传递的字符串),并有一个小控制器将数据绘制到视图上.
在Rails中,模型,视图和控制器的角色由框架明确定义,任何教程都会指出这三个组件,因为它会引导您浏览其创建的文件.
在其他系统中,这些部分可能更难识别.另外,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).
推荐答案
Some background, MVC is a (compound) design pattern and was developed in 1979 by Trygve Reenskaug (Smalltalk).
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
The Model
- Contains data for the application (often linked to a database)
- Contains state of the application (e.g. what orders a customer has)
- Contains all business logic
- Notifies the View of state changes (** not true of ROR, see below)
- No knowledge of user interfaces, so it can be reused
The View
- Generates the user interface which presents data to the user
- Passive, i.e. doesn’t do any processing
- Views work is done once the data is displayed to the user.
- Many views can access the same model for different reasons
The Controller
- Receive events from the outside world (usually through views)
- Interact with the model
- Displays the appropriate view to the user
** 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).
Here is a description of GUI patterns including MVC from the master, Martin Fowler GUI Architectures
The best book I have found so far is Agile Web Development with Rails. It begins by assuming no knowledge, and is quite comprehensive.
Hope this helps to shed some light for you!
其他推荐答案
MVC basically indicates Model-View-Controller. And MVC used by many languages like PHP, Perl, Python etc. Generally MVC works like this:
Request first comes to the controller, controller finds and appropriate view and interacts with model, model interacts with your database and send the response to controller then controller based on the response give the output parameter to view.
其他推荐答案
Your Model is the data structure that your program uses.
The View is the part that interacts with the screen or the next level up.
The Controller generally processes data between the model and view
MVC structures are often nested, so a "Model" or "View" may contain its own MVC (Think of a component on the screen. You may just fill it with a string, but behind the scenes the code of the component draws its own little view, has it's own little model (the string you pass in) and has a little controller drawing the data onto the view.
In Rails, the roles of the model, view and controller are well-defined by the framework, any tutorial will point out the three components as it walks you through the files it created.
In other systems, those pieces may be harder to identify. Also, MVC is not "Perfect", just keep in mind that there are valid alternatives, but it's a good way to start organizing.