First knowledge of MVC

Source: Internet
Author: User
Tags php language

MVC, called the Model View controller, is the abbreviation for models-views-controllers (Controller), a software design paradigm that organizes code with a separate approach to business logic, data, and interface display. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

First meet you--MVC will always put you and the previous study of the three layers and design patterns confused, and then this, small series to consult relevant information, and then, small series on these similar knowledge points for a simple comparison:

MVC PK Three Layer

Three-layer Architecture interface layer, business logic layer and data access layer, many people in the MVC three core components with the three-tier architecture, the interface layer equals the view, the business logic layer equals the controller, the data access layer equals the model, this is completely wrong, The MVC design pattern solves the problem of page code, page control logic and data coupling, so she first belongs to the interface layer, for example, MVC and structs are interface layer framework, the controller in MVC is responsible for controlling the interface, for example, jumping between pages, displaying logic, etc. Three layer architecture in the business logic layer is mainly the business Entity data processing, the processing of data to the page display, MVC model is only the data entity, does not have the function of adding and removing changes, she received data from the business Logic layer processing good data passed over, And the three-layer architecture within the data access layer is a function of adding and deleting, is directly on the database operation, for the business logic layer to provide data support, avoid MVC is only three layers inside the interface layer, do not confuse the OH.

MVC PK Design Pattern

Many small partners tend to confuse frame patterns with design patterns, thinking that MVC is a design pattern. In fact, they are completely different concepts.
The two concepts of frame and design pattern are always easy to be confused, but there are still differences between them. Frameworks are often code reuse, while design patterns are reuse, schemas are in between, part of code reuse, partial design reuse, and sometimes analysis can be reused. There are three levels of reuse in software production: internal reuse, an abstract block that can be used publicly in the same application, code reuse, the assembly of common modules into libraries or toolsets for use in multiple applications and domains, and the reuse of application frameworks that provide a common or off-the-shelf infrastructure for specialized areas For the highest level of reusability.

Although the framework is similar to the design pattern, it is fundamentally different. The design pattern is a description of the recurring problems in an environment and the solution to the problem, which is more abstract than the framework, the framework can be expressed in code, can be directly executed or reused, and only instances of the schema can be represented in code; Design patterns are smaller elements than frames. A framework often contains one or more design patterns, and the framework is always targeted at a particular application area, but the same pattern can be applied to a variety of applications. It can be said that the framework is software, while design patterns are software knowledge.

What are the framework modes?
MVC, MTV, MVP, CBD, ORM and so on;
What are the frameworks?
C + + language qt, MFC, Gtk,java language ssh, ssi,php language smarty (MVC mode), Python language Django (MTV mode), etc.
What are the design patterns?
Factory mode, adapter mode, policy mode, etc. in short: the framework is a great wisdom, used for the division of Software design, design mode is a small skill, the specific problems proposed solutions to improve code reuse rate, reduce coupling. After the introduction of the difference and contact, back to the main character of today's blog MVC, and then a small series to do a simple introductory example.

The first step: Create an example of MVC, as shown in:

Step Two: Select an Internet application:

Then we started to write code to create a new dog class in the model to simulate the data, as shown in the following code:

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.web;namespace mvcTest.Models{    public class Dog    {public        int ID {get; set;}        public string name{get; set;}        public override string ToString ()        {            return ' id= ' + this.id + ", name=" + this. Name;}}    } </span></span>
The third step: Create a new controller under the Controllers folder, which can be used to get the data, add the controller, right-click the controllers, add the controller, the code is as follows:

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >using mvctest.models;using system;using system.collections.generic;using system.linq;using System.Web;using               System.web.mvc;namespace mvctest.controllers{//Controller Class (Inherited controllers) public class Homecontroller:controller {        #region Initialize data collection +void InitData ()///<summary>//initialization data set//</summary>             Public list<models.dog> InitData () {list<models.dog> List = new List<models.dog> ()  {New Dog () {id=1,name= "Sample 1~~"}, New Dog () {id=2,name= "Sample 2~~"}, New        Dog () {id=3,name= "Sample 3~~"}, New Dog () {id=4,name= "Sample 4~~"}}; } #endregion//action method (but as model for MVC design pattern) public ActionResult Index () {Sy Stem.           Text.stringbuilder sbhtml = new System.Text.StringBuilder (4000); Handle current business (e.g. read database, judge etc)//Create aData collection (pseudo-data), gets the list<models.dog> List = InitData (); Iterate through the collection, generate HTML code, and deposit sbhtml list.            ForEach (D + = {sbhtml.appendline ("<div>" + d.tostring () + "</div>");           }); Use ViewBag to transfer data to the index.cshtml view with the same name//viewbag is a collection of dynamic types that can dynamically add properties and values of any name of any type VIEWBAG.HTMLSTR = SbH Tml.            ToString ();        return View (); }}}</span></span>
Fourth Step: Add the view, the code looks like this:

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >@{    Layout = null;} <! DOCTYPE html>
at this point, a simple example of MVC is done.

Small Editor's note: this blog post, a small part of the basic knowledge of MVC, and using MVC to knock a simple example, and the MVC and three layers, and MVC and the previous learning of the design patterns in a simple contrast, through the study of MVC, The understanding of this piece of knowledge deepened a little bit, file management project, not finished, to be continued ...

First knowledge of MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.