CT.Space
Personal Internet Space
28.223
首页(Home)日志(Blog)知识库(KB)关于(About)
IT Technology
Web
操作系统
Language
设计模式
软件工程
程序人生
领域建模
GIS
DataBase
知识标签
老子(4) 养生(3) 夏季(3) 股市(3) 血气(2) mail(2) 婚姻(2) SmtpClient(2) 地震(1) 情绪(1) 爱情(1) 股票(1) 集合初始化器(1) 房地产(1) 日本(1) 理财(1) 饮料(1) 经络(1) 管理(1) 对象初始化器(1) 任正非(1) rss(1) 老板(1) 人力资源(1) 五脏六腑(1) 自动属性(1) 台湾(1) 扩展方法(1) 饮食(1) 人体系统(1) 中国(1) C#(1) 投资(1) iframe(1) 智力(1) 女性(1) 福州(1) 职场(1) 进化(1) 离婚(1) MVC(1) 经济增速(1) 家庭(1) 运动(1) 哭泣(1) 人性(1)
最新知识
  • 非管理员帐号远程桌面修改密码方法
  • windows7开机直接登录
  • windows性能监视器工具
  • 让RSS主动被IE7.0发现
  • IIS6安装urlscan后不支持中文路径的解决方法
  • 打造抵御SQL注入攻击的MSSQL服务器
  • 敏捷开发思想之简单最好
  • 基于.NET的开源GIS项目收集整理
  • 失血模型,贫血模型,充血模型,胀血模型
  • 面向对象设计和系统分层经验谈
IT Technology Understanding the What and Why of the MVC Pattern
Tag MVC
Introduction

With this design for structuring applications garnering such attention, it is becoming more important for people to understand how and why this is being used. I have had a few people ask me questions about MVC. The two questions people ask me about MVC are "What is MVC?" and "How does it help me?" Before anyone tells me this article is too simple or easy, keep in mind I have written this for people who do not know what MVC is. This is really just supposed to be a quick article which can be read quickly and give a person a good understanding of the MVC pattern.

In this article I will be explaining what MVC is and how it can help you develop applications. I will be specifically talking about ASP.NET MVC. The MVC pattern can be used for a lot of different reasons. In a few simple words I will say that MVC is just a different way of structuring an application, and a common reason why someone should use MVC is so that testing becomes easier.

The basic idea with MVC is that the controller is in charge of handling everything. When the view needs data it is the controller's responsibility to pass that data to the view. This means that as its name states, the controller controls things. The view should simply be for viewing things not being concerned with logic. And the model, as its name states, is there to model the data.

 

Aspects of MVC

The obvious thing to wonder is what the acronym even means. It is not easy to tell. A lot of people know what it stands for, but not what it means. The first thing I want to do is explain this.

We have a few pieces I will be explaining. There is obviously the Model, the View, and the Controller. One piece a lot of people forget about is the infrastructure. This includes any external resources you might access: databases, files, RSS feeds, etc. Even though the acronym goes MVC, for the purposes of this article I will be going View, Controller, and then Model.

View - What

As anyone who has ever tried to test a web application knows, it is difficult to test the UI of an application. It is easy to test a method with few dependencies because you can easily just call the method and check the results. With web pages it is quite difficult since you would need to check what html the page is generating. In the case of MVC, the view is not very different from the web forms version. The difference actually comes in how people use them. These views for the most part need to only know about displaying information. Decisions should not be made on the UI. Those decisions should be made elsewhere. Even decisions which will affect UI should be elsewhere and just called from the View.

View - Why

This is a huge advantage to us because we no longer have much to test in the UI. This makes it a lot easier to test the UI because it is a lot less complicated. This makes our tests easier and also has a lot fewer tests we will need in the UI.

Controller - What

The controller is exactly as its name states. The controller takes control of the site. This is where all of the actions logic exits. Actions which users may take using the site are exactly what you will find in the controller. The pages, which users can view, are in here. Forms around the site will post back to these actions in the controller. This is a lot like what used to exist in the web forms. This logic would be contained in the code behind files of the pages.

Controller - Why

As we mentioned before about the view, user interfaces are hard to test. I have also mentioned that we kept a lot of this logic with the UI in the past. By keeping this completely separate the whole time, we are able to keep the view clean of this logic. The controller is actually pretty easy to test. Actions in controllers receive information from the forms when users post to the site. Thanks to bindings, the methods for the actions take parameters which are used to get the data from the user.

When we test these we can pass in the data we want to test as parameters and we are able to look at the result returned from the controller. This allows us to very easily and effectively test the controller and its actions.

Model - What

The model is the connection between the application and the data in the infrastructure. In the model you will define all of your domain objects. This is where entity classes will exist. These classes will define your data. These will include properties of different types as well as probably some logic specifically for working with these classes.

When data needs to be shown to users, it is obtained in the controller and this modeled data is then passed from the controller to the view. The model is where the application is going to get its data.

Model - Why

It just makes sense to keep the data objects and the logic which works with them separate from the rest of the application. I am sure if you know about unit testing, then you know all about the idea of breaking dependencies. Keeping these objects nicely separated allows you to test them more easily.

 

Conclusion

The obvious big answer for why things are structured this way is for testability. By restructuring in this way it is much easier to have each piece separate. We have also managed to keep the most difficult part to test (the User interface) as small and lightweight as possible. This means that more of our application can be tested easily. The ASP.NET MVC Framework is designed to make testing easier. This does not mean that you cannot use it even if you do not test. Anyone can use ASP.NET MVC. Enjoy it.

2009-01-19 14:27 | View(514) | Comment(0) | Send a mail
添加评论
 主题   *  
 姓名   *  
 Email   
 主页  
 
Copyright © 2009. All Rights Reserved. 闽ICP备08006344号 master@crazythief.com      [ Base On L.Smart ]