Upload
samuel-long
View
213
Download
0
Embed Size (px)
Citation preview
ASP.Net MVCExtensibility, scalability and testability
Andrew Locatelli Woodcock
Andrew Woodcock 15 years development experience, mostly Microsoft
technologies specializing in server-side development with C#, ASP.Net / ASP.Net MVC and SQL Server
Writing code with C# since 2004 For last 2 years have worked as a Lead Developer for
CitiBank in Dublin as a member of Citi’s UI Framework’s team, developing a common ASP.Net MVC and C# based UI framework to underpin Citi’s future banking websites around the globe
About Me
Brief intro to ASP.Net MVC, what it is and what it isn’t
The Model View Controller design pattern Why use ASP.Net MVC: extensibility,
scalability and testability ASP.Net MVC in action: the Fly by Night
Courier website, unit testing and custom view engines
About the presentation
Developed as an open-source compliment to ASP.Net WebForms
Gaining in popularity and now in version 3 Very logical and easy to use – most of the time! In comparison to WebForms, offers:
Full control over HTML and URLs Better scalability: no more View State! Better separation of concerns Extensibility Testability
Brief intro to ASP.Net MVC
Event-driven Stateful Tightly-coupled: data, business logic and
presentation are separated – you can even develop your own View Engine to drive presentation
WinForms for the Web! It does not have a complex event-driven page-lifecycle, it does not mix presentation and business logic in a form
ASP.Net MVC is NOT
The Model-View-Controller design pattern has been around since the late 1970s
Clear separation of concerns Model is data View is presentation of data Controller manages Model and View to
produce output but decouples data from its presentation
Model View Controller
Very easy to develop – most of the time! Logical and consistent – again, most of the time! Stateless: no more page life-cycle Highly testable: more so than ASP.Net WebForms
although some features are still difficult to mock: HttpRequest object for one (as opposed to HttpRequestBase)
Scalable: single round-trip per view, no ViewState Highly extensible: pretty much everything can be
customized
Why MVC?
Renders pure HTML, CSS and JavaScript Designed to REST principles Excellent AJAX support – supports JSON natively Like Ruby on Rails, ASP.Net MVC “favours convention over
configuration”: Controller names end in …Controller Controllers are placed in the Controllers folder, Models in the
Models folder and Views in the Views folder Controller ActionMethod names usually match the name of the View
they generate: Details method will return the Details view, etc. Views reside in known locations: Views / {controller} / {action}
although this can be changed in Routes
More MVC goodness
MVC supports multiple View Engines even in the same project: ASP.Net, Razor, Spark, Nhaml, etc.
Views can use any syntax that their View Engine understands
Views can be strongly-typed to a Model, allowing much of the view to be templated and automatically created
Different Views can use different View Engines View Engines can support new View syntax or use
existing syntax in new ways You can develop your own custom View Engine … as we
shall see …
There’s still more …
ASP.Net MVC is more readily unit testable than ASP.Net WebForms Controllers, Views and Models can all be unit tested reasonably
easily Separation of concerns aids unit testing – Models are POCO for
example MVCContrib TestHelper provides helper methods to setup
Controllers, etc. ready for testing – we’ll see this in action later Models are POCO – very easy to test MVC provides wrapper classes to better support mocking intrinsic
objects: HttpContextWrapper, HttpRequestWrapper, etc. Still not perfect though due mainly to hang-overs from ASP.Net
WebForms – very hard to mock the HttpRequest static class for example.
Testing, testing
Let’s get our hands dirty! Deep dive into MVC via the Fly by Night
Couriers website Hands-on MVC Some testing And some custom View Engine goodness …
Fly by Night Couriers
ASP.Net MVC 3: http://www.asp.net/mvc/mvc3 Moq mocking framework:
http://code.google.com/p/moq/ - no more record-replay!
MVCContrib TestHelper: http://mvccontrib.codeplex.com/releases/59313/download/198550 - easy initialization of Controllers and their intrinsic objects, etc.
Log4Net: http://logging.apache.org/log4net/ activePDF WebGrabber WBE: http://www.activepdf.com
Software used
This presentation and the code samples will be summarized on my blog over the next few weeks:
http://www.andrewlocatelliwoodcock.com
Second chance