Ready, steady, go!

This article was originally published in VSJ, which is now part of Developer Fusion.
Even though it has been getting coverage for the past couple of years, ASP.NET 2.0 is a brand new platform. The great news is that while it isn’t built on a new system infrastructure and framework, it does look like the ASP.NET we have all been waiting for.

If you look at ASP.NET 2.0 from a broader perspective, and after having carefully dissected most of its core classes, you see two main characteristics: an improved design of the overall framework (more abstraction and intermediate classes), and a bunch of brand new features such as master pages and themes.

Refactoring and extensibility are the two key words of ASP.NET 2.0. Several subsystems have been refactored to expose the same functionality on top of a brand new implementation and underlying API. Furthermore, this new API contains many more hooks than in previous versions, and basically gives you the possibility to get into the nerve-centre of the platform. Extensibility means that what was a take-or-leave feature in the past has now been downgraded to the rank of a mere default setting. For example, you can now define custom session state data stores, custom authentication providers, custom build components, and custom expressions in the markup.

A full bag of new server controls make their debut, and building new custom controls is easier than ever thanks to a more rational and rich fabric of intermediate classes.

But what’s really new in ASP.NET 2.0?

Visual Studio design facilities
As well as providing coding facilities Visual Studio also provides design facilities for ASP .NET pages

New capabilities

ASP.NET 2.0 can boast a long list of new features and capabilities ranging from themes to master pages, user profiles, Web Parts, and smart data-bound controls. The provider model is a thread that ties together various features of the platform – membership, role management, session state storage, compilation, site mapping, and page personalisation. It supplies a model for you to unplug the system’s modules and plug your own ones in. For example, if you don’t like how the session state is managed, or want to store it on a different persistent data store (e.g. Oracle), you write and register a provider and proceed. The outermost interface of the provider shows a fixed contract that the ASP.NET runtime knows and fulfills. ASP.NET offers the same service to applications, even though the “algorithm” behind the service is implemented in an external component. The ASP.NET provider model is the ASP.NET implementation of a well-known design pattern – the strategy pattern.

With themes, developers can easily give the whole site a consistent and hopefully appealing user interface. More importantly, all the visual settings can be exported from one application to the next, as easily as you move a bunch of files.

Overall, building rich and similar-looking pages is a much more approachable task in ASP.NET 2.0 than it was with previous versions of ASP. With master pages, for example, you build pages based on an existing template of code and markup. The master is an incomplete page that contains replaceable regions within a fixed layout. Derived pages take care of filling those empty regions thus making each page similar but not identical to the master.

In ASP.NET 2.0, you also find a built-in mechanism for managing user-specific profile information. You define a data model and ASP.NET transforms it into a runtime class. An instance of this class is bound to each request based on the identity of the logged user. It is up to the page’s interface to let users express preferences (e.g. theme, colours, layout, font, links). As long as these preferences are stored in the user profile class, ASP.NET takes care of persistence.

Web Parts provide an infrastructure for creating web applications that can handle rich as well as large amounts of content. Using Web Parts, you can build sites that enable users to select and receive only the content they want. Web Parts are container components that aggregate different types of content. As such, they are particularly useful for creating portal pages. You can think of a Web Part as a window of information available within the page. Users can close, minimise, or restore that window. Web Parts look a lot like SharePoint Web Parts and, in fact, they will become the building blocks of the next version of SharePoint. So if you’re using SharePoint today, taking a close look at Web Parts cannot be further delayed.

Data binding in ASP.NET 1.x was a bit like the classic rabbit from a hat for developers – a big surprise and an entertaining feature, which made the task of building data-driven pages easy. At least, this is what most of us thought in the beginning. After months of routine work, developers realised that data binding was suffering a major disease – code obesity. Too much boilerplate code is required in ASP.NET 1.x to handle relatively simple situations. In ASP.NET 2.0, a smarter family of data-bound controls, and a brand new breed of components named data source controls reduce the amount of code required for data-driven pages. In some special (but still realistic) cases you can reduce the version 1.x code by 70%, and even have codeless pages where most of the logic is expressed declaratively.

Finally, ASP.NET 2.0 includes a much-requested feature that simplifies and facilitates the deployment of web applications – site precompilation. Site precompilation was possible in ASP.NET 1.x through third-party or homemade utilities. In version 2.0, though, it has the status of a system tool, fully supported by the framework. Site precompilation offers two main advantages. First, requests to the site don’t cause any delay because the pages and code are precompiled to assemblies. Second, sites can be deployed without any source code, thus preserving and protecting the intellectual property of the solutions implemented. Precompilation comes in two forms: in-place and deployment. In the former case, you run the tool once the application is deployed. In the latter, the tool creates an assembly-only image of the application to be packed in a ZIP or MSI file for actual deployment.

Visual Studio project wizard
The project wizard makes it possible for beginners to build ASP .NET 2.0 web sites

What will never be the same

There are features in ASP.NET 2.0 that constitute a breakthrough, and make real things that were simply impossible with previous versions. Only by fully understanding and embracing the ASP.NET 2.0 programming model can developers take advantage of these features and build ‘pure’ ASP.NET 2.0 applications. The list of “breakthrough” features is personal, and depends on what you are trying to do and what you value. The point is that a new capability may or may not be a feature that makes it possible to achieve otherwise impossible results. Master pages, for example, are a great option, but they are not fit for all situations. It’s great to have them available, but you can still write effective applications without. The same can be said for themes or user profiles.

Here’s my personal selection of three ‘hot’ features that will make ASP.NET 2.0 pages unique, and deserving the “Designed for ASP.NET 2.0” logo, if it only existed:

  • $-expressions and build providers
  • Codeless pages
  • ObjectDataSource
The ASP.NET compilation model has been refactored to use ad hoc components to process resources with a given extension. What was limited to .aspx and a few more resources in version 1.x, is now available also to custom resources. For example, you can drop an XML file to the App_Code folder and install a custom component that generates a class, or perhaps an image, dynamically out of it. Application specific resources can be mapped to classes promoting an overall OOP approach and a better design.

In ASP.NET 2.0, you can use $-prefixed expressions to insert dynamic expressions declaratively in the page – connection strings, resources, application settings. Also this aspect of ASP.NET is fully customisable, and new expressions can be created. $-expressions are processed at parse time. The use of $-expression is strictly bound to fully declarative, codeless pages. If you’re not a fan of declarative programming, skip these paragraphs. If you think that everything in a page should be programmatically controlled, obviously this feature is not for you. For all the others, codeless pages filled with expressions and smart controls are an excellent way of arranging pages quickly and quite effectively. I’m not saying that this is what you should be doing all the time, but this is a pretty unique capability.

The canonical example of a codeless page is a page that uses a data source control – for example, SqlDataSource or ObjectDataSource. ObjectDataSource is particularly interesting. You must have a data access layer (DAL) to take advantage of it, which is common in large applications. Once you connect ObjectDataSource to the DAL you can forget about ADO.NET containers like DataSet and DataTable. Working with collections of entity classes and binding this data to grids becomes easy.

Designing simple applications was easy enough already in ASP.NET 1.x. In ASP.NET 2.0, simple applications are even easier to write, and often require a very limited amount of code. However, designing applications of some complexity is also significantly easier because of the new features – and the ObjectDataSource control is a key one.

The Bottom Line

ASP.NET 2.0 is a major upgrade to the platform, even though it doesn’t introduce any new or revolutionary programming paradigms. Moreover, ASP.NET 2.0 is a milestone for application architects as well as developers. Many of the classes have been reworked and several new controls have been added for the sake of productivity. As a result, new practices emerge as best practices, and new programming techniques are available to architects and lead developers. New system features provide native solutions to known issues with earlier versions.

ASP.NET 1.x was clearly a quantum leap in the right direction, but developers soon realised that it was only the first step of a clearly longer road. ASP.NET 2.0 gets us further on the way to truly substainable real-world web development.


Dino Esposito is a Solid Quality Learning mentor and a prolific author. He is the author of the two-volume Programming Microsoft ASP.NET 2.0 (Microsoft Press, 2005), he writes the Cutting Edge column for MSDN Magazine, and he regularly contributes to a variety of developer magazines. He is also a regular speaker at Bearpark’s annual DevWeek conference in London – the 9th annual event takes place on 20–24 February 2006.

You might also like...

Comments

About the author

Dino Esposito United Kingdom

Dino Esposito is an instructor for Solid Quality Mentors, and a trainer and consultant based in Rome. He is the author of various books, including Windows Shell Programming, Instant DHTML Script...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry