Scaffolding in ASP.NET: Dynamic Data Support
ASP.NET Dynamic Data Support is part of the ASP.NET 3.5 Extensions, which have been published as a CTP last week. It provides a scaffolding framework that allows rapid application development based on ASP.NET.
Scaffolding originates from the field of architecture and construction. It is a temporary framework used in the early stages of constructing or repairing a building. In the field of programming scaffolding code is used to provide temporary structure and support until a permanent solution is available. The scaffolding approach has become popular in the Rails MVC Web Framework.
In a nutshell [ASP.NET Dynamic Data] enables you to really quickly build data driven web-sites that work against a LINQ to SQL (and in the future LINQ to Entities) object model - and optionally allows you to-do this without having to build any pages manually.
David Ebbo has published a screen-cast, "which walks through a basic scenario of using ASP.NET Dynamic Data in a simple site using Northwind". In his introduction to Dynamic Data he outlines the basic steps of rapidly developing an ASP.NET application:
- Define your model: this step is not specific to Dynamic Data, but this is where it all starts. You have a database, and you create a model for it. Currently, this is done using Linq to SQL. In the future Linq to Entities will be supported as well.
- Test a scaffolded version of your app: with no additional effort, Dynamic Data lets you run a scaffolded web application on top of your schema. Although this scaffold has a 'standard' UI, it lets you try all CRUD operations on your tables. Furthermore, it has full support for relationships. For instance (assuming Northwind), the Products page would let you navigate to a product's category info, and editing a Product's category would show you a rich drop down with category names (instead of IDs).
- Customize the shared page views: you then get to make changes to the page templates that are used to display the scaffolded views. That, you can make changes that will affect the view of all the tables.
- Customize the specific pages: the next step is to define some custom UI for specific pages. e.g. you could write one of Products and another for Categories. The nice thing is that while you do this, you still get the standard scaffolded view for all the other tables. This lets you create specfic pages at your own pace, while keeping a functional application at every point.
- Annotate your model: you can add attributes to your model to add extra knowledge about certain fields. e.g. you could set a Range on the product's UnitsInStock field so it only allows values between 0 and 50. The UI would then automatically pick this up and validate inputs accordingly. The great thing is that you are keeping this information on your model, and don't need to 'pollute' your pages with it.
- Create custom field templates: all fields in Dynamic Data are rendered via field templates, which are basically user controls. You can also create custom templates, which can be very powerful. e.g. in the above case, you could create a field template that renders a range as a slider control instead of a textbox. Again, your page would pick that up without you having to add this custom UI directly in the aspx file.
The ASP.NET 3.5 Extensions CTP provides Visual Studio 2008 templates for Dynamic Data Web Sites and Web Application Projects. After creating a Web Site or Web Application based on the template you simply add a LINQ to SQL data model, run the project and start browsing and editing the entities of your data model. If this sounds familiar, you're rightly reminded of the Blinq project, which has been discontinued in favour of ASP.NET Dynamic Data.
The Quickstarts, Forum, and David Ebbo's FAQ provide further help and guidance. The first question of the FAQ is: "Will this work with [ASP.NET]MVC?". The answer is yes. You'll have to download the MVC-Toolkit and examine the Blog example, which is included.
In order to customize the behavior of the ASP.NET Dynamic Data application, you'll have to provide metadata to your data model. At the moment the CTP includes a .NET Attribute approach, which might be used to add formatting or range validation to your data model directly. Marcin Dobosz shows how to add custom metadata providers in ASP.NET 3.5 Extensions Preview:
In the recently released ASP.NET 3.5 Extensions Preview this annotation is done by declaring special metadata attributes on the partial classes representing the entities in the model. The initial implementation is a bit limited, however, because the metadata association can only be done using CLR attributes and those attributes have to be declared on the class (even if they pertain to the class's properties)
Marcin explains that the MVC-Toolkit "contains an alternative implementation":
This implementation has the following improvements:
- A pluggable metadata model based on providers.
- New metadata attributes that can be declared on the properties of a special metadata "buddy" class.
- An alternative reference provider implementation based on XML files.
ASP.NET Dynamic Data provides a rich scaffolding framework that allows to develop application prototypes very fast. These prototypes can easily be customized by adding metadata. The scaffolding approach can be replaced by a "real solution", easpecially when used in combination with the ASP.NET MVC Framework, and ASP.NET web pages and Dynamic Data web pages can coexists within a single ASP.NET application.
Dmytro Svarytsevych Oct 30, 2014