150 likes | 353 Views
ASP.Net MVC. Extensibility, scalability and testability Andrew Locatelli Woodcock. About Me. Andrew Woodcock 15 years development experience, mostly Microsoft technologies specializing in server-side development with C#, ASP.Net / ASP.Net MVC and SQL Server
E N D
ASP.Net MVC Extensibility, scalability and testability Andrew Locatelli Woodcock
About Me 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 the presentation 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
Brief intro to ASP.Net MVC • Developed as an open-source compliment to ASP.NetWebForms • 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
ASP.Net MVC is NOT 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
Model View Controller 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
Why MVC? 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.NetWebFormsalthough 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
More MVC goodness • 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
There’s still more … 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 …
Testing, testing ASP.Net MVC is more readily unit testable than ASP.NetWebForms Controllers, Views and Models can all be unit tested reasonably easily Separation of concerns aids unit testing – Models are POCO for example MVCContribTestHelper 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.NetWebForms – very hard to mock the HttpRequest static class for example.
Fly by Night Couriers 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 …
Software used • ASP.Net MVC 3: http://www.asp.net/mvc/mvc3 • Moq mocking framework: http://code.google.com/p/moq/- no more record-replay! • MVCContribTestHelper: http://mvccontrib.codeplex.com/releases/59313/download/198550- easy initialization of Controllers and their intrinsic objects, etc. • Log4Net: http://logging.apache.org/log4net/ • activePDFWebGrabber WBE: http://www.activepdf.com
Second chance This presentation and the code samples will be summarized on my blog over the next few weeks: http://www.andrewlocatelliwoodcock.com