Introduction to ASP.NET Dynamic Data Part - I






3.18/5 (17 votes)
An article on ASP.NET Dynamic Data
Introduction
ASP.NET Dynamic data allows us to create data driven websites in minutes. ASP.NET Dynamic data comes as a template with VS2008 SP1. Using this template we can create a fully running website without writing a single line of code.
This part of the article will walk you through the features such as Insert/Update/Delete for all tables. Validations on the fields while editing. Paging and sorting for the data displayed from tables.
Background
When developing any data driven website we could not forget to maintain code tables, we create an interface to maintain code table information. For each project I have been doing this and repeating the code for each table. After awhile we developed a common code to maintain a code table for all applications. But when I saw ASP.NET Dynamic data I was knocked of my feet. It does create every single line of code for us to maintain database tables. We can do all functions on the tables that are needed in the real world like, Insert/Update/Delete. On top of that it allows us to modify the code depending on our needs. ASP.NET Dynamic data uses the LINQ to SQL class to store metadata of our database tables and uses that metadata to build a fully dynamic site. Initially, ASP.NET Dynamic data supported only SQL server as a database but with the new release, the LINQ to Entities class is supported which can be used for any database.
Using the Code
To get ASP.NET Dynamic data working on your m/c you will have to install VS2008 SP1.
Once you install SP1, start Visual Studio and create a new website.
Let's pick "Dynamic Data Web Site" from the list.
In this example we are going to use SQL Server as a database, so let's add Northwind database to our App_Data directory.
Now, let's create a LINQ to SQL class for our website.
After creating a LINQ to SQL class, drop a couple of tables on the class. When we drag these tables on the LINQ to SQL class, it internally creates one class for each table. And the columns of the table are created as properties to the class. This class also stores the type of the column and that information is used to provide validation for the fields while editing.
Now let's go and enable the website for ASP.NET Dynamic data. To do this we need to go to the global.asax file. You will find some lines of commented code, we need to uncomment the line where we register datacontext. We also need to set ScaffoldAllTables = true
, this will enable all tables dragged on LINQ to SQL class for Insert/Update/Delete functionality.
We can do it as shown below:
//
// model.RegisterContext(typeof(MyDemoOnDDDataContext),
new ContextConfiguration() { ScaffoldAllTables = true });
//
That's all we have to do, let's hit F5 to run the website.
We get list of tables on the screen and when we go the tables by clicking the link we get a list of records. We can do Insert/Update/Delete on our tables.
Let's see the validation provided by ASP.NET Dynamic Data. Click edit link on any record in the order_details table — the "quantity" column is declared as Integer in the database. When we try to put a non-integer value in that field we get an error saying that the column is Integer. Similarly, if the column is declared as not null in a database a required field validator is applied on field.
As I mentioned earlier the LINQ to SQL class stores metadata and applies validations on the fields.
ASP.NET Dynamic Data also provides sorting and paging by default, as seen in image below:
ASP.NET Dynamic Data also recognizes foreign key relations between the tables. Foreign keys are displayed as links and when we click on the link we are taken to the parent table. This is one of the amazing features of ASP.NET Dynamic Data.
Let's consider one example:
ProductID is a foreign key in the Categories table, when we see a list of records in the categories table the data in Product column is shown as a link. On clicking of "View Products" we go to the page showing a list of products.
As shown in the image below, we can see the list of products. If you see carefully we get the list of products filtered by category. The dropdowns provided at the top are filters for the relations between the tables.
We have achieved all these functionalities for our tables without writing a line of code and that’s what we need for code tables in our applications.
ASP.NET Dynamic Data can be used for code table maintainance, but that is not the only use of it. We can use dynamic data for prototyping the applications, rapid development of web sites, and, most importantly, we can customize a whole web site depending on our needs.
When we create a new web site using a Dynamic Data template we get a Directory named "DynamicData", this directory holds all the code to do the magic that we saw in this article. We have whole code so we can modify it to fulfill our needs.
Dynamic Data is based on generic design. There are predefined templates for each functionality. Displaying Data from each table is done using List.aspx, displaying details of record is done ListDetails.aspx page, displaying Edit template is done using Edit.aspx page and so on...
Because of this generic structure we have the ability to customize a whole web site and I am going to discuss how to customize Dynamic Data templates in next part of this article.
Points of Interest
In this article we saw the simplicity of design allowing us to create a whole web site in a couple of minutes, but the real fun lies in customizing what is available and we can take it to any level depending on our imagination.