6 Steps to N-Tier ASP.NET Dynamic Data

Dynamic Data websites use a flexible template-based architecture that makes working with data driven websites a much more fluid process for developers.

For further information check Scott Hunter's detailed post: Breakdown of Dynamic Data Features.

Moving the data context from the web layer to a lower level will allow you to reuse the LINQ to SQL data context as well as any other logic or wrappers you create around the data model.

Step 1: Create Dynamic Data Website

To begin create a Dynamic Data website.

 

Step 2: Create Data Layer

The next step is to create a new class library. This project will house your LINQ to SQL data context, the meta-data classes and wrappers for your data access.

 

Step 3: Add References

When you are using the LINQ to SQL class in the Dynamic Data website you have a pre-established reference setup for System.ComponentModel.DataAnnotations. You must create a reference in the class library in order to have access to the attributes used for the meta-data classes.

Since the DataAnnotations DLL is not loaded in the GAC, you will have to search your system for “System.ComponentModel.DataAnnotations.dll” and copy this file into your project.

Here the DLL is in the Dependencies folder* and then the project is referenced to this DLL.

 

*Often it’s a good idea to create a Dependencies class library of its own to act as a source control container for any extra DLLs. This will keep you from getting your references tangled up for larger projects.

Step 4: Create Data Context

Next you will create the data context. The process is simple: create a LINQ to SQL file and drag on the tables and views you want to model.

 

This example only brings in one table...

 

Step 5: Create the Metadata

The Dynamic Data engine will choose the appropriate field template based off the database type of each field. If you would like override this functionality and use a custom field template then you must add meta-data information on top of the existing models.

 

As seen on line five, make sure you add the using statement to bring in the DataAnnotations namespace.

After that, creating the meta-data is easy. Simply create a partial class named the same as your model class and add the MetadataType attribute so the Dynamic Data engine knows where to find the meta-data.

The meta-data class exists only to hang the UITemplate attributes off the object properties. You don’t even need to know about the return type of the property at this point.

Normally the Dynamic Data engine will map the CreatedOn field to the DateTime field template. Here we want to re-route the engine to use the Text template so we can verify the application is threaded together correctly.

Step 6: Update the Website

The next step is to update the website so it can see the data layer. Right click on the web project and choose Add Reference.

 

In order to keep this demo simple we’ll just update one of the stock field templates to verify that we are getting the data context and all the meta-data from the lower-level project. When you setup the meta-data you told a field that would normally map to a DateTime field to go to a Text field template. Since by default both the DateTime and Text field templates just emit the field data on the screen in the read-only view, you’ll update the Text template.

 

Open the Text field template and simply add the word “text” (or really anything for that matter) to the field template.

 

The last part of the process is to update the Global.asax

 

Note that (at least on my machine) IntelliSense would not bring up the data context class name once I typed DataLayer. – so you may have to just type the class name. Once the class was there the coloring came in as expected though.

Finally you can run the website to verify that the CreatedOn field is being rendered using the Text field template.

 

Considerations

Obviously the application created in this post is not a true n-tier application, but this shows you what you need to know for you to architect your applications as appropriate.

kick it on DotNetKicks.com

Published Tuesday, July 1, 2008 3:28 PM by craigshoemaker
Filed under: ,

Comments

# re: 6 Steps to N-Tier ASP.NET Dynamic Data

Wednesday, July 2, 2008 12:51 PM by Tom Wayson

Great post Craig.  Finally a dynamic data example not using LinqToSql!  We're an Oracle shop, so LinqToSql is dead to me.  I'll have to try this with our Subsonic generated DAL.

Tom

# re: 6 Steps to N-Tier ASP.NET Dynamic Data

Wednesday, July 2, 2008 1:47 PM by craigshoemaker

Hey Tom - thanks for the kind words. I just want to point out though that I do use LINQ to SQL in this post :( Step 4 has me doing the LINQ to SQL file.

Rob Conery has blogged a little about his progress in getting SubSonic to play nice with Dynamic Data. You can check that out here: blog.wekeroad.com/.../subsonic-update-on-3-0-plans

The technology is really new so I understand your frustration with it not being more pervasively supported.

For a little bit more information, Scott Hunter details what it takes to get a data provider hooked up with Dynamic Data. You can listen to the interview I did with him here: getpixel8ed.com/.../dyndata

HTH,

Craig

# re: 6 Steps to N-Tier ASP.NET Dynamic Data

Tuesday, March 17, 2009 9:30 AM by rtpHarry

Thanks for this post craig, was just starting to scratch my head on what I needed to do to make my class library play nice with my dynamic data admin site.

# re: 6 Steps to N-Tier ASP.NET Dynamic Data

Tuesday, March 17, 2009 9:36 AM by rtpHarry

Oh also I just wanted to add that I didnt have to do the step of adding System.ComponentModel.DataAnnotations.dll in, it was already available in the list. Dont know if this changed as of SP1?

# re: 6 Steps to N-Tier ASP.NET Dynamic Data

Tuesday, March 17, 2009 9:52 AM by craigshoemaker

Yep that is an SP1 addition