Taking a look at Dynamic Data

One of the best things about being a programmer is new technologies,  taking a look at exciting new approaches to everyday tasks.  The .NET world is continually growing, in fact it was only 6-months ago that the 3.5 Framework was released giving you new tools such as LINQ, WPF, and Silverlight.  Now comes ASP.NET 3.5 Extensions Preview and you can add to the list Dynamic Data Controls. Dynamic Data Controls will enable you to build data driven website's that will work against a LINQ to SQL and LINQ to Entities object model.  With a fully functional website built on top of the MVC framework you can optionally override or customize any of the view templates giving you full control over our website.

Dynamic data works with the standard data controls enabling them to automatically support foreign key relationships,  giving you a friendly name display of foreign key values along with built in UI validation support based upon the constraints set in your data-model.

Once you download the new Dynamic Data controls (you can down load them from here ) unzip them to your favorite directory.  Inside the directory you will find a series of DLL's, Zip files, CMD files and a readme.  Inside the readme you will find instructions on setting up your environment and your first project of which both of these we will go over in this blog.  So now its time to get to the fun.


For this example you will need

Visual Studio 2008
SQL Express
ASP.NET 3.5 Extensions Preview
Northwind database

The examples here will be in VB.NET, however you can use C# if you so desire.


First you need to install the Dynamic Data controls.  To do this you can either open up a command window and navigate to the directory in which you unzipped the files, or you can simply double click on the appropriate one to install depending upon your system.  You will notice that there is support for both 32-bit and 64-bit operating systems.

Your first Project

Launch Visual Studio 2008, and click on File > New > Website and choose Dynamic Data Website (Preview).



Directory Structure:
You will notice that you have a pre-built website complete with a Dynamic Data directory structure.  The key area's I want to point out here are the CustomPages, FieldTemplates, and PageTemplates directories. 



FieldTemplates:These are the templates which are used to render each of the field types with in your datamodel.  You will notice the standard data types such as Boolean, DateTime, Integer, and Text.  You should also notice templates to handle the rendering of ForeignKey and Children relationships.  What you should also notice is that the templates for Image and XML data types are missing.  As of this release they are not installed by default; however you can create your own templates to control the rendering of these datatypes.  You can also override/customize the existing templates depending upon your needs.

PageTemplates:These are the default page templates to display / edit the data. 

CustomPages:This is where you will put any custom pages or override any pages in the default PageTemplates directory.




Data Model:

Add your database to your project.

Add a LINQ to SQL Class


Add the desired tables to your datacontext.


Now its time for the magic.  Open your global.asax and uncomment the line:

'    model.RegisterContext(GetType(YourDatacontext), New ContextConfiguration() With {.ScaffoldAllTables = False})

Then replace "YourDatacontext" with the name of the datacontext we just created and set ScaffoldAllTables = True.  Setting this value will build the dynamic pages based upon the data model.

   1: <%@ Application Language="VB" %>
   2: <%@ Import Namespace="System.Web.Routing" %>
   3: <%@ Import Namespace="System.Web.DynamicData" %>
   5: <script RunAt="server">
   6: Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
   7:     Dim model As New MetaModel
   9:     ' Uncomment this line to register your data context with the Dynamic Data engine.
  10:     ' Only set ScaffoldAllTables = true if you are sure that you want all your tables
  11:     ' to support a scaffold (i.e. templated) view.
  12:         model.RegisterContext(GetType(NorthwindDataContext), New ContextConfiguration() With {.ScaffoldAllTables = True})
  14:     routes.Add(New DynamicDataRoute("{table}/ListDetails.aspx") With { _
  15:         .Action = PageAction.List, _
  16:         .ViewName = "ListDetails", _
  17:         .Model = model})
  19:     routes.Add(New DynamicDataRoute("{table}/ListDetails.aspx") With { _
  20:         .Action = PageAction.Details, _
  21:         .ViewName = "ListDetails", _
  22:         .Model = model})
  24:     'routes.Add(New DynamicDataRoute("{table}/{action}.aspx") With { _
  25:     '    .Constraints = New RouteValueDictionary(New With {.Action = "List|Details|Edit|Insert"}),
  26:     '    .Model = model})
  27: End Sub


Now run the site.


Drilling down to the Product details you will see that the friendly names for the categories are in both the drop down list, and the grid view.  You will also notice the ability to edit and delete.

So now go off and have some fun with this.  Next time we will look at adding support for the Image data type based upon Scott Hunter's sample and building our own custom pages.

So until next time, have fun and I'll see ya on the flip side.

David Yancey


Comments have been disabled for this content.