Mortal Kombat! ASP.NET MVC vs ASP.NET Webforms – ASP.NET MVC is amazing
Introduction to ASP.NET MVC 3.0 with Razor View Engine
description
Transcript of Introduction to ASP.NET MVC 3.0 with Razor View Engine
Introduction to ASP.NET MVC 3.0 with Razor View Engine
http://kb.nextbridge.org 1
By: Shahid Maqsood
Session Agenda• What is ASP.NET MVC?
– What is MVC?• Is it deprecating WebForms?• What are its value propositions?• Is ASP.NET MVC right for you?• Can it ease some web development pain you’re currently
having?
2http://kb.nextbridge.org
ASP.NET Then…
One web application framework to rule them all…
3http://kb.nextbridge.org
Caching Modules
HandlersIntrinsics
Pages Controls
Globalization
Profile
Master Pages
MembershipRoles
Etc.
Asp.Net Now
4http://kb.nextbridge.org
ASP.NETDynamic Data
ASP.NETWebForms
ASP.NETMVC
Presentation
ASP.NETCore
Runtime
Dynamic Data? What is it?
• ASP.NET Dynamic Data lets you create extensible data-driven Web applications by inferring at run time the appearance and behavior of data entities from the database schema and deriving UI behavior from it.
• Dynamic Data supports scaffolding which is a way to
automatically generate Web pages for each table in the database.
• RoR guys will know it very well.
• http://msdn.microsoft.com/en-us/library/ee845452.aspx
Asp.Net Dynamic Data
WebForms is great, but options are good… So,
Asp.Net MVC is another option for Asp.Net Development…
Options are good…
Lets talk about some of the areas from Asp.Net Web Forms that Asp.Net MVC addresses…
7http://kb.nextbridge.org
No real role, responsibility…
8http://kb.nextbridge.org
Master Page
Control
Control
UIPresentation LogicBusiness LogicData Access
Who does what?How and when?
Control
ControlPage
Control
Control
Control
Control
No real role, responsibility…• The level of abstraction that WebForms provides has a lot of
benefits to it, but it doesn’t provide any framework-level guidance in terms of what should do what and when.
• Between your pages, master pages, user controls, server controls, and custom controls, you can end up with a mixture of HTML, data access code, and business logic.
• Yes, we can avoid above issues but it will be the task of the developer.
http://kb.nextbridge.org 9
Control abstractions can be negative…
10http://kb.nextbridge.org
Unit testing… It isn't easy enough to test…
11http://kb.nextbridge.org
UILogic
So how does ASP.NET MVCdiffer?• What is it about ASP.NET MVC that differentiates
itself from WebForms? • What value propositions does it provide and how
does it benefit users?• The primary thing is that it embraces the MVC
pattern at the architectural level, and is generally just about getting out of the way of the developer, allowing you to have full control over the areas you want/need to.
12http://kb.nextbridge.org
What is MVC?
Lets talk about MVC pattern in general.
http://kb.nextbridge.org 13
MVC = Model-View-ControllerThe Model-View-Controller (MVC) pattern is an architectural design principle that separates the components of a Web application. This separation gives you more control over the individual parts of the application, which lets you more easily develop, modify, and test them.
14http://kb.nextbridge.org
Controller(Input)
Model(Logic)
View(Presentation)
Separation of concerns!
MVC = Model-View-Controller
• The Controller is responsible for handling all user input. Once input has been received, the Controller will perform any operations/actions it needs to, which might include interacting with the Model.
• The Model represents the core concern/logic of the application. Once the Controller retrieves some model data and performs any work with the model, it constructs a presentation model that describes the model in terms the View can understand.
• The View is the visual representation of the model. It presents the model data to the actual user in a way that is meaningful. In a web application, this would typically be HTML.
With these three pieces in place, your presentation layer becomes cleanly separated in such a way that each component can be developed/tested independently.
http://kb.nextbridge.org 15
How does Asp.Net MVC look?
16http://kb.nextbridge.org
Request
View
Controller
Response
ControllerHandles input(HTTP requests)
ViewVisually representsthe model
How Asp.Net MVC work?
• So what does MVC look like when implemented over the web?
• When an HTTP request comes into the application it is mapped to a controller. Remember as we mentioned in the previous slide, in the MVC design pattern, the controller is the piece of the trifecta that handles all user input. In the case of a web application, user input is represented as HTTP requests.
• Once the controller has received input, it performs whatever operations it needs to and then assembles a presentation model.
http://kb.nextbridge.org 17
How does Asp.Net MVC work?
• The controller then takes the model and passes it off to the view. Remember that the view is simply a visual representation of the model.
• The view then “transforms” the model into whatever format it uses to represent it. In a web application, this would typically be HTML.
• The view then serves the request by responding with its visual representation.
http://kb.nextbridge.org 18
What are the tenets of ASP.NET MVC?
http://kb.nextbridge.org 19
Framework Goals
• Frictionless Testability– The framework should not cause any friction. – This is very important if you want to seriously perform long-term unit
testing on an application, because if developers were constantly running into points of friction when trying to test, they would eventually abandon it.
• Tight control over <markup>– ASP.NET MVC doesn’t contain any server controls or high-
level abstractions that mask their underlying rendering. – When you develop an ASP.NET MVC application, you have complete
control over the markup. ASP.NET MVC does include some HTML helpers that “hide” away some HTML, but they are at the most basic level of rendering (i.e. just an <input> element).
Framework Goals
• Leverage the benefits of ASP.NET– Because ASP.NET MVC is built on top of the core ASP.NET runtime,
you still have plenty of old-friends that can be used (i.e. profiles, membership, roles, caching). All of the same intrinsic you’ve always known are still valid and used heavily.
• Conventions and guidance– ASP.NET MVC comes with a set of predefined conventions that make
the use of it much easier, without the need to for tons of configuration. It also provides framework-level guidance. The idea is that ASP.NET MVC wants to try to lead developers down the pit of success.
http://kb.nextbridge.org 21
Simple MVC Application
Lets quickly create a simple asp.net MVC application and examine certain areas of it.
http://kb.nextbridge.org 22
Clean URLs
Don’t settle for…
/Products.aspx?CategoryID=123
When you can easily have…
/Product/Puppies
What can be the benefits?– Easy to Remember?– Search Engines? SEO
http://kb.nextbridge.org 23
Extensibility
• Replace anything in the execution pipeline that you don’t like. No restriction to stick with default behaviors or conventions.
• Since ASP.NET MVC employs separations of concerns throughout its entire API, every task has specific representation that can easily be tweaked/replaced, without having to affect anything else around it.
http://kb.nextbridge.org 24
Extensibility
Any of these can be replaced!
http://kb.nextbridge.org 25
ControllerBuilder
ControllerFactory
Controller
ViewEngine
View
ControllerActionInvoker
ActionResult
ActionFilters
Model Binders
Some benefits of Asp.Net MVC
• Part of Asp.Net framework• Easy to test – unit testing• Easy to maintain, upgrade and perform fixes• Can improve collaboration amongst teams• More powerful solutions - you have complete
control over markup.• Improved reliability and robustness
http://kb.nextbridge.org 26
When should I use ASP.NET Web Forms vs. ASP.NET MVC?
You should use ASP.NET MVC when:
• Complete control over markup is important to you and you are willing to put in the extra effort.
• You don’t like the abstraction offered by controls.
• You don’t mind writing inline code.
• You are interested in pattern-based software development and like the separation between layers offered by the MVC pattern.
http://kb.nextbridge.org 27
When should I use ASP.NET Web Forms vs. ASP.NET MVC?
You should use ASP.NET Web Forms when:
• Creating a website faster is more important to you than complete control over markup.
• You like the abstraction offered by controls.
• You want to avoid writing inline code.
• You are not interested in pattern-based software development and are not familiar with the MVC pattern.
http://kb.nextbridge.org 28
Summary
• ASP.NET MVC is a new application option built on top of ASP.NET
• WebForms isn’t being deprecated
• ASP.NET MVC strives to provide strong, frictionless testability
• It places a lot of the control in your hands
http://kb.nextbridge.org 29
Further readings
• www.asp.net/mvc• http://
msdn.microsoft.com/en-us/library/dd381619.aspx
• http://msdn.microsoft.com/en-us/library/dd381612.aspx
http://kb.nextbridge.org 30
Thank you!
Questions please?
http://kb.nextbridge.org 31