Download ASP.NET MVC Framework

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

URL redirection wikipedia , lookup

Transcript
ASP.NET MVC Framework
Damian Sorin, Ionel Ancuta
Abstract. The paper focuses on features of ASP.NET MVC framework that
make it easier to use than other existing frameworks. It also covers topics like
developer’s control over the application and unit testing. The framework has
evolved with each of it previews and pre release versions based on community
feedback. It is not meant as a replacement for the classic ASP.NET WebForms
framework but leverages a lot of the functionality and extensibility advantages
provided by it. Even if it is a new framework it is gaining popularity fast in the
.net developer community. A new version is of the framework is already on its
way.
Keywords: asp.net, mvc, framework test driven development
1
Introduction
ASP.NET MVC is a Microsoft product launched in April 2009 under Microsoft
Public License (version 1.0). It is now at the second preview for version 2.0.
Using ASP.NET MVC, developers can easily build web applications having full
control over views, enabling AJAX integration and making test driven development
more accessible.
ASP.NET MVC is not a replacement for WebForms. It actually works as an
extension for ASP.NET. The ASP.NET MVC framework fully supports existing
ASP.NET features like forms/windows authentication, URL authorization,
membership/roles, output and data caching, session/profile state management, health
monitoring, configuration system, the provider architecture, and many more.
2
Main principles
2.1
MVC
As the name suggests, the framework relies on model view controller design pattern.
Coupling these three components makes independent testing easier.
The model is responsible for maintaining the state. Very often, the state is kept in a
database.
The view is responsible for displaying the application’s user interface.
The controller is used to handle end user interaction, to manipulate the model and
finally to render the view to be displayed.
2
Damian Sorin, Ionel Ancuta
In a MVC application the view is only about displaying information. It is the
controller that handles and responds to user input and interaction.
2.2
Extensibility
By default, ASP.NET MVC uses regular .aspx pages to render the view. But since the
framework is made of interchangeable parts, different types of view engines can be
used (for example, Spark, NVelocity, Brail)
It is highly extensible and pluggable. Everything in the MVC framework is
designed so that it can be easily replaced/customized (for example: you can optionally
plug-in your own view engine, routing policy, parameter serialization, etc). It also
supports using existing dependency injection and IOC container models (Windsor,
Spring.Net, NHibernate, etc).
It includes a very powerful URL mapping component that enables building
applications with clean URLs (URLs that don’t have extensions within them and are
designed to easily support SEO and REST-friendly naming patterns).
2.3
Files replaced by methods
This is a core concept in ASP.NET MVC. Working with WebForms implied that a
request was actually a request for a page which was translated into HTML and then
served as response. In ASP.NET MVC’s case, the request is handled by a routing
engine which decides what controller and action to use. The actions are merely public
methods and they hand the control to the view which is rendered to HTML and
returned to the user.
2.4
Testability
Working with test units is easier due to separation of concerns, one of the
framework’s principles. “Mockable” components and their isolations help when it
comes to writing independent tests for a particular component. Also, ASP.NET MVC
doesn’t use page lifecycle.
The developer can unit test the application without having to run the Controllers
within an ASP.NET process (making unit testing fast). Any unit testing framework
would work : NUnit, MBUnit, MS Test, and so on.
3
Architecture
ASP.NET MVC relies on a certain amount of convention to reduce the amount of
configuration. One of these conventions regards naming of directories where
application’s views and controllers are kept. The ASP.NET MVC expects directories
“Views” and “Controllers” to exist. According to this naming convention, images
and CSS files should be placed in the “Content” directory but this is not mandatory.
Error! Use the Home tab to apply title to the text that you want to appear here.
3
In order to make a simple but functional ASP.NET application, the following files
need to be added (taken from a starter ASP.NET MVC project):





HomeController.cs
HomeController view files (Index.aspx and About.aspx in the example)
Site.master file
~/Views/Web.config
Other optional directories (for ajax helpers, for example)
Some small changes in Web.config need to be done so that for the site to make the
right requests to the ASP.NET MVC engine (each required assembly should be
referenced for compilation, a namespace reference to system.web/pages section
should be added to allow access to System.Web.Mvc helpers, System.Linq and
System.Collections.Generic from the view page). The only required namespaces are
System.Web.Mvc and System.Web.Mvc.Html. The others are just helpful.
Registering the UrlRoutingModule HttpModule is mandatory because this is the
module responsible for matching the URL requested to the proper route
(Controller/Action).
4
Comparison with Zend Framework
Both ASP.NET MVC and Zend are frameworks based on model view controller
design pattern. They both simplify developer’s work by offering “shortcuts”:
generated views, forms, validations, and so on.
They both offer a “default” directory structure which can be altered from
configuration files (Web.config, bootstrap), they have a routing engine that tells
which controller and action should be considered next and separation of concerns is a
principle enforced in both frameworks.
Other than that, ASP.NET offers a stronger support when using databases: with
Linq to Sql there is no need to create models while Zend_Db imposes creation of
those models and this may be considered a waste of time.
5
Limitations and future improvements
Amongst the improvements the second preview brought are the following:
 Client-side validation: ASP.NET MVC 2 includes the jQuery validation library to
provide client-side validation based on the model’s validation metadata. Also,
using adaptors to change the client library is possible.
4
 Areas: Preview 2 includes in-the-box support for single project areas for
developers who wish to organize their application without requiring multiple
projects. Registration of areas has also been streamlined.
 Model validation providers: They allow hooking in alternative validation logic to
provide validation when model binding. The default validation providers uses Data
Annotations.
 Metadata providers: They allow hooking in alternative sources of metadata for
model objects. The default metadata provider uses Data Annotations.
6
References
Professional ASP.NET MVC 1.0, Rob Connery, Scott Guthrie, Phil Haack, Scott
Hanselman, Wiley Publishing, 2009
ASP.NET MVC in action, Jeffrey Palermo, Ben Schreiman, Jimmy Bogard,
Manning Publications, 2009
http://www.asp.net/mvc/
http://www.asp.net/learn/mvc/tutorial-01-cs.aspx
http://www.asp.net/learn/mvc-videos/video-8145.aspx - technical overview