ASP.NET MVC Blogging Engine

Download Report

Transcript ASP.NET MVC Blogging Engine

Introduction to ASP.NET MVC

Adnan Masood Volkan Uzun

www.CodePlex.com/YABE

About Adnan

aka. Shameless Self Promotion • • • • • • • • • Sr. Software Engineer / Tech Lead for Green Dot Corp. (Financial Institution) Design and Develop Connected Systems.

Involved with SoCal Dev community, co-founded San Gabriel Valley .NET Developers Group. Published author and speaker. Winner of 2008-2009 Developer Community Champion Award.

MS. Computer Science, MCPD (Enterprise Developer), MCT, MCSD.NET

Doctoral Student - Areas of Interest: Machine learning, Bayesian Inference, Data Mining, Collaborative Filtering, Recommender Systems.

Contact at [email protected]

Read my Blog at www.AdnanMasood.com

REST and WCF 3.5 – Geek Speak Webcast Doing a session in IASA 2008 in San Francisco on Aspect Oriented Programming; for details visit http://www.iasaconnections.com

About Me

• • • • • • • • • • Web Developer at California State University, San Bernardino Developing campus web sites, online courses, databases, securing the systems, making the sites accessible according 508 Guidelines Active member of Inland Empire .NET user group User group freak, attending average 5/6 users groups a month, driving 1000 miles average extra.

MS Computer Science, MCTS SQL Server 2005, MCP ASP.NET 2.0

2007-2008 Most Valuable member of Inland Empire .NET user group Not so active blog: http://msnetprogrammer.net/blog Trying to write better code by applying TDD (newbie!), reading other developers’ code (Scott Hanselman’s idea) Lifetime learner

Email: [email protected]

Agenda

1. Introduction to MVC 2. Building your first MVC application (demo) 3. Understanding Controllers, Controller Actions, and Action Results 4. Understanding Models, Views, and Controllers 5. How to do Routing (demo) 6. How to do unit testing (demo) 7. How does YABE work (code walkthrough and demo) 8. Brief discussion of dependency injection et al

Top 5 MVC Resources

1. ASP.NET MVC Home: www.asp.net/mvc

2. Scott Guthrie’s Weblog

http://www.weblogs.asp.net/Scottgu

3. Scott Hanselman’s Blog

http://www.hanselman.com/blog/

4. Phil Haack

http://haacked.com/Tags/aspnetmvc/default.aspx

5. ASP.NET Quick Start

http://quickstarts.asp.net/3-5-extensions/mvc/default.aspx

ASP.NET MVC in a nutshell

• • • • • ASP.NET MVC enables you to build Model View Controller (MVC) applications by using the ASP.NET

Forms.

framework.

ASP.NET

MVC is an alternative, not a replacement, for ASP.NET Web Benefits

Clear separation of concerns Testability - support for Test-Driven Development Fine-grained control over HTML and JavaScript Intuitive URLs

Highlights & Latest Developments in the MVC world

• • • • MVC Beta has been released JQuery is coming with MVC Beta More TDD support Some functions are carried over between dll’s

MVC vs. “Classic” ASP.NET

Model View Controller Pattern

Why MVC?

• • • • Very Clean separation of Concerns Inherent Unit Testing Support Model that leads you down a maintainable path (helps writing better code but doesn’t stop you writing crappy code) Clean URLs and HTML

MVC Features

• • • • • • Routing Controller View Dependency Injection Pluggable Interfaces for all core contracts

DEMO

• Build your first ASP.NET MVC APP

MVC Components

• • • Model: Your data source; database, text file, web services etc View: HTML page, what client sees Controller: CPU of the mechanism, gets request from web server, communicates with Model to get data, returns a view to the requestor.

 A URL request is a function inside the controller class

URLs in MVC

• • • • • • URL no longer points to the view/aspx page A URL maps to a controller class Allows more flexibility to map Urls URL routing engine, Route table Routes are tokenized strings Can use RegEx

Controllers & Actions

• • • • • Responds to requests Interacts with Model, and renders Views All controllers should have suffix “Controller” Derives class  from

System.Web.MVC.Controller

At the end, it is just a class, that has functions  Controller Actions that can’t be static, returns ActionResult

ActionResult & Actions

Controller Actions return ActionResult

ActionResult can be:

ViewResult: Represents HTML and markup.

EmptyResult : Represents no result.

RedirectResult: Represents a redirection to a new URL.

RedirectToRouteResult: Represents a redirection to a new controller action.

JsonResult: Represents a JavaScript Object Notation result that can be used in an AJAX application.

ContentResult: Represents a text result.

Custom ActionResult

Model, View, Controller

• • • • • Controller gets the request from the requestor, (route mapping happens here), fires the action depending on the request ActionMethod communicates with Model to get the data, returns ActionResult ActionMethod can use ViewData, ViewData.Model

or TempData to send data to View View generates the markup language for the requestor.

View can use strongly typed Model

Views/1

• • • • • • Similar to existing page

No postback or view state

Not all page events will fire Editing forms requires more work Separate pages for viewing, editing, and updating Ajax will use UserControls as update panels but request still goes through the control

Views/2

• • • • • Views probably shouldn’t directly access data sources or services.

You shouldn’t put the business logic inside the views; try to stay away from <%if … Use the ViewData which stores the model the view is rendering ViewPage, ViewPage

No postback

DEMO

• Add a new controller Test, with model interaction

How much backward compatibility is there?

• The MVC framework supports using the existing ASP.NET .ASPX, .ASCX, and .Master markup files as "view templates" (meaning you can easily use existing ASP.NET features like nested master pages, <%= %> snippets, declarative server controls, templates, data binding, localization, etc). It does not, however, use the existing post-back model for interactions back to the server. Instead, you'll route all end-user interactions to a Controller class instead - which helps ensure clean separation of concerns and testability (it also means no viewstate or page lifecycle with MVC based views).

• 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, etc.

URL Routing

• • • • • • • URL Routing is mapping the incoming requests to controller’s action methods.

There are handler’s settings in web.config Routing table exists in Global.asax.cs

Application_Start() registers the routing table

You can use regex to define route mappings Route mapping works top to bottom, first match is called

Download Phil Hack’s RouteDebug tool

Route Examples

• • • • // http://domain/Home/CustomerDetail/45 Html.Link for creating outbound urls Html.Link(, , )

DEMO

• Add a new controller action , set the route

Unit Testing

• • • • • • Mock Objects to simulate responses.

Don’t have to go through a page or view Red/Green Testing Create instance of controller Uses Mock Objects You will need depedency injection down the road

YABE DEMO

• •

YABE

YET ANOTHER BLOG ENGINE

Uses MVC Preview 5 Check it out: http://www.codeplex.com/yabe

Future of ASP.NET MVC

• • MVC for the Enterprise Classic ASP.NET for quicker implementations

References

• Download latest MVC Beta from Codeplex • Quick Start Guides http://quickstarts.asp.net/3-5-extensions/mvc/default.aspx

Questions?

• ASP.NET MVC is a new paradigm for .NET Developers; a new way to think about programming web application.

• Any Questions / Comments … feel free to contact [email protected]

[email protected]

• Visit www.CodePlex.com/YABE