Thursday, December 13, 2007

Does ASP.NET Dynamic Data == ASP.NET on Rails?

I found this to be a very odd name for this feature of the ASP.NET 3.5 Extensions.  I don't know why, but it sounds so old school.  Didn't we start getting dynamic data with asp and VBScript?  CGI/PERL?

Anywho, since the the name was so misleading, I thought I'd try and clarify it for myself.  Here is how the documentation from www.asp.net describes the feature:

ASP.NET Dynamic Data provides the Web application scaffolding that enables you to build rich data-driven Web applications. This scaffolding is a mechanism that enhances the functionality of the existing ASP.NET framework by adding the ability to dynamically display pages based on the data model of the underlying database, without having to create pages manually. Dynamic Data infers the table to display and the view to create from the URL of the request.

Sounds similar to the plumbing that Rails provides.  Build your model.  Execute a tool that builds the scaffolding, and viola, you've got a model, a view, a controller, and some unit tests.

To create a ASP.NET Dynamic Data project, you need to have Visual Studio.NET 2008, any of the flavors with Visual Web Developer, and you must have installed the ASP.NET 3.5 Extensions Preview.  Open Visual Studio, and create a new web site using the new Dynamic Data Website template:

add project 

Sticking to the "off by default" philosophy, the templates that are generated for each table are not enabled in the web.config after creating the project.  To fix this, you need to locate the DynamicData configuration section and change the enableTemplates attribute to "true".

The next thing you need to do is create your Linq to SQL classes the project.  For this example, I'm going use Northwind again, and am going to put all the tables into my entity model.  See my previous post about creating simple Linq to SQL classes.

image

Next thing you have to do is create a connection string for your database in the web.config.  I'm not sure why the designer didn't do this for us.  A bug maybe?  So, go ahead and add your connection string to the web.config:

<connectionStrings>
    <add name="default" connectionString="Data Source=(local);Initial Catalog=NorthwindEF;Integrated Security=True"/>
</connectionStrings>

Again, the designer should have created a parameter-less constructor for us that would use the connection string from the configuration, but it did not for me (maybe it will for you???).  Open up the designer for your dbml file (in my case Northwind.designer.cs), and add a parameterless constructor:

public NorthwindDataContext() :
    base(System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString, mappingSource)
{
    OnCreated();
}

After that's done, you can go ahead and run the project.  You'll see that you're given a list of the tables:

table list

Clicking on each gives you the standard asp.net editable grid for that entity.

edit data

This also handles relationships automatically, by providing you a link that will filter the rows of another grid.

Now, this is a simple example.  Rails' approach is that the scaffolding is a great starting point.  From there you can do just about anything you want.

Does the ASP.NET Dynamic Data have the same philosophy?  Is this just a starting point?  If so, where's my model?  My Controllers?  My Tests?  If this isn't the intent, I'm really struggling to find what problem this truly solves.  Is there anyone out there who can set me straight?  In the mean time, I'm going to poke around and see what else you can do.

0 comments: