Introduction to ASP.NET MVC

acceptableseashoreΑσφάλεια

5 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

107 εμφανίσεις

Introduction to ASP.NET MVC
Blogging Engine in 60 minutes

Adnan Masood

www.AdnanMasood.com

www.CodePlex.com/YABE

About Me

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.


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 adnanmasood@acm.org


Read my Blog at
www.AdnanMasood.com


Doing

a session in IASA 2008 in San Francisco on Aspect Oriented
Programming; for details
visit http://www.iasaconnections.com

YABE

Yet another blogging engine


Team


Adnan Masood


Jeff Bergman


Sean Xiao


www.CodePlex.com/YABE


Uses
Linq

to SQL for data access


Syndication Libraries for the RSS/Atom Feeds


Experiment in learning MVC

Latest Developments in the MVC world


Scott Guthrie, adaptation from
RoR


How does this relate to Visual Studio 2008/.NET 3.5 SP1 Beta?


It doesn't. For now ASP.NET MVC is an out
-
of
-
band Web
downloadable thing. It's bin deployable (meaning you don't HAVE
to put them in the GAC) and you don't even need to tell your
hosting/ISP. They are what they are
-

three DLLs in a folder. Bam.


MVC Preview 3 will not be affected by having SP1 installed. Preview
3 will include newer privately versioned bin deployable builds of the
Routing and Abstractions DLLs. That way these assemblies will not
be affected by the versions in the GAC installed by SP1, because they
will have different version numbers.


MVC vs. “Classic” ASP.NET

Ok, so what is this MVC thingy?


It is an ASP.NET framework that performs data exchange by
using a REST model versus the
postback

model of classic
ASP.NET. Each page is split into two distinct components
-
controller
and
view
-

that operate over the same
model
of
data. This is opposed to the classic code
-
behind model
where no barrier is set that forces you to think in terms of
separation of concerns and controllers and views. However,
by keeping the code
-
behind class as thin as possible, and
designing the business layer appropriately, a good
developer could achieve separation of concerns even
without adopting MVC and its overhead. MVC, however, is
a model superior to a properly
-
done code
-
behind for its
inherent support for test
-
driven development.

Model View Controller Pattern

More Concrete Model View Controller
Pattern

Why use ASP.NET MVC?


It enables clean separation of concerns, testability, and TDD by default.


All core contracts within the MVC
framework are interface based and easily
mockable

(it includes interface based
IHttpRequest
/
IHttpResponse

intrinsics
).


You can unit test the application without having to run the
Controllers within an ASP.NET process (making unit testing fast).


You can use any unit testing framework
you want to
-
do this testing (including
NUnit
,
MBUnit
, MS Test, etc).


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 you to build applications with clean
URLs.


URLs do not need to have extensions within them, and are designed to easily support SEO and
REST
-
friendly naming patterns.


For example, I could easily map the
/products/edit/4

URL to the "Edit"
action of the
ProductsController

class in my project above, or map the
/Blogs/
scottgu
/10
-
10
-
2007/
SomeTopic
/

URL to a "
DisplayPost
" action of a
BlogEngineController

class.


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.

Why MVC is so different?


REST is an architectural pattern that defines how
network resources should be defined and addressed in
order to gain shorter response times, clear separation
of concerns between the front
-
end and back
-
end of a
networked system. REST is based on three following
principles:


An application expresses its state and implements its
functionality by acting on logical resources


Each resource is addressed using a specific URL syntax


All addressable resources feature a contracted set of
operations


The answer to
Url’s

Life, 42.


In the MVC Framework, a URL is seen as the
mean to address a logical server resource, but
not necessarily an ASPX file to parse. So the URLs
employed by the pages of an MVC Framework
application have a custom format that the
application itself mandates. In the end, the MVC
Framework employs a centralized HTTP handler
that recognizes an application
-
specific syntax for
links. In addition, each addressable resource
exposes a well
-
known set of operations and a
uniform interface for executing operations.

What? No more
postbacks
!


The MVC Framework doesn't support classic
postbacks

and
viewstate

and doesn't consider
any URL as the endpoint to a physical server
file to parse and compile to a class. In ASP.NET,
you have a 1:1 correspondence between a
URL and a resource.

Why MVC? (links)


The REST
-
like Aspect of MVC Framework


ASP.NET MVC Framework: An early look


RSS Feed with the new ASP.NET MVC
Framework


MVC, REST, and the Alternative ASP.NET
Framework


ASP.NET MVC framework
-

ready or not


Why MVC?


Very Clean separation of Concerns


Unit Testing


Model that leads you down a maintainable
path (prevent crappy code)


Clean
Urls

and Html

Front Controller


Classic asp.net framework is Page Controller


Map
Urls

to classes/controllers instead of
Pages

Unit Testing


Mock Objects to simulate responses.


Don’t have to go through a page or view


Red/Green Testing

Unit Testing


Create instance of controller


controller.ViewEngine

= new
TestViewEngine
();


Uses Mock Objects

Features


Routing


Controller


View


Dependency Injection


Pluggable


Interfaces for all core contracts

Url


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

Route Examples


<Controller>/<Action>/<
Param
>


http://domain/
Home/CustomerDetail/45


Html.Link

for creating outbound
urls


Html.Link
(<Friendly Name>, <Action>,
<Controller>)

Controller


IController


Execute(
IHttpContext

ctx
,
RouteData

routeData
)


class
MyController

: Controller


[
ControllerAction
] attribute

View


Similar to existing page


No
postback

or view state


Not all page events will fire


Editing forms will be more work


Separate page for viewing, editing, and updating


Ajax will use
UserControls

as update panels but

request still goes through the control

Views


View probably shouldn’t directly access data
sources or services.


Use the
ViewData

which stores the model the
view is rendering


ViewPage
<T>,
ViewPage

Future of the ASP.NET MVC


MVC for the Enterprise


Classic
Asp.Net

for quicker implementations

References


Download the Latest MVC Preview 3

http://www.microsoft.com/downloads/details.aspx?FamilyId=92F2
A8F0
-
9243
-
4697
-
8F9A
-
FCF6BC9F66AB&displaylang=en


ASP.NET MVC Preview 3

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


Quick Start Guides

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


Upcoming Changes in Routing

http://haacked.com/archive/2008/04/10/upcoming
-
changes
-
in
-
routing.aspx


Scott Guthrie’s Post on ASP.NET MVC Framework

http://weblogs.asp.net/scottgu/archive/2007/10/14/asp
-
net
-
mvc
-
framework.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
adnanmasood@gmai
l.com



Visit

www.CodePlex.com/YABE


Visit
www.AdnanMasood.com