Pages

Wednesday 18 June 2008

ASP.NET Dynamic Data - New Feature in ASP.NET 3.5 SP1


After attending NxtGenUG Fest 08 last week in my old office at Microsoft UK HQ, Thames Valley Park, I was particularly excited to hit the ground running with a topic presented by Dave Sussman, ASP.NET Dynamic Data.

This is a new feature in the yet to be released SP1 for .NET 3.5. (A beta is available here - this is pre-release so use on a VM or spare machine)

Dynamic Data is a scaffolding framework - what does this mean? Well this is how Scott Guthrie describes it:

.NET 3.5 SP1 adds support for a rich ASP.NET data "scaffolding" framework that enables you to quickly build functional data-driven web application. With the ASP.NET Dynamic Data feature you can automatically build web UI (with full CRUD - create, read, update, delete - support) against a variety of data object models (including LINQ to SQL, LINQ to Entities, REST Services, and any other ORM or object model with a dynamic data provider).

SP1 adds this new functionality to the existing GridView, ListView, DetailsView and FormView controls in ASP.NET, and enables smart validation and flexible data templating options. It also delivers new smart filtering server controls, as well as adds support for automatically traversing primary-key/foreign-key relationships and displaying friendly foreign key names - all of which saves you from having to write a ton of code.


I think of this scaffolding framework as DataBinding 2.0 - to get a good idea of what Dynamic Data is, I recommend watching this video from David Ebbo.

Now, the awesome thing about Dynamic Data is that it doesn't have to work on just a Data Model generated from a database or LINQ datasource. It can work against a business object using the ObjectDataSource approach - this is an amazing ability to save time getting features such as validation, column typing, display names and descriptions etc piped into your application for free.

For an integration project I have been working on where I generate business objects using an ASP.NET build provider, and then databind them against custom controls, I can now get rid of the following things:
  • A proprietary ValidationFramework
  • Custom column type mappings
  • Custom attributes on my business objects
  • Custom BoundFields
  • Custom DetailsView and GridView controls (which use the mappings and custom BoundFields)
...and instead I simply modify the build provider to decorate my class with the expected Dynamic Data attributes, and use the "DynamicObjectDataSource" from the latest samples pack.

Example business object:


public class NewData {
  [RegularExpression(@"[A-Z].*", ErrorMessage="The name must start with an upper case character")]
  [Required]
  [DisplayName("The name")]
  public string Name { get; set; }

  [Range(0, 1000)]
  [UIHint("IntegerSlider")]
  [DefaultValue(345)]
  public int Number { get; set; }

  [DataType(DataType.Date)]
  [UIHint("DateAjaxCalendar")]
  [Required]
  public DateTime Date { get; set; }
}


Here is the ASPX declaration:

<asp:ValidationSummary ID="ValidationSummary1"
  runat="server"
  EnableClientScript="true"
  HeaderText="List of validation errors" />

<asp:dynamicvalidator runat="server"
  id="DetailsViewValidator"
  controltovalidate="DetailsView1"
  display="None">

<asp:DetailsView ID="DetailsView1"
  runat="server"
  AllowPaging="True"
  AutoGenerateInsertButton="True"
  DataSourceID="ObjectDataSource1"
  EnableModelValidation="true">
</asp:DetailsView>

<asp:DynamicObjectDataSource ID="ObjectDataSource1"
  runat="server"   DataObjectTypeName="DynamicDataExtensionsSample.NewData"
  InsertMethod="Insert"
  SelectMethod="Select"
  TypeName="DynamicDataExtensionsSample.AggregateData">
</asp:DynamicObjectDataSource>


...from that, a DetailsView control loads showing the correct data, using the correct client side controls (which can be completely customised) based on the data type, with enforced validation using automatically generated validators.

Now for some handy links:

Dynamic Data Website
Dynamic Data Forum
MSDN Documentation


This really is an awesome new technology. Expect to see more about this on my blog soon.

Many thanks to David Ebbo from the ASP.NET team for helping me out with Dynamic Data.

1 comment:

Anonymous said...

Now that the Verizon i - Phone has made its way onto the market, Verizon can look back and
boast about some of its contract features that others do not have.
However, if the cut is slightly bigger or it is a sidewall puncture, tire repair is not a practical solution and
the flat tire needs to be replaced. Also, the height of the trailer hitch from the ground and the tow
vehicles hitch receiver should be within a few inches of each other.



Also visit my blog post - salvage auto sales