ASP.NET Dynamic Data
An Initial Look at .NET Dynamic Data
Introduction

.NET Dynamic data (.NET 3.5 SP1) is a framework that provides dynamic web page templates to support CRUD operations on classes in a Linq to SQL or Linq to Entities model. Allowing you to rapidly develop a data driven web application.

My concern with Dynamic Data was that it would not easily support custom functionality. However I found that Dynamic Data was adequately extensible enabling you to change existing templates, define custom pages and custom field controls (field templates) to achieve the required functionality.

Another neat part of Dynamic Data is that it applies field level validation automatically based on metadata that you define using custom attributes against the model classes.

Meta data is also used to change the way dynamic data processes a table or field, for example it can be used to configure the display name of a table or configure the field template used for a data field.

Creating a Dynamic Data Project

It’s very easy to get started with Dynamic Data, depending on your model type; simply add a new Dynamic Data Web Application or Dynamic Data Entities Web Application project in Visual Studio 2008:

Add a reference to the assembly containing your Linq to SQL/Entities model and register the type of the model in the global.asax file. Setting ‘ScaffoldAllTables’ to true causes the template pages to be configured to all entities in the model :

model.RegisterContext(typeof(MyEntities), new ContextConfiguration() { ScaffoldAllTables = true });

Compile and run the website and see just how much you get for free.

A Quick Look at Extensibility

One thing you will notice very quickly about dynamic data is that it’s not ‘magic’ and that the way things hang together is fairly transparent.

Have a look at the standard PageTemplates under the new project and you will see that apart from the DynamicDataManager they use standard .NET controls and also notice that you have complete control over the page template itself. You can see how each page really is just a template which can support CRUD operations across all the types in the model.

The code behind; in say the Edit page; is very simple, first the details view on the page is registered with Dynamic Data and on page load the MetaTable instance is used to configure the data source. The MetaTable contains the definition of the underlying table being edited which you can utilise to implement custom functionality. You can tweak the existing page templates or add your own pages for specific types by creating a new page with the relevant table name under the CustomPages folder.

The existing FieldTemplates are a good reference to create your own templates.  Field templates derive FieldTemplateUserControl and this base class contains the metadata on the underlying field and its parent data model and also the state of the current row being edited.  To change the field template used by a field all that is required is to decorate the field in the model metadata.

Conclusion

Though in my opinion Dynamic Data is probably best suited to developing prototypes or small data driven applications with limited business processing, I was impressed with the ease at which a Dynamic Data site can be extended with custom functionality.

Also, the nature of Dynamic Data means that theres no reason why you couldn't use it for parts of your application, utilising it for system administration screens for example.

One caveat to bear in mind is that its use of Entity/SQL data sources does mean that you are very tightly coupled to your data context.

I hope to expand on this with some more specific posts on Dynamic Data soon.

A good starting point: http://www.asp.net/dynamicdata/