VS2010: Web Development
By Tim Anderson
Using Visual Studio 2010 to build rich Internet applications.
HardCopy Issue: 52 | Found In: Visual Studio 2010 | Published: 19/05/2011 | Last Revision: 19/05/2011
In the uncertain world of software development, one thing is for sure: the Internet is not going away. Web applications are inherently cross-platform, with minimal deployment hassles and huge flexibility, while the advent of Rich Internet Applications (RIA) built with Adobe Flash or Microsoft Silverlight blurs the boundaries between the desktop and the Web. A new edition of Visual Studio, called LightSwitch, lets you build Silverlight database applications that run in or out of the browser. There are also advances in the ASP.NET platform which now comes in two flavours: the old-style Web Forms for rapid development, or the new ASP.NET MVC for leaner code and greater testability.
In January 2011, Microsoft released a new version of its Web server designed specifically for development. IIS Express 7.5 improves on the old ASP.NET development server by supporting the full features of IIS, but with the same zero-install convenience. You can even distribute it with an application if you need a lightweight Web server. Microsoft’s Web Platform Installer makes IIS Express 7.5 and other optional features easy to install.
So what is the difference between Microsoft’s three Web application models?
ASP.NET Web Forms
ASP.NET Web Forms is the original framework for .NET Web applications, introduced at the same time as .NET itself around ten years ago - although of course now much enhanced compared to its first implementation. Web Forms have a couple of advantages. First, the framework handles state management on your behalf, making coding a Web application closer to the experience of coding a desktop application. The values of fields persist automatically when the page is refreshed. Second, the maturity of the framework means excellent third-party support, so you can easily find the components you need. Web Forms is an excellent framework for rapid development.
Web Forms gets worthwhile improvements in version 4.0. The first thing developers will notice is that new Web Application and Web Site projects contain more code for getting started, though there is also an option for empty projects if you like lean and mean. Web Application projects include membership support by default, for user management. Both project types include master pages, jQuery Ajax scripts and CSS classes so that you can quickly modify existing code to create a basic Web application.
The new Chart control lets you add a wide variety of 2D and 3D chart types to a page. It is now easier to do fine-grained management of view state (the hidden state data which is transmitted with a page) by disabling it for a page and then enabling it for only those controls which need it. Another new feature is better framework support for page routing. This is the technique of mapping URLs to physical pages in order to improve the usability of a site.
Web Forms also have better standards support. You can specify that all server-side control rendering conforms to XHTML 1.0 strict markup, making them easier to style with CSS. You can also control how client-side ID values are generated for data-bound controls. The result is HTML that is easier to work with on the client, for rich AJAX applications.
ASP.NET MVC
Although Microsoft is continuing to evolve Web Forms, the framework is showing its age. Web Forms by design wraps the Web model to make it somewhat desktop-like, but this now counts against it among developers for whom the Web has always been the primary platform. The complex Web Forms page lifecycle along with the burden of view state data can get in the way.
ASP.NET MVC is Microsoft’s next generation Web framework. MVC stands for Model View Controller, an architecture in which the Model (behaviour and data) of an application is separate from the View (what the user sees) and the Controller (managing the interaction). That said, studying MVC theory may not the best way to get started with ASP.NET MVC. Another way of looking at it is as a lightweight framework based on URLs and templates. In ASP.NET MVC, the URL is not the address of a physical page, but user input into the application. Handlers process the URL, extracting arguments, getting the results and feeding them into the templates.
One of the benefits of this approach is that it is more amenable to testing. Tests can easily simulate input and parse the results, enabling test-driven development. There are also performance advantages. The popular developer resource site Stack Overflow is one of the best-known public ASP.NET MVC applications, and succeeded partly because of its fast response.
ASP.NET MVC has been adopted enthusiastically: “One of the great aspects of ASP.NET MVC is how lean and clean the architecture is - there is so little background noise compared to Web Forms,” says Howard van Rooijen of EMC; while a recent paper on software development trends from ThoughtWorks, a global software development company, lists ASP.NET MVC as a tool ready for immediate adoption.
ASP.NET MVC is released under an open source licence. Version 1.0 appeared in March 2009. Visual Studio 2010 is the first version to be built with ASP.NET MVC in mind, and comes with ASP.NET MVC 2.0. In January 2011, Microsoft released ASP.NET MVC 3.0, which is recommended as a downloadable upgrade.
Starting an ASP.NET MVC 3.0 Web application.
New features in version 2.0 include declarative form validation both on the server and on the client, asynchronous controllers for calling Web services with unpredictable response times without blocking performance, and a technique called Areas which lets you divide an application into isolated sub-projects. Version 3.0 adds a new view engine called Razor, which once learnt is compact and highly productive for coding templates. There is also deeper jQuery integration, better form validation, and partial page output caching.
When you start an ASP.NET MVC project in Visual Studio, it prompts you to create a test project at the same time. Visual Studio also creates a skeleton application, including sample pages and ASP.NET Membership support. Working with ASP.NET MVC tends to be more code-oriented than with Web Forms, but nevertheless it is possible to use ASP.NET controls in MVC projects.
Silverlight
The debate is raging: as Web applications increase in importance relative to desktop applications, is it better to use advanced HTML and JavaScript for a rich user interface, or a plug-in that does its own rendering and code execution? Silverlight is Microsoft’s implementation of the plug-in approach. In some ways it competes with Adobe Flash, though Microsoft is now positioning Silverlight as mainly for Windows (though it also works on a Mac), and supporting HTML 5 as the right choice for broad-reach clients.
Visual Studio LightSwitch
LightSwitch is both a new edition of Visual Studio and a new application framework which installs into an existing installation. It is free for users of Visual Studio Professional or higher.
But what is LightSwitch? The quick answer is that it is a rapid application builder for database applications. Using LightSwitch, you can define database tables using simple visual tools, set up relationships, and create screens for viewing and entering data. Much of the code is generated for you, but you can add your own custom code in C# or Visual Basic. LightSwitch builds Silverlight applications, and there are deployment options which range from a Windows desktop application running entirely on a single machine, to a Web application hosted on Windows Azure or your own Windows server.
But this brief description does not do justice to LightSwitch. Building relational database applications has an inherent complexity, and LightSwitch does a great job of hiding that complexity while still encouraging sound design. LightSwitch builds three-tier applications, with separate tiers for the database, server application, and Silverlight client. Using a model-driven approach, LightSwitch generates most of the code for you, so that even amateur developers can build applications. Professionals should also take a look though, because you can build capable multi-tier applications with relatively little effort.
If the original Visual Basic made programming for Windows approachable, then LightSwitch does the same for multi-tier applications, complete with an option for cloud hosting. It is a remarkable achievement.
The downside of LightSwitch is that there are a few gaps in this first version, such as reporting, and that its innovative approach takes a while to learn. The Silverlight dependency means it is best suited to Windows environments, and not a good choice if you need to support Apple or Android mobile devices. Even Windows Phone is not supported in the first version, though since it is Silverlight based it would make sense for Microsoft to add Windows Phone support in a future version.
Silverlight lets you create a Web application using a lightweight version of Windows Presentation Foundation (WPF), driven by client-side .NET code written in C#, Visual Basic, or other languages. The Silverlight runtime is supported by Microsoft on Windows and Intel Mac, while an open source Linux version is maintained by Novell’s Mono project. Silverlight is currently installed on around 70 per cent of computers active on the Internet, according to figures from RIAStats, and slightly more than that in the UK. The plug-in is generally easy to install, though it may be blocked in some locked- down business environments.
There are several advantages to the Silverlight approach. First, it is a multimedia player, ideal for video or animations. Second, it offers a rich and predictable surface for designers, enabling elegant user interfaces that would be hard to achieve in HTML. Third, the ability to run .NET code means both fast performance and a familiar platform for developers. Fourth, Silverlight can also run out of the browser as a desktop application, and in the version 4.0 has support for COM Automation, enabling interaction with Microsoft Office on Windows. In the forthcoming version 5.0, platform invocation of native code will be supported for fully trusted Silverlight applications.
Silverlight is backed by WCF RIA Services, a server-side framework which simplifies the hook-up of a client application to data and authentication. This is the first release of Visual Studio to fully support Silverlight, with a visual designer and several Silverlight project types. A strong feature of Silverlight projects is that you can seamlessly debug client and server code in the same solution.
Creating a data relationship in Visual Studio LightSwitch.
Silverlight has progressed rapidly from version 1.0 in September 2007 to Silverlight 4.0 today and 5.0 coming soon. The 4.0 release is the first that is truly ready for business use. Highlights in a long list of new features include printing support, a RichTextBox with editing, localisation with bi-directional text, improved databinding, Webcam and microphone support, embedded HTML and access to the notification area when out of the browser, and the ability to read and write files when a trusted out-of-browser application. The range of supplied controls now includes business essentials such as a DataGrid and DatePicker. With Visual Studio 2010 the tooling is now mature, with developer-oriented design support in the IDE, and compatibility with Expression Blend for designers.