Introduction to ASP.NET MVC 3.0 with Razor View Engine

31
Introduction to ASP.NET MVC 3.0 with Razor View Engine http://kb.nextbridge.org 1 By: Shahid Maqsood

description

Introduction to ASP.NET MVC 3.0 with Razor View Engine. 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 ?. - PowerPoint PPT Presentation

Transcript of Introduction to ASP.NET MVC 3.0 with Razor View Engine

Page 1: 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

Page 2: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 3: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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.

Page 4: Introduction to ASP.NET MVC 3.0 with Razor View Engine

Asp.Net Now

4http://kb.nextbridge.org

ASP.NETDynamic Data

ASP.NETWebForms

ASP.NETMVC

Presentation

ASP.NETCore

Runtime

Dynamic Data? What is it?

Page 5: Introduction to ASP.NET MVC 3.0 with Razor View Engine

• 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

Page 6: Introduction to ASP.NET MVC 3.0 with Razor View Engine

WebForms is great, but options are good… So,

Asp.Net MVC is another option for Asp.Net Development…

Options are good…

Page 7: Introduction to ASP.NET MVC 3.0 with Razor View Engine

Lets talk about some of the areas from Asp.Net Web Forms that Asp.Net MVC addresses…

7http://kb.nextbridge.org

Page 8: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 9: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 10: Introduction to ASP.NET MVC 3.0 with Razor View Engine

Control abstractions can be negative…

10http://kb.nextbridge.org

Page 11: Introduction to ASP.NET MVC 3.0 with Razor View Engine

Unit testing… It isn't easy enough to test…

11http://kb.nextbridge.org

UILogic

Page 12: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 13: Introduction to ASP.NET MVC 3.0 with Razor View Engine

What is MVC?

Lets talk about MVC pattern in general.

http://kb.nextbridge.org 13

Page 14: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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!

Page 15: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 16: Introduction to ASP.NET MVC 3.0 with Razor View Engine

How does Asp.Net MVC look?

16http://kb.nextbridge.org

Request

View

Controller

Response

ControllerHandles input(HTTP requests)

ViewVisually representsthe model

Page 17: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 18: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 19: Introduction to ASP.NET MVC 3.0 with Razor View Engine

What are the tenets of ASP.NET MVC?

http://kb.nextbridge.org 19

Page 20: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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).

Page 21: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 22: Introduction to ASP.NET MVC 3.0 with Razor View Engine

Simple MVC Application

Lets quickly create a simple asp.net MVC application and examine certain areas of it.

http://kb.nextbridge.org 22

Page 23: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 24: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 25: Introduction to ASP.NET MVC 3.0 with Razor View Engine

Extensibility

Any of these can be replaced!

http://kb.nextbridge.org 25

ControllerBuilder

ControllerFactory

Controller

ViewEngine

View

ControllerActionInvoker

ActionResult

ActionFilters

Model Binders

Page 26: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 27: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 28: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 29: Introduction to ASP.NET MVC 3.0 with Razor View Engine

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

Page 31: Introduction to ASP.NET MVC 3.0 with Razor View Engine

Thank you!

Questions please?

http://kb.nextbridge.org 31