Monday, 5 March 2012

Dynamic Data in Asp.net 4.0. Part1 : Introduction

Dynamic Data in Asp.net 4.0. Part1 : Introduction
Introduction

Dynamic Data in Asp.net was first introduced in .NET 3.5 SP1, and later was improved in Asp.net 4.0. For those who were longing for a RAD (Rapid Application Development) platform in Asp.net, Dynamic Data was a real good news for them. It provides a framework to quickly develop a Data Driven Asp.net application by generating CRUD functionality for the database tables. But, if you want to consider Dynamic Data as just another Code Generator tool, you are completely wrong. Generation of codes is just a part of the Dynamic Data framework. What makes it different? Well, unlike most other code generation frameworks, the framework allowes you to utilize the powerful Dynamic Data features without using the code generation based on database tables (scaffolding) at all! Most importantly, the Dynamic Data framework is so elegantly designed that its really easy for you to customize the framework generated codes at each granular level and this makes it really valuable and effective to be considered as a RAD framework to build your next Asp.net application.

In this Part1 of the article on Dynamic Data in Asp.net, I'll cover the basics and then will eventually cover the customizations which you might need to know when developing real applications.

Here we go.

Creating a Dynamic Data Application project

Assuming you are using Visual Studio 2010, go to File->New project->Asp.net Dynamic Data Entities Web Application (If you want to develop a Dynamic Data application based on Entity framework) or File->New project->Asp.net Dynamic Data Linq to SQL Web Application (If you want to develop a Dynamic Data application based on Linq to SQL) and click "OK" to let Visual Studio create the project.


Figure : Add new dynamic data project
Once the project is created, it will be shown in the solution explorer as below:


Figure : Dynamic Data project in Solution Explorer
Running a Dynamic Data Project

As Dynamic Data is a Data driven Asp.net application, it cannot be run unless we configure the data source for the application project.

Follow these steps to execute a "Hello world" run of the Dynamic Data application.
Right click on the project and Add an "ADO.NET Entity Data Model" to launch the wizerd to configure the Entity data model and specify inputs in each step until Finish.


Figure : Add ADO.NET Entity Data Model

Figure : Choose Data Model

Figure : Choose Database


Figure : Choose Database objects (Tables)
Once done, the Entity Model will be added in the solution and it will be displayed in the designer view and in the solution explorer.


Figure : An Entity data model
Figure : Entity Model in Solution Explorer
Double click on the Designer.cs file of the Entity Model (edmx) to open the corresponding Code file and expand the region "Context" to find the Entity context class name.


Figure : EntityContext class
Copy the class name (The class which inherits the ObjectContext class) for the Entity context and open the Global.asax in the Visual Studio editor to register and configure the Dynamic Data with the Entity context class. 

To configure, uncomment the following line in the Global.asax (Which is commented out by default , and paste the Entity context class name (DynamicDataEntities in this example) within the typeof() operation (The first red box)



Figure : Configure the Entity Context with Dynamic Data

Also, specify ScaffoldAllTables = true (Which is by default false) to specify that the Dynamic Data engine should generate codes to perform CRUD operations for all tables in the database (Ideally, this may not be desired, but, for understanding and running the very first Dynamic Data application, lets keep this simple).
Build the Application and once build is successful, Run or Debug the application. It will launch the application which should be something like as follows:

Figure : Dynamic Data web site
 Click on a particular Table name in the listing and it will show a listing page including all filtering options (For all the foreign keys) with Insert/Update/Delete links for each data


Figure : Listing page for a particular table

Using the Filter options you can filter the listing page by selecting particular filter data. The filter data are retrieved from the database using the foreign key values. Also, clicking on the "Insert new item" link below allows you to enter a new item in the system and the Edit and Delete links let you Edit a particular item and Delete one respectively.

Creating a Dynamic Data web application project and running it by configuring an Entity or Linq to SQL Data source requires 5 minutes of works at best and the end result is a powerful bug free CRUD application for the entire database generated right way.

In the next part of this article we would try to understand the building blocks of the Dynamic Data Application project, which would eventually help us customize the application when we would implement a real application using this framework.


No comments :