May 13, 2015
Key Take Aways…
• Why MVC?• What is MVC?• How to Implement MVC?• ASP .Net VS 2010 MVC Enhancements…
Why MVC?
• Easier code maintenance and support.• Supports automated unit testing tools such as
NUnit, MBUnit,...• Decouple business and presentation
application layers.• Web-based thin-client application.• User interface logic tends to change more
frequently than business logic.
Patterns…• Page Controller Pattern
• ASP.NET web forms are the code-behind classes.• Each page has a code-behind class.• URL requested by the client is directly handled by
individual pages.• All page code is in the code-behind files.• By default, ASP.NET is page controller based.
Patterns…• Front Controller Pattern
• MVC is based on a front controller design.• Centralized controller instead of multiple
controllers.• Trap all client requests, and controller decides
which view or .aspx to render.
Client Request
Central Controller
MyPage2 ASPXHTML View
MyPage1 ASPXHTML View
What is MVC?• Model: This refers to the data that is shown in the UI.
Data can come from different sources.• View: This refers to the UI components that will show the
model data.• Controller: This controls when to change the view, based
on user actions.• MVC design is not a replacement to the n-tier
architecture.• The model, the view, and the controller are not related
directly to the layers, or to the physical tiers; they are logical components that operate together in a certain pattern.
Basics of MVC• Model is independent of the view and the controller.
• Controller is associated with the view, allowing to change views independent of the controller.
• View depends on the model, and is updated when the model's state has changed.
Controller View
Model
REST: Representation State Transfer• Architectural pattern used to identify and fetch
resources from networked systems. e.g. www.• Sharing of resources via unique identifiers.• No need to check for a page postback.• Foll. code is not supported in REST approach:
btnSave_Cick(){Response.Redirect("~/MyPage.aspx");}
ASP.NET MVC Framework• In MVC framework, URL routing is used. • URLs are used based on the settings in a config
file. e.g.: http://localhost/customer/list/ instead of traditional URL like: http://localhost/CustomerList.aspx
• ASPX pages are kind of view engine, nothing else.
• ASP.NET MVC framework is not a replacement or upgrade of web forms, but merely another way of programming.
URL Routing Engine• Standard ASP.NET model
• MVC Framework model:
Client browser requests URL
http://myapp/CustomerList.asp
x
CustomerList.aspx processed by
ASP.NET runtime and HTML
returned to IIS
IIS
Client browser requests URL
http://myapp/Customer/List
ASP.NET runtime
IIS Controller Class
View (ASPX)
Model Class
MVC 2.0 Enhancements• Includes ASP.NET MVC 2 framework.• Strongly Typed Html Helpers: view model
level error instead of all validation Errors ASP.NET MVC 1 : <%= Html.TextBox(“ClassId”,
Model.ClassId) %> ASP.NET MVC 2 : <%= Html.TextBoxFor(model => model.ClassId) %>
• Model Validation: Validation logic is always enforced on the server, and if needed can be done on client via JavaScript.
MVC 2.0 Framework Enhancements• Areas Support: Group controllers and views
into sections.• Data-Annotation Attribute Validation Support:
Enable to perform validation by adding one or more attributes. (Add References: Microsoft.Web.Mvc.DataAnnotations.dll and System.ComponentModel.DataAnnotations.dll assemblies. E.g.:
MVC 2.0 Framework Enhancementsusing System.ComponentModel;Using System.ComponentModel.DataAnnotations;namespace MvcApplication1.Models{public class Product{public int Id { get; set; }[Required][StringLength(10)]public string Name { get; set; }[DisplayName("Price")][Required][RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]public decimal UnitPrice { get; set; } }}
Suggested Reading…• ASP.Net 3.5 (Application Architecture and Design) by Vivek Thakur