10

I've been learning about the Model-View-Controller paradigm ("MVC"), but I'm quite confused since some tutorials contradict other tutorials.

My current understanding of the process looks something like this:

Router / Dispatcher / Front Controller:

  • Though not specifically referenced in the "MVC" name, the Router is still a very important part. It is here that requests are translated from raw URLs to a specific controller. For example, routing a request for www.StackUnderflow.com/question/123 to the "Question" Controller of the application.

Model:

  • This is where raw data is collected from some storage source, such as a database or XML file. The model serves as an abstraction layer, translating the Controller's request for specific data into (for example) an SQL query, and translating the query results into a standard format like a data object.

  • For example, in the /browse/all scenario stated above:

    • The "Question" Controller would ask the Model "Please give the data for question 123."
    • The Model would then translate that into "SELECT * FROM Questions WHERE Id = 123;" and punt it to the database
    • The database would return a "Question" record to the Model.
    • The Model would take the record, and translate it into a Question data object
    • The Model then asks does the same thing "SELECT * FROM Answers WHERE QuestionID = 123;" and creates an array of Answer objects from the resultset, and adds that to the Question object's answers member variable.
    • The Model would return the Question object to the "Question" Controller.

Controller:

  • This is the real workhorse of the application. In addition to relaying messages back and forth to the Model and the View, the Controller is also responsible for things like Authorization and application/"business" logicEdit: Per answer, business logic belongs in the Model.

  • In the ongoing example, the Controller wold be responsible for:

    • Ensuring the user is logged in.
    • Determining the QuestionId from the URL.
    • Determining which View to use.
    • Sending HTTP codes and redirecting if needed.
    • Asking the Model for data, and store needed data in member variables.

View:

  • By and large, the View is the simplest part of the application. It mostly consists, in a basic application, of HTML templates. These templates would have placeholders to insert data into the template from the Controller's member variables:

e.g.

<html>

  <head>
    <title>
      <?php $question->getTitle() ?>
    </title>
  </head>

  <body>
    <h1> <?php $question->getQuestionText(); ?> </h1>
    <h2> Answers: </h2>
    <div class="answerList"> 
      <?php formatAnswerList($question->getAnswers()); ?> 
    </div>
  </body>

</html>
  • The View would also contain methods to format data for delivery to the user. For example, the formatAnswerList() method above would take an array of answers, taken from the Controller, and loop through them while calling something like include $markupPath . "/formatAnswer.inc" which would be a small template of just an answer container.

Questions:

  • Is this view of MVC fundamentally accurate?
  • If not, please carefully explain which components are misplaced, where they should actually go, and how they should properly interact with the other components (if at all).
  • How many classes are used to describe this? In my example there are four objects - one each for the three components of MVC and one that simply stores related data for transmission. Is this normal, or should some be combined. If so, which ones?
4

2 に答える 2

2

この説明は、コントローラーに重きを置きすぎて、モデルに十分ではないと思います。モデルは、理想的にはビジネスロジックが存在する場所です。コントローラは、実際にはサイトのユーザーのための単なるインターフェイスであり、必要な場所にルーティング制御を行います。このトピックに関する以前の議論を見てください。

MVCを理解する:モデルの「Fat」、コントローラーの「Skinny」の概念は何ですか?

于 2010-08-23T13:10:53.293 に答える
0

基本的に、すべてが適切な場所にあります。

この例では、Questionクラスを定義します。これはViewModelと呼ばれ、ビューで使用される/ビューから取得されるすべてのデータの単なるコンテナーです。

場合によっては、ViewModelが見落とされ、モデルがViewに渡されるのを目にしました。これは、チュートリアルを最初に見たときに混乱しました。ViewModelを省略するのは好きではないので、混乱を招くと思います。

于 2010-08-23T13:12:24.617 に答える